Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to use the new I/O safety types and traits in std. #2

Merged
merged 1 commit into from
Aug 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,30 @@ jobs:
with:
toolchain: ${{ matrix.rust }}
- run: cargo test --workspace --all-features

test_use_std:
name: Test with std's types and traits
runs-on: ${{ matrix.os }}
strategy:
matrix:
build: [ubuntu-nightly, windows-nightly]
include:
- build: ubuntu-nightly
os: ubuntu-latest
rust: nightly
- build: windows-nightly
os: windows-latest
rust: nightly

env:
RUSTFLAGS: --cfg io_lifetimes_use_std
RUSTDOCFLAGS: --cfg io_lifetimes_use_std

steps:
- uses: actions/checkout@v2
with:
submodules: true
- uses: ./.github/actions/install-rust
with:
toolchain: ${{ matrix.rust }}
- run: cargo test --workspace --all-features
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,39 @@ 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, io-lifetimes uses
the std's `OwnedFd`, `BorrowedFd`, and `AsFd` instead of defining its own.

To enable `io_lifetimes_use_std` mode:
- Set the environment variable `RUSTFLAGS=--cfg=io_lifetimes_use_std`, and
- add `#![cfg_attr(io_lifetimes_use_std, feature(io_safety))]` to your
lib.rs or main.rs.

Note that, unfortunately, `io_lifetimes_use_std` mode doesn't support the
optional impls for third-party crates.

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