Skip to content

Commit

Permalink
Add an option to use the new I/O safety types and traits in std.
Browse files Browse the repository at this point in the history
I/O safety is now [in Rust Nightly]; add a mode to io-lifetimes to use
std's types and traits. See the blurb in README.md for more details.

[in Rust Nightly]: rust-lang/rust#87329
  • Loading branch information
sunfishcode committed Aug 21, 2021
1 parent 5f68afb commit 6e44a05
Show file tree
Hide file tree
Showing 12 changed files with 776 additions and 619 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,32 @@ is what motivates having `BorrowedFd` instead of just using `&OwnedFd`.
Note the use of `Option<OwnedFd>` as the return value of `open`, representing
the fact that it can either succeed or fail.

## I/O Safety in Rust Nightly

The I/O Safety
[implementation PR](https://github.com/rust-lang/rust/pull/87329) has now
landed and is available on Rust Nightly. It can be used directly, or through
io-lifetimes: when `io_lifetimes_use_std` mode is enabled, such as by setting
`RUSTFLAGS=--cfg=io_lifetimes_use_std`, io-lifetimes uses the std's `OwnedFd`,
`BorrowedFd`, and `AsFd` instead of defining its own.

The code in `std` uses `From<OwnedFd>` and `Into<OwnedFd>` instead of `FromFd`
and `IntoFd`. io-lifetimes is unable to provide impls for these for third-party
types, so it continues to provide `FromFd` and `IntoFd` for now, with default
impls that forward to `From<OwnedFd>` and `Into<OwnedFd>` in
`io_lifetimes_use_std` mode.

io-lifetimes also includes several features which are not (yet?) in std,
including the portability traits `AsFilelike`/`AsSocketlike`/etc., the
`from_into_*` functions in the `From*` traits, and [views].

If you test a crate with the std I/O safety types and traits, or io-lifetimes
in `io_lifetimes_use_std` mode, please post a note about it in the
[I/O safety tracking issue] as an example of usage.

[I/O safety tracking issue]: https://github.com/rust-lang/rust/issues/87074
[views]: https://docs.rs/io-lifetimes/*/io_lifetimes/views/index.html

## Prior Art

There are several similar crates: [fd](https://crates.io/crates/fd),
Expand Down
2 changes: 2 additions & 0 deletions examples/easy-conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//! implementing `IntoFilelike` and `FromSocketlike` to types implementing
//! `FromFilelike` and `IntoSocketlike`, respectively.

#![cfg_attr(io_lifetimes_use_std, feature(io_safety))]

use io_lifetimes::FromFilelike;
use std::fs::File;
use std::io::{self, Read};
Expand Down
2 changes: 2 additions & 0 deletions examples/flexible-apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//! The following uses the POSIX-ish `Fd` types; similar considerations
//! apply to the Windows and portable types.

#![cfg_attr(io_lifetimes_use_std, feature(io_safety))]

#[cfg(all(feature = "close", not(windows)))]
use io_lifetimes::{AsFd, BorrowedFd, IntoFd, OwnedFd};

Expand Down
1 change: 1 addition & 0 deletions examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! the io-lifetimes API.

#![cfg_attr(not(rustc_attrs), allow(unused_imports))]
#![cfg_attr(io_lifetimes_use_std, feature(io_safety))]

#[cfg(feature = "close")]
use io_lifetimes::example_ffi::*;
Expand Down
2 changes: 2 additions & 0 deletions examples/owning-wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! A simple example implementing the main traits for a type.

#![cfg_attr(io_lifetimes_use_std, feature(io_safety))]

use io_lifetimes::OwnedFilelike;
#[cfg(not(windows))]
use io_lifetimes::{AsFd, BorrowedFd, FromFd, IntoFd, OwnedFd};
Expand Down
2 changes: 2 additions & 0 deletions examples/portable-views.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! io-lifetimes provides safe, convenient, and portable ways to temporarily
//! view an I/O resource as a `File`, `Socket`, or other types.

#![cfg_attr(io_lifetimes_use_std, feature(io_safety))]

use io_lifetimes::AsFilelike;
use std::fs::File;
use std::io::{self, stdout};
Expand Down
Loading

0 comments on commit 6e44a05

Please sign in to comment.