Skip to content

Commit ddfe168

Browse files
committed
kmc-solid: Document I/O safety in std::os::solid::io
Mostly copied from `std::os::unix::io`, except quantifying file descriptors with SOLID Sockets and removing the paragraph mentioning `mmap`.
1 parent 5d3aefe commit ddfe168

File tree

1 file changed

+44
-0
lines changed
  • library/std/src/os/solid

1 file changed

+44
-0
lines changed

library/std/src/os/solid/io.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,48 @@
11
//! SOLID-specific extensions to general I/O primitives
2+
//!
3+
//! Just like raw pointers, raw SOLID Sockets file descriptors point to
4+
//! resources with dynamic lifetimes, and they can dangle if they outlive their
5+
//! resources or be forged if they're created from invalid values.
6+
//!
7+
//! This module provides three types for representing raw file descriptors
8+
//! with different ownership properties: raw, borrowed, and owned, which are
9+
//! analogous to types used for representing pointers:
10+
//!
11+
//! | Type | Analogous to |
12+
//! | ------------------ | ------------ |
13+
//! | [`RawFd`] | `*const _` |
14+
//! | [`BorrowedFd<'a>`] | `&'a _` |
15+
//! | [`OwnedFd`] | `Box<_>` |
16+
//!
17+
//! Like raw pointers, `RawFd` values are primitive values. And in new code,
18+
//! they should be considered unsafe to do I/O on (analogous to dereferencing
19+
//! them). Rust did not always provide this guidance, so existing code in the
20+
//! Rust ecosystem often doesn't mark `RawFd` usage as unsafe. Once the
21+
//! `io_safety` feature is stable, libraries will be encouraged to migrate,
22+
//! either by adding `unsafe` to APIs that dereference `RawFd` values, or by
23+
//! using to `BorrowedFd` or `OwnedFd` instead.
24+
//!
25+
//! Like references, `BorrowedFd` values are tied to a lifetime, to ensure
26+
//! that they don't outlive the resource they point to. These are safe to
27+
//! use. `BorrowedFd` values may be used in APIs which provide safe access to
28+
//! any system call except for:
29+
//!
30+
//! - `close`, because that would end the dynamic lifetime of the resource
31+
//! without ending the lifetime of the file descriptor.
32+
//!
33+
//! - `dup2`/`dup3`, in the second argument, because this argument is
34+
//! closed and assigned a new resource, which may break the assumptions
35+
//! other code using that file descriptor.
36+
//!
37+
//! `BorrowedFd` values may be used in APIs which provide safe access to `dup`
38+
//! system calls, so types implementing `AsFd` or `From<OwnedFd>` should not
39+
//! assume they always have exclusive access to the underlying file
40+
//! description.
41+
//!
42+
//! Like boxes, `OwnedFd` values conceptually own the resource they point to,
43+
//! and free (close) it when they are dropped.
44+
//!
45+
//! [`BorrowedFd<'a>`]: crate::os::solid::io::BorrowedFd
246
347
#![deny(unsafe_op_in_unsafe_fn)]
448
#![unstable(feature = "solid_ext", issue = "none")]

0 commit comments

Comments
 (0)