|
1 | 1 | //! 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 |
2 | 46 |
|
3 | 47 | #![deny(unsafe_op_in_unsafe_fn)]
|
4 | 48 | #![unstable(feature = "solid_ext", issue = "none")]
|
|
0 commit comments