Skip to content

Commit 701321e

Browse files
committed
Start sample stdlib documentation
1 parent 8676ab7 commit 701321e

File tree

4 files changed

+265
-0
lines changed

4 files changed

+265
-0
lines changed

src/SUMMARY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@
129129

130130
- [The Rust runtime](runtime.md)
131131

132+
- [Standard Library](stdlib.md)
133+
- [core](core.md)
134+
- [core::ffi](core/ffi.md)
135+
132136
- [Appendices](appendices.md)
133137
- [Macro Follow-Set Ambiguity Formal Specification](macro-ambiguity.md)
134138
- [Influences](influences.md)

src/core.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# core
2+
3+
r[library.core]
4+
5+
r[library.core.intro]
6+
The `core` library is the most fundamental library in the Rust language. Every crate (other than itself) depends on it. `core` defines fundamental operations and language item types, as well as (most) functions on primitive types.
7+
8+
## Synopsis
9+
10+
r[library.core.synopsis]
11+
```rust
12+
13+
pub mod ffi;
14+
pub mod marker;
15+
16+
```

src/core/ffi.md

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
# Module core::ffi
2+
3+
r[core.ffi]
4+
5+
6+
## Module core::ffi Synopsis
7+
8+
r[core.ffi.synopsis]
9+
10+
<!--ignore: synopsis of module, not necessarily valid rust-->
11+
```rust,ignore
12+
13+
pub struct FromBytesUntilNulError(/*private fields*/);
14+
pub struct FromBytesWithNulError(/*private fields*/);
15+
pub struct CStr{/*private fields*/}
16+
17+
pub enum c_void{}
18+
19+
pub type c_char = /* see below */;
20+
pub type c_double = /* see below */;
21+
pub type c_float = /* see below */;
22+
pub type c_int = /* see below */;
23+
pub type c_long = /*see below*/;
24+
pub type c_longlong = /*see below*/;
25+
pub type c_schar = /*see below*/;
26+
pub type c_short = /*see below*/;
27+
pub type c_uchar = /*see below*/;
28+
pub type c_ulong = /*see below*/;
29+
pub type c_ulonglong = /*see below*/;
30+
pub type c_ushort = /* see below */;
31+
```
32+
33+
## CStr
34+
35+
r[core.ffi.cstr]
36+
37+
r[core.ffi.cstr.intro]
38+
A `CStr` is a slice of bytes that contains a nul-terminated string of arbitrary non-nul bytes.
39+
40+
r[core.ffi.cstr.literal]
41+
A literal of the form `c"<string literal content>"` has type `&'static core::ffi::CStr`.
42+
43+
r[core.ffi.cstr.sized]
44+
`CStr` is an unsized type.
45+
46+
### `CStr::from_ptr`
47+
48+
r[core.ffi.cstr.from_ptr]
49+
50+
r[core.ffi.cstr.from_ptr.def]
51+
<!--ignore: incomplete code fragment, showing a synopsis as a function -->
52+
```rust,ignore
53+
impl CStr{
54+
pub const unsafe fn from_ptr<'a>(ptr: *const c_char) -> &'a CStr;
55+
}
56+
```
57+
58+
r[core.ffi.cstr.from_ptr.intro]
59+
The `CStr::from_ptr` can be used to `unsafe`ly construct a `CStr` from a pointer to a nul-terminated C string.
60+
61+
r[core.ffi.cstr.from_ptr.precondition]
62+
The behavior of this function is undefined unless:
63+
* There exists a value `i` of type `usize`,
64+
such that the range `[ptr, ptr.add(i)]` is a range valid for reads, and `ptr.add(i).read()==0`,
65+
* `i < (isize::MAX as usize)`,
66+
67+
> [!WARNING]
68+
> In addition, to safely use the result, the callee must ensure that the validity of `ptr` remains for the duration of `'a`,
69+
> and that the bytes referred to be the `CStr` aren't modified for the duration of `'a`.
70+
71+
r[core.ffi.cstr.from_ptr.postcondition]
72+
The returned reference is a shared borrow derived from `ptr` which borrows `[ptr, ptr.add(i+1))`
73+
74+
r[core.ffi.cstr.from_ptr.return]
75+
The return value is a `&CStr` that starts at `ptr`, and for which `CStr::count_bytes` returns `i`.
76+
77+
r[core.ffi.cstr.from_ptr.safety]
78+
`CStr::from_ptr` is an `unsafe` function.
79+
80+
r[core.ffi.cstr.from_ptr.const]
81+
`CStr::from_ptr` is a `const` function.
82+
83+
### `CStr::from_bytes_until_nul`
84+
85+
r[core.ffi.cstr.from_bytes_until_nul]
86+
87+
r[core.ffi.cstr.from_bytes_until_nul.def]
88+
<!--ignore: incomplete code fragment, showing a synopsis as a function def -->
89+
```rust,ignore
90+
impl CStr{
91+
pub const fn from_bytes_until_nul(bytes: &[u8]) -> Result<&CStr, FromBytesUntilNulError>;
92+
}
93+
```
94+
95+
r[core.ffi.cstr.from_bytes_until_nul.intro]
96+
Constructs a `CStr` slice over `bytes`, up to the first instance of a `0` byte, terminating the string. If no such terminator exists, an error is returned instead.
97+
98+
r[core.ffi.cstr.from_bytes_until_nul.return]
99+
If there exists some index `i` of type `usize`, such that `i < bytes.len()` and `bytes[i] == 0`, returns a `CStr` borrowed from `bytes` starting from the first index,
100+
and such that `CStr::count_bytes` returns `i` . If no such `i` exists, returns a `FromBytesUntilNulError` that indicates the failure.
101+
102+
> [!NOTE]
103+
> If there is exactly one 0 byte in `bytes`, located at the last index of the slice, the returned slice is identical to the one returned by [`CStr::from_bytes_with_nul`][core.ffi.cstr.from_bytes_with_nul]
104+
105+
### `CStr::from_bytes_with_nul`
106+
107+
r[core.ffi.cstr.from_bytes_with_nul]
108+
109+
r[core.ffi.cstr.from_bytes_with_nul.def]
110+
<!--ignore: incomplete code fragment, showing a synopsis as a function def -->
111+
```rust,ignore
112+
impl CStr{
113+
pub const fn from_bytes_with_nul(bytes: &[u8]) -> Result<&CStr, FromBytesWithNulError>;
114+
}
115+
```
116+
117+
r[core.ffi.cstr.from_bytes_with_nul.intro]
118+
Constructs a `CStr` slice over `bytes`, provided that exactly one `0` byte occurs at the very end of `bytes`. If the last byte of the string is not `0`, or a `0` byte occurs anywhere else in `bytes`, an error is returned instead.
119+
120+
r[core.ffi.cstr.from_bytes_with_nul.return]
121+
If there exists some index `i` of type `usize`, such that `i < bytes.len()` and `bytes[i] == 0`, then:
122+
* If `i + 1 == bytes.len()`, returns a `CStr` borrowed from `bytes` starting from the first index,
123+
and such that `CStr::count_bytes` returns `i`,
124+
* Otherwise, or if no such index exists, returns a `FromBytesWithNullError` that indicates the failure.
125+
126+
## C-compatible primitive types
127+
128+
r[core.ffi.c-primitives]
129+
130+
r[core.ffi.c-primitives.def]
131+
<!--ignore: incomplete code fragment, showing a synopsis as a function def -->
132+
```rust
133+
pub type c_char = /* see below */;
134+
pub type c_double = /* see below */;
135+
pub type c_float = /* see below */;
136+
pub type c_int = /* see below */;
137+
pub type c_long = /*see below*/;
138+
pub type c_longlong = /*see below*/;
139+
pub type c_schar = i8;
140+
pub type c_short = /*see below*/;
141+
pub type c_uchar = /*see below*/;
142+
pub type c_ulong = /*see below*/;
143+
pub type c_ulonglong = /*see below*/;
144+
pub type c_ushort = u8;
145+
```
146+
147+
r[core.ffi.c-primitives.intro]
148+
The C-compatible primitive types are type aliases of primitive types, which are ABI compatible with the corresponding type in C on the current target.
149+
150+
r[core.ffi.c-primitives.c_char]
151+
The type alias `c_char` is a target dependent integer type with the same width and signedness as the `char` type in C.
152+
153+
> [!NOTE]
154+
> On every platform that can support Rust, this is either `u8` or `i8`.
155+
156+
r[core.ffi.c-primitives.c_double]
157+
The type alias `c_double` is a target dependent floating-point type with the same range and precision as the `double` type in C.
158+
159+
> [!NOTE]
160+
> On most platforms, this is `f64`.
161+
162+
r[core.ffi.c-primitives.c_float]
163+
The type alias `c_float` is a target dependent floating-point type with the same range and precision as the `float` type in C.
164+
165+
> [!NOTE]
166+
> On most platforms, this is `f32`.
167+
168+
r[core.ffi.c-primitives.c_int]
169+
The type alias `c_int` is a target dependent signed integer type with the same width as the `int` type in C.
170+
The minimum width of this type is 16-bit, and it is at least as wide as the `c_short` alias.
171+
172+
> [!NOTE]
173+
> On most 32 and 64-bit platforms, this is `i32`,
174+
> but it may be `i16` also on a 16-bit platform.
175+
176+
r[core.ffi.c-primitives.c_long]
177+
The type alias `c_long` is a target dependent signed integer type with the same width as the `long` type in C.
178+
The minimum width of this type is 32-bit, and it is at least as wide as the `c_int` alias.
179+
180+
> [!NOTE]
181+
> The minimum width for this type is 32-bit.
182+
> On most 64-bit platforms, this is `i64`.
183+
184+
r[core.ffi.c-primitives.c_longlong]
185+
The type alias `c_longlong` is a target dependent signed integer type with the same width as the `long long` type in C.
186+
The minimum width of this type is 64-bit, and it is at least as wide as the `c_long` alias.
187+
188+
> [!NOTE]
189+
> This is almost always `i64`, but may be wider.
190+
191+
r[core.ffi.c-primitives.c_schar]
192+
The type alias `c_schar` is an alias of the type `i8`.
193+
194+
> [!NOTE]
195+
> On every platform that can support Rust, this is compatible with the C type `signed char`
196+
197+
r[core.ffi.c-primitives.c_short]
198+
The type alias `c_short` is a target depedent signed integer type with the same width as the `short` type in C.
199+
The minimum width of this type is 16-bit.
200+
201+
> [!NOTE]
202+
> This is almost always `i16`.
203+
204+
r[core.ffi.c-primitives.c_uchar]
205+
The type alias `c_uchar` is an alias of the type `u8`.
206+
207+
> [!NOTE]
208+
> On every platform that can support Rust, this is compatible with the C type `unsigned char`
209+
210+
r[core.ffi.c-primitives.unsigned]
211+
The aliases `c_uint`, `c_ulong`, `c_ulonglong`, and `c_ushort` are all aliases of the unsigned counterpart of the same integer type as the corresponding signed alias.
212+
213+
> [!NOTE]
214+
> In particular, an alias `c_u`*`ty`* is `usize` if and only if `c_`*`ty`* is `isize`.
215+
> In all other cases, they will be `uN` and `iN` respectively, where `N` is the appropriate width.
216+
217+
r[core.ffi.c-primitives.traits]
218+
Each type in this section implements the traits `Copy`, `Clone`, `Send`, `Sync`, `Debug`, and `Display`. The integer type aliases implement `LowerHex`, `UpperHex`, `Octal`, and `Binary`. `c_float` and `c_double` implement `LowerExp` and `UpperExp`.
219+
220+
## c_void
221+
222+
r[core.ffi.c_void]
223+
224+
r[core.ffi.c_void.def]
225+
<!--ignore: incomplete code fragment, showing a synopsis as a function def -->
226+
```rust,ignore
227+
#[non_exhaustive]
228+
pub enum c_void{}
229+
```
230+
231+
r[core.ffi.c_void.intro]
232+
The `c_void` type is an enum type such that `*const c_void` is compatible with the C type `const void*`.
233+
234+
> [!NOTE]
235+
> This is only true behind an indirection - `c_void` itself is not compatible with a function returning `void`.
236+
237+
r[core.ffi.c_void.variants]
238+
The `c_void` type cannot be constructed, and it cannot be exhaustively matched.
239+
240+
> [!NOTE]
241+
> The `c_void` type acts as though it has no variants. but cannot be matched like an empty enum.
242+
243+
r[core.ffi.c_void.traits]
244+
The `c_void` type implements the `Send`, `Sync`, and `Debug` traits.

src/stdlib.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Standard Library

0 commit comments

Comments
 (0)