1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright 2024-2026 Gabriel Bjørnager Jensen.
//
// SPDX: MIT OR Apache-2.0
//! The [`Outlay`] trait.
mod test;
use core::alloc::{Layout, LayoutError};
use core::fmt::Debug;
use core::hash::Hash;
use core::num::NonZero;
use core::panic::UnwindSafe;
use core::ptr::{self, dangling, dangling_mut, NonNull};
use oct::Immutable;
#[cfg(feature = "bstr")]
use core::bstr::ByteStr;
/// Denotes an outlayable type.
///
/// Note that this trait need to be implemented
/// manually for <code>![Sized]</code> types.
///
/// # Safety
///
/// See specific, associated items.
pub unsafe trait Outlay {
/// The metadata type used by pointers to this type.
///
/// [`Pointee::Metadata`]: core::ptr::Pointee::Metadata
type Metadata:
Debug
+ Copy
+ Hash
+ Immutable
+ Ord
+ Send
+ Sync
+ Unpin
+ UnwindSafe;
/// Classifies the size of an octet representation.
///
/// This function determines metadata that could be
/// used to point to a `Self` object of the provided
/// size.
///
/// If no such metadatum exists, or is disputable
/// (e.g. if multiple metadata would yield the same
/// object size,) this function must return
/// [`None`]. If the implementing type cannot have
/// metadata determined from an object size, this
/// function must always return `None`. As a rule of
/// thumb:
///
/// * Sized types accept any sufficiently-large
/// buffer size.
/// * DSTs should only accept *exact* buffer sizes,
/// except when multiple metadata yield the same.
///
/// The size of an object with the returned
/// metadata may be less than this here provided
/// size, but never larger than it.
#[must_use]
fn classify_size(size: usize) -> Option<Self::Metadata>;
/// Constructs metadata for an allocation with
/// specific metadata.
///
/// The returned layout can be used to describe a
/// `Self` allocation, based on the provided
/// metadata. The size denoted by the layout may be
/// zero, so extra care must be taken when using
/// some allocators.
///
/// # Errors
///
/// This function must return an [`Err`] instance if
/// the size of the resulting layout is
/// unrepresentable by [`isize`].
fn layout_for_metadata(metadata: Self::Metadata) -> Result<Layout, LayoutError>;
/// Constructs a raw, constant pointer from raw
/// metadata.
///
/// # Safety
///
/// The returned pointer inherits the provenance of
/// the provided one. If the destination denotes an
/// object where the provided metadata is
/// appropriate, the returned pointer is
/// dereferencable.
#[must_use]
fn ptr_from_raw_parts(data: *const u8, metadata: Self::Metadata) -> *const Self;
/// Constructs a raw, mutable pointer from raw
/// parts.
///
/// # Safety
///
/// The returned pointer inherits the provenance of
/// the provided one. If the destination denotes an
/// object where the provided metadata is
/// appropriate, the returned pointer is
/// dereferencable.
#[must_use]
fn ptr_from_raw_parts_mut(data: *mut u8, metadata: Self::Metadata) -> *mut Self;
/// Constructs a new, dangling, constant pointer
/// with specific metadata.
///
/// The returned pointer is guaranteed to be
/// aligned and non-null.
#[must_use]
fn ptr_dangling_with_metadata(metadata: Self::Metadata) -> *const Self;
/// Constructs a new, dangling, mutable pointer with
/// specific metadata.
///
/// The returned pointer is guaranteed to be
/// aligned and non-null.
#[must_use]
fn ptr_dangling_with_metadata_mut(metadata: Self::Metadata) -> *mut Self;
/// Constructs a new, dangling, non-null pointer
/// with specific metadata.
#[inline]
#[must_use]
fn non_null_dangling_with_metadata(metadata: Self::Metadata) -> NonNull<Self> {
let p = Self::ptr_dangling_with_metadata_mut(metadata);
// SAFETY: `ptr_dangling_with_metadata_mut` return-
// ing a non-null pointer is a safety requirement
// for `Outlay`.
unsafe { NonNull::new_unchecked(p) }
}
}
unsafe impl<T> Outlay for T {
// NOTE: As per the docs for `core::ptr::Pointee`:
//
// > For statically-sized types (that implement the
// > `Sized` traits) as well as for `extern` types,
// > pointers are said to be “thin”: metadata is
// > zero-sized and its type is `()`.
type Metadata = ();
#[inline]
fn classify_size(size: usize) -> Option<Self::Metadata> {
// There is only one, possible metadatum, so we
// just test that the size is sufficient.
//
// NOTE: This implementation still accepts sizes
// larger than `isize::MAX` as those will still be
// rounded down when the metadata is applied. In no
// case will this downrounded size exceed `isize::MAX`.
if size >= size_of::<Self>() {
Some(())
} else {
None
}
}
#[inline]
fn layout_for_metadata((): Self::Metadata) -> Result<Layout, LayoutError> {
Ok(Layout::new::<T>())
}
#[inline(always)]
fn ptr_from_raw_parts(data: *const u8, _meta: Self::Metadata) -> *const Self {
data.cast::<Self>()
}
#[inline(always)]
fn ptr_from_raw_parts_mut(data: *mut u8, _meta: Self::Metadata) -> *mut Self {
data.cast::<Self>()
}
#[inline(always)]
fn ptr_dangling_with_metadata((): Self::Metadata) -> *const Self {
dangling::<Self>()
}
#[inline(always)]
fn ptr_dangling_with_metadata_mut((): Self::Metadata) -> *mut Self {
dangling_mut::<Self>()
}
}
#[cfg(feature = "bstr")]
unsafe impl Outlay for ByteStr {
type Metadata = <[u8] as Outlay>::Metadata;
#[inline(always)]
fn classify_size(size: usize) -> Option<Self::Metadata> {
// NOTE: Same rules as for `<[u8]>`.
<[u8]>::classify_size(size)
}
#[inline(always)]
fn layout_for_metadata(metadata: Self::Metadata) -> Result<Layout, LayoutError> {
<[u8]>::layout_for_metadata(metadata)
}
#[inline(always)]
fn ptr_from_raw_parts(data: *const u8, metadata: Self::Metadata) -> *const Self {
// NOTE: `<*const T>::cast` requires `T: Sized`.
<[u8]>::ptr_from_raw_parts(data, metadata) as *const Self
}
#[inline(always)]
fn ptr_from_raw_parts_mut(data: *mut u8, metadata: Self::Metadata) -> *mut Self {
// NOTE: `<*mut T>::cast` requires `T: Sized`.
<[u8]>::ptr_from_raw_parts_mut(data, metadata) as *mut Self
}
#[inline(always)]
fn ptr_dangling_with_metadata(metadata: Self::Metadata) -> *const Self {
// NOTE: `<*mut T>::cast` requires `T: Sized`.
<[u8]>::ptr_dangling_with_metadata(metadata) as *const Self
}
#[inline(always)]
fn ptr_dangling_with_metadata_mut(metadata: Self::Metadata) -> *mut Self {
// NOTE: `<*mut T>::cast` requires `T: Sized`.
<[u8]>::ptr_dangling_with_metadata_mut(metadata) as *mut Self
}
}
unsafe impl<T> Outlay for [T] {
type Metadata = usize;
#[inline]
fn classify_size(size: usize) -> Option<Self::Metadata> {
// The size function for ZST slices is not symmet-
// ric, so we can't pick a definitive length.
let element_size = NonZero::new(size_of::<T>())?;
// No Rust object can have a size larger than
// `isize::MAX`; thus we don't enforce that here.
// Note that this computation can't panic due to
// the right-hand side being `NonZero`.
if size % element_size == 0 {
Some(size / element_size)
} else {
None
}
}
#[inline(always)]
fn layout_for_metadata(metadata: Self::Metadata) -> Result<Layout, LayoutError> {
Layout::array::<T>(metadata)
}
#[inline]
fn ptr_from_raw_parts(data: *const u8, metadata: Self::Metadata) -> *const Self {
let data = data.cast::<T>();
ptr::slice_from_raw_parts(data, metadata)
}
#[inline]
fn ptr_from_raw_parts_mut(data: *mut u8, metadata: Self::Metadata) -> *mut Self {
let data = data.cast::<T>();
ptr::slice_from_raw_parts_mut(data, metadata)
}
#[inline]
fn ptr_dangling_with_metadata(metadata: Self::Metadata) -> *const Self {
let data = dangling::<T>();
ptr::slice_from_raw_parts(data, metadata)
}
#[inline]
fn ptr_dangling_with_metadata_mut(metadata: Self::Metadata) -> *mut Self {
let data = dangling_mut::<T>();
ptr::slice_from_raw_parts_mut(data, metadata)
}
}
unsafe impl Outlay for str {
type Metadata = <[u8] as Outlay>::Metadata;
#[inline(always)]
fn classify_size(size: usize) -> Option<Self::Metadata> {
// NOTE: Same rules as for `<[u8]>`.
<[u8]>::classify_size(size)
}
#[inline(always)]
fn layout_for_metadata(metadata: Self::Metadata) -> Result<Layout, LayoutError> {
<[u8]>::layout_for_metadata(metadata)
}
#[inline(always)]
fn ptr_from_raw_parts(data: *const u8, metadata: Self::Metadata) -> *const Self {
// NOTE: `<*const T>::cast` requires `T: Sized`.
<[u8]>::ptr_from_raw_parts(data, metadata) as *const Self
}
#[inline(always)]
fn ptr_from_raw_parts_mut(data: *mut u8, metadata: Self::Metadata) -> *mut Self {
// NOTE: `<*mut T>::cast` requires `T: Sized`.
<[u8]>::ptr_from_raw_parts_mut(data, metadata) as *mut Self
}
#[inline(always)]
fn ptr_dangling_with_metadata(metadata: Self::Metadata) -> *const Self {
// NOTE: `<*mut T>::cast` requires `T: Sized`.
<[u8]>::ptr_dangling_with_metadata(metadata) as *const Self
}
#[inline(always)]
fn ptr_dangling_with_metadata_mut(metadata: Self::Metadata) -> *mut Self {
// NOTE: `<*mut T>::cast` requires `T: Sized`.
<[u8]>::ptr_dangling_with_metadata_mut(metadata) as *mut Self
}
}