From a9fd3180951cceb9edccfeaf99fdbd3aac1c09d4 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 19 May 2019 14:27:11 -0700 Subject: [PATCH] prep for publishing filedescriptor to crates.io --- filedescriptor/Cargo.toml | 5 +++++ filedescriptor/src/lib.rs | 7 +++++++ filedescriptor/src/windows.rs | 24 ++++++++++++------------ 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/filedescriptor/Cargo.toml b/filedescriptor/Cargo.toml index 130a4ad59..808126699 100644 --- a/filedescriptor/Cargo.toml +++ b/filedescriptor/Cargo.toml @@ -3,6 +3,10 @@ name = "filedescriptor" version = "0.1.0" authors = ["Wez Furlong"] edition = "2018" +repository = "https://github.com/wez/wzsh" +description = "More ergonomic wrappers around RawFd and RawHandle" +license = "MIT" +documentation = "https://docs.rs/filedescriptor" [dependencies] failure = "0.1" @@ -15,4 +19,5 @@ winapi = { version = "0.3", features = [ "handleapi", "fileapi", "namedpipeapi", + "processthreadsapi" ]} diff --git a/filedescriptor/src/lib.rs b/filedescriptor/src/lib.rs index 540d6ea3d..c2877eccd 100644 --- a/filedescriptor/src/lib.rs +++ b/filedescriptor/src/lib.rs @@ -1,3 +1,10 @@ +//! The purpose of this crate is to make it a bit more ergonomic for portable +//! applications that need to work with the platform level `RawFd` and +//! `RawHandle` types. +//! Rather than conditionally using `RawFd` and `RawHandle`, the `FileDescriptor` +//! type can be used to manage ownership, duplicate, read and write. +//! The `Pipe` type makes it more convenient to create a pipe and manage +//! the lifetime of both the read and write ends of that pipe. use failure::Fallible; #[cfg(unix)] mod unix; diff --git a/filedescriptor/src/windows.rs b/filedescriptor/src/windows.rs index 25109a392..35ab991d8 100644 --- a/filedescriptor/src/windows.rs +++ b/filedescriptor/src/windows.rs @@ -5,12 +5,12 @@ use crate::{ use failure::{bail, Fallible}; use std::io::{self, Error as IoError}; use std::os::windows::prelude::*; -use std::os::windows::raw::HANDLE; use std::ptr; use winapi::um::fileapi::*; use winapi::um::handleapi::*; use winapi::um::namedpipeapi::CreatePipe; use winapi::um::processthreadsapi::*; +use winapi::um::winnt::HANDLE; /// `RawFileDescriptor` is a platform independent type alias for the /// underlying platform file descriptor type. It is primarily useful @@ -39,8 +39,8 @@ unsafe impl Send for OwnedHandle {} impl Drop for OwnedHandle { fn drop(&mut self) { - if self.handle != INVALID_HANDLE_VALUE && !self.handle.is_null() { - unsafe { CloseHandle(self.handle) }; + if self.handle != INVALID_HANDLE_VALUE as _ && !self.handle.is_null() { + unsafe { CloseHandle(self.handle as _) }; } } } @@ -55,7 +55,7 @@ impl OwnedHandle { #[inline] pub(crate) fn dup_impl(f: &F) -> Fallible { let handle = f.as_raw_file_descriptor(); - if handle == INVALID_HANDLE_VALUE || handle.is_null() { + if handle == INVALID_HANDLE_VALUE as _ || handle.is_null() { return Ok(OwnedHandle { handle }); } @@ -83,13 +83,13 @@ impl OwnedHandle { } impl AsRawHandle for OwnedHandle { - fn as_raw_handle(&self) -> HANDLE { + fn as_raw_handle(&self) -> RawHandle { self.handle } } impl IntoRawHandle for OwnedHandle { - fn into_raw_handle(self) -> HANDLE { + fn into_raw_handle(self) -> RawHandle { let handle = self.handle; std::mem::forget(self); handle @@ -107,13 +107,13 @@ impl FileDescriptor { } impl IntoRawHandle for FileDescriptor { - fn into_raw_handle(self) -> HANDLE { + fn into_raw_handle(self) -> RawHandle { self.handle.into_raw_handle() } } impl AsRawHandle for FileDescriptor { - fn as_raw_handle(&self) -> HANDLE { + fn as_raw_handle(&self) -> RawHandle { self.handle.as_raw_handle() } } @@ -171,17 +171,17 @@ impl io::Write for FileDescriptor { impl Pipe { pub fn new() -> Fallible { - let mut read: HANDLE = INVALID_HANDLE_VALUE; - let mut write: HANDLE = INVALID_HANDLE_VALUE; + let mut read: HANDLE = INVALID_HANDLE_VALUE as _; + let mut write: HANDLE = INVALID_HANDLE_VALUE as _; if unsafe { CreatePipe(&mut read, &mut write, ptr::null_mut(), 0) } == 0 { bail!("CreatePipe failed: {}", IoError::last_os_error()); } Ok(Pipe { read: FileDescriptor { - handle: OwnedHandle { handle: read }, + handle: OwnedHandle { handle: read as _ }, }, write: FileDescriptor { - handle: OwnedHandle { handle: write }, + handle: OwnedHandle { handle: write as _ }, }, }) }