Skip to content

Commit 76084c1

Browse files
committed
Add an option to use the new I/O safety types and traits in std.
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
1 parent 5f68afb commit 76084c1

13 files changed

+811
-619
lines changed

.github/workflows/main.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,30 @@ jobs:
4747
with:
4848
toolchain: ${{ matrix.rust }}
4949
- run: cargo test --workspace --all-features
50+
51+
test_use_std:
52+
name: Test with std's types and traits
53+
runs-on: ${{ matrix.os }}
54+
strategy:
55+
matrix:
56+
build: [ubuntu-nightly, windows-nightly]
57+
include:
58+
- build: ubuntu-nightly
59+
os: ubuntu-latest
60+
rust: nightly
61+
- build: windows-nightly
62+
os: windows-latest
63+
rust: nightly
64+
65+
env:
66+
RUSTFLAGS: --cfg io_lifetimes_use_std
67+
RUSTDOCFLAGS: --cfg io_lifetimes_use_std
68+
69+
steps:
70+
- uses: actions/checkout@v2
71+
with:
72+
submodules: true
73+
- uses: ./.github/actions/install-rust
74+
with:
75+
toolchain: ${{ matrix.rust }}
76+
- run: cargo test --workspace --all-features

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,32 @@ is what motivates having `BorrowedFd` instead of just using `&OwnedFd`.
9999
Note the use of `Option<OwnedFd>` as the return value of `open`, representing
100100
the fact that it can either succeed or fail.
101101

102+
## I/O Safety in Rust Nightly
103+
104+
The I/O Safety
105+
[implementation PR](https://github.com/rust-lang/rust/pull/87329) has now
106+
landed and is available on Rust Nightly. It can be used directly, or through
107+
io-lifetimes: when `io_lifetimes_use_std` mode is enabled, such as by setting
108+
`RUSTFLAGS=--cfg=io_lifetimes_use_std`, io-lifetimes uses the std's `OwnedFd`,
109+
`BorrowedFd`, and `AsFd` instead of defining its own.
110+
111+
The code in `std` uses `From<OwnedFd>` and `Into<OwnedFd>` instead of `FromFd`
112+
and `IntoFd`. io-lifetimes is unable to provide impls for these for third-party
113+
types, so it continues to provide `FromFd` and `IntoFd` for now, with default
114+
impls that forward to `From<OwnedFd>` and `Into<OwnedFd>` in
115+
`io_lifetimes_use_std` mode.
116+
117+
io-lifetimes also includes several features which are not (yet?) in std,
118+
including the portability traits `AsFilelike`/`AsSocketlike`/etc., the
119+
`from_into_*` functions in the `From*` traits, and [views].
120+
121+
If you test a crate with the std I/O safety types and traits, or io-lifetimes
122+
in `io_lifetimes_use_std` mode, please post a note about it in the
123+
[I/O safety tracking issue] as an example of usage.
124+
125+
[I/O safety tracking issue]: https://github.com/rust-lang/rust/issues/87074
126+
[views]: https://docs.rs/io-lifetimes/*/io_lifetimes/views/index.html
127+
102128
## Prior Art
103129

104130
There are several similar crates: [fd](https://crates.io/crates/fd),

examples/easy-conversions.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//! implementing `IntoFilelike` and `FromSocketlike` to types implementing
33
//! `FromFilelike` and `IntoSocketlike`, respectively.
44
5+
#![cfg_attr(io_lifetimes_use_std, feature(io_safety))]
6+
57
use io_lifetimes::FromFilelike;
68
use std::fs::File;
79
use std::io::{self, Read};

examples/flexible-apis.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
//! The following uses the POSIX-ish `Fd` types; similar considerations
55
//! apply to the Windows and portable types.
66
7+
#![cfg_attr(io_lifetimes_use_std, feature(io_safety))]
8+
79
#[cfg(all(feature = "close", not(windows)))]
810
use io_lifetimes::{AsFd, BorrowedFd, IntoFd, OwnedFd};
911

examples/hello.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! the io-lifetimes API.
33
44
#![cfg_attr(not(rustc_attrs), allow(unused_imports))]
5+
#![cfg_attr(io_lifetimes_use_std, feature(io_safety))]
56

67
#[cfg(feature = "close")]
78
use io_lifetimes::example_ffi::*;

examples/owning-wrapper.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! A simple example implementing the main traits for a type.
22
3+
#![cfg_attr(io_lifetimes_use_std, feature(io_safety))]
4+
35
use io_lifetimes::OwnedFilelike;
46
#[cfg(not(windows))]
57
use io_lifetimes::{AsFd, BorrowedFd, FromFd, IntoFd, OwnedFd};

examples/portable-views.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! io-lifetimes provides safe, convenient, and portable ways to temporarily
22
//! view an I/O resource as a `File`, `Socket`, or other types.
33
4+
#![cfg_attr(io_lifetimes_use_std, feature(io_safety))]
5+
46
use io_lifetimes::AsFilelike;
57
use std::fs::File;
68
use std::io::{self, stdout};

0 commit comments

Comments
 (0)