uapi/docs.rs
1//! Module containing the crate documentation
2//!
3//! This crate provides safe wrappers for Unix APIs.
4//!
5//! All wrappers are provided in the crate root. There are no wrapper types unless the raw
6//! types cannot be used safely and as noted below.
7//!
8//! # File Descriptors
9//!
10//! This crate implements lifecycle management of file descriptors with two types:
11//!
12//! - `OwnedFd` - An owned file descriptor
13//! - `Fd` - Any kind of file descriptor
14//!
15//! `OwnedFd` is distinct from `Fd` in that it assumes ownership of the contained file
16//! descriptor. It implements `Drop` and can be converted from and to many libstd types.
17//!
18//! Both `OwnedFd` and `Fd` implement `Read` and `Write`, and `Deref` to `libc::c_int`.
19//!
20//! APIs that return owned file descriptors return `OwnedFd`. APIs that assume ownership
21//! take `OwnedFd`.
22//!
23//! It's important to recognize that `OwnedFd` is not about safety but about convenience.
24//! An `OwnedFd` can be created from any integer by calling `OwnedFd::new`. An `OwnedFd`
25//! can be unwrapped by calling `OwnedFd::unwrap`.
26//!
27//! # Strings
28//!
29//! This crate contains 3 string types:
30//!
31//! - `&Bstr` - Any sequence of bytes
32//! - `&Ustr` - Any sequence of bytes with a terminating nul byte
33//! - `Ustring` - Owned version of `&Ustr`
34//!
35//! And several associated traits:
36//!
37//! - `AsUstr` - Cheap conversion into `&Ustr`
38//! - `Bytes` - Types with a binary representation
39//! - `IntoUstr` - Types that can be converted into `Cov<'a, Ustr>`
40//!
41//! `&Bstr` is a simple wrapper around `&[u8]` which supports conversions from and to many
42//! libstd string types:
43//!
44//! - `&[u8]`
45//! - `&str`
46//! - `&Path`
47//! - `&OsStr`
48//! - `&CStr`
49//!
50//! `&Bstr` supports `Debug` and `Display`. The implementations are the same as the ones
51//! used for `&Path`. To access the `Display` implementation, call `Bstr::display()`.
52//! `&Bstr` supports `PartialEq` for many libstd types, `&Ustr`, and `Ustring`.
53//!
54//! `&Ustr` is like `&Bstr` but guarantees that there is a nul byte after the last element
55//! of the sequence. Unlike `&CStr`, `&Ustr` can contain inner nul bytes. `&Ustr` derefs
56//! to `&Bstr`. Like `&Bstr`, `&Ustr` can be converted from and to many libstd types.
57//!
58//! `Ustring` is the owned version of `&Ustr`. It supports conversion from and to many
59//! owned libstd string types.
60//!
61//! APIs that return strings usually return `&CStr`. Since `&CStr` is uncomfortable to
62//! work with, `AsUstr` provides the `as_ustr` method on `&CStr`.
63//!
64//! APIs that accept strings usually accept `impl IntoUstr`. `IntoUstr` is implemented for
65//! many libstd string types, `&Bstr`, `&Ustr`, and `Ustring`. The implementations of
66//! `IntoUstr` perform least-cost conversions into `&Ustr`. For example
67//!
68//! ```rust,ignore
69//! fn f<'a>(s: impl IntoUstr<'a>) {
70//! let _ = s.into_ustr();
71//! }
72//!
73//! f("abc"); // allocates
74//! f("abc\0"); // does not allocate
75//! f("abc".to_string()); // appends a nul byte to the `String`
76//! f(CStr::from_ptr(p)); // does not allocate
77//! ```
78//!
79//! # Pod & Packed
80//!
81//! This crate contains two traits for conversions of data structures from and to bytes:
82//!
83//! - `Pod` - Plain Old Data
84//! - `Packed` - Types without padding bytes
85//!
86//! The central property of `Pod` types is that they can be converted from bytes without
87//! first having to validate the bytes. All C types are `Pod`, though this crate only
88//! implements `Pod` for a selected number of types. More implementations might get added
89//! in the future.
90//!
91//! A `Packed` type does not contain padding bytes. This means that they can be safely
92//! converted into bytes because all bytes are guaranteed to be initialized.
93//!
94//! These types are useful for working with APIs that transfer structures over
95//! byte-oriented APIs like cmsg, inotify, signalfd, etc.
96//!
97//! The utilities to facilitate this are
98//!
99//! - `pod_zeroed` - Returns an instance of a `Pod` type with all bytes zeroed.
100//! - `pod_read` - Reads an instance of a `Packed` type as an instance of a `Pod` type.
101//! - `pod_iter` - Iterates over the instances of a `Pod` typed stored in an instance of a
102//! `Packed` type.
103//! - `pod_read_init` - Reads an initial part of an instance of a `Packed` type as an
104//! instance of a `Pod` type.
105//! - `pod_write` - Writes an instance of a `Packed` type to an instance of a `Pod` type.
106//! - `as_bytes` - Returns the bytes of an instance of a `Packed` type.
107//!
108//! If a certain type does not implement `Pod` or `Packed` and you require it, you can
109//! use `assert_pod` or `assert_packed`.
110//!
111//! # Control Messages (cmsg)
112//!
113//! This crate provides safe functions for writing and reading control messages:
114//!
115//! - `cmsg_write` - Writes a cmsg to a byte buffer
116//! - `cmsg_read` - Reads a cmsg from a byte buffer