Skip to content

Commit

Permalink
Backport the HandleOrInvalid patch from the PR.
Browse files Browse the repository at this point in the history
Co-authored-by: Josh Triplett <[email protected]>
  • Loading branch information
sunfishcode and joshtriplett committed Aug 18, 2021
1 parent 695bee7 commit 5f68afb
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 135 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl FromFd for OwnedFd { ... }
```

On Windows, there are `Handle` and `Socket` versions of every `Fd` thing, and
a special `OptionFileHandle` type to cope with inconsistent error reporting
a special `HandleOrInvalid` type to cope with inconsistent error reporting
in the Windows API.

Full API documentation:
Expand Down
2 changes: 1 addition & 1 deletion examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn main() -> io::Result<()> {
#[cfg(windows)]
fn main() -> io::Result<()> {
let handle = unsafe {
// Open a file, which returns an `OptionFileHandle`, which we can fallibly
// Open a file, which returns an `HandleOrInvalid`, which we can fallibly
// convert into an `OwnedFile`.
let handle: OwnedHandle = CreateFileW(
['C' as u16, 'O' as _, 'N' as _, 0].as_ptr(),
Expand Down
6 changes: 3 additions & 3 deletions src/example_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#[cfg(any(unix, target_os = "wasi"))]
use crate::{BorrowedFd, OwnedFd};
#[cfg(windows)]
use crate::{BorrowedHandle, OptionFileHandle, OwnedHandle};
use crate::{BorrowedHandle, HandleOrInvalid, OwnedHandle};

#[cfg(any(unix, target_os = "wasi"))]
use libc::{c_char, c_int, c_void, size_t, ssize_t};
Expand All @@ -35,7 +35,7 @@ extern "C" {
#[cfg(any(unix, target_os = "wasi"))]
pub use libc::{O_CLOEXEC, O_CREAT, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY};

/// The Windows analogs of the above. Note the use of [`OptionFileHandle`] as
/// The Windows analogs of the above. Note the use of [`HandleOrInvalid`] as
/// the return type for `CreateFileW`, since that function is defined to return
/// [`INVALID_HANDLE_VALUE`] on error instead of null.
#[cfg(windows)]
Expand All @@ -48,7 +48,7 @@ extern "C" {
dwCreationDisposition: DWORD,
dwFlagsAndAttributes: DWORD,
hTemplateFile: HANDLE,
) -> OptionFileHandle;
) -> HandleOrInvalid;
pub fn ReadFile(
hFile: HANDLE,
lpBuffer: LPVOID,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub use traits::{AsHandle, AsSocket, FromHandle, FromSocket, IntoHandle, IntoSoc
#[cfg(any(unix, target_os = "wasi"))]
pub use types::{BorrowedFd, OwnedFd};
#[cfg(windows)]
pub use types::{BorrowedHandle, BorrowedSocket, OptionFileHandle, OwnedHandle, OwnedSocket};
pub use types::{BorrowedHandle, BorrowedSocket, HandleOrInvalid, OwnedHandle, OwnedSocket};

pub use portability::{
AsFilelike, AsSocketlike, BorrowedFilelike, BorrowedSocketlike, FromFilelike, FromSocketlike,
Expand Down
4 changes: 2 additions & 2 deletions src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(any(unix, target_os = "wasi"))]
use crate::{BorrowedFd, OwnedFd};
#[cfg(windows)]
use crate::{BorrowedHandle, BorrowedSocket, OptionFileHandle, OwnedHandle, OwnedSocket};
use crate::{BorrowedHandle, BorrowedSocket, HandleOrInvalid, OwnedHandle, OwnedSocket};
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
#[cfg(target_os = "wasi")]
Expand Down Expand Up @@ -307,7 +307,7 @@ impl FromSocket for OwnedSocket {
}

#[cfg(windows)]
impl FromHandle for OptionFileHandle {
impl FromHandle for HandleOrInvalid {
#[inline]
fn from_handle(owned: OwnedHandle) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
Expand Down
Loading

0 comments on commit 5f68afb

Please sign in to comment.