/rust/registry/src/index.crates.io-1949cf8c6b5b557f/zerocopy-0.8.63/src/wrappers.rs
Line | Count | Source |
1 | | // SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0 OR MIT |
2 | | // |
3 | | // Copyright 2023 The Fuchsia Authors |
4 | | // |
5 | | // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 |
6 | | // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT |
7 | | // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. |
8 | | // This file may not be copied, modified, or distributed except according to |
9 | | // those terms. |
10 | | |
11 | | use core::{borrow::Borrow, fmt, hash::Hash}; |
12 | | |
13 | | use super::*; |
14 | | use crate::pointer::{invariant::Safe, SizeEq, TransmuteFrom}; |
15 | | |
16 | | /// A type with no alignment requirement. |
17 | | /// |
18 | | /// An `Unalign` wraps a `T`, removing any alignment requirement. `Unalign<T>` |
19 | | /// has the same size and bit validity as `T`, but not necessarily the same |
20 | | /// alignment [or ABI]. This is useful if a type with an alignment requirement |
21 | | /// needs to be read from a chunk of memory which provides no alignment |
22 | | /// guarantees. |
23 | | /// |
24 | | /// Since `Unalign` has no alignment requirement, the inner `T` may not be |
25 | | /// properly aligned in memory. There are five ways to access the inner `T`: |
26 | | /// - by value, using [`get`] or [`into_inner`] |
27 | | /// - by reference inside of a callback, using [`update`] |
28 | | /// - fallibly by reference, using [`try_deref`] or [`try_deref_mut`]; these can |
29 | | /// fail if the `Unalign` does not satisfy `T`'s alignment requirement at |
30 | | /// runtime |
31 | | /// - unsafely by reference, using [`deref_unchecked`] or |
32 | | /// [`deref_mut_unchecked`]; it is the caller's responsibility to ensure that |
33 | | /// the `Unalign` satisfies `T`'s alignment requirement |
34 | | /// - (where `T: Unaligned`) infallibly by reference, using [`Deref::deref`] or |
35 | | /// [`DerefMut::deref_mut`] |
36 | | /// |
37 | | /// [or ABI]: https://github.com/google/zerocopy/issues/164 |
38 | | /// [`get`]: Unalign::get |
39 | | /// [`into_inner`]: Unalign::into_inner |
40 | | /// [`update`]: Unalign::update |
41 | | /// [`try_deref`]: Unalign::try_deref |
42 | | /// [`try_deref_mut`]: Unalign::try_deref_mut |
43 | | /// [`deref_unchecked`]: Unalign::deref_unchecked |
44 | | /// [`deref_mut_unchecked`]: Unalign::deref_mut_unchecked |
45 | | /// |
46 | | /// # Example |
47 | | /// |
48 | | /// In this example, we need `EthernetFrame` to have no alignment requirement - |
49 | | /// and thus implement [`Unaligned`]. `EtherType` is `#[repr(u16)]` and so |
50 | | /// cannot implement `Unaligned`. We use `Unalign` to relax `EtherType`'s |
51 | | /// alignment requirement so that `EthernetFrame` has no alignment requirement |
52 | | /// and can implement `Unaligned`. |
53 | | /// |
54 | | /// ```rust |
55 | | /// use zerocopy::*; |
56 | | /// # use zerocopy_derive::*; |
57 | | /// # #[derive(FromBytes, KnownLayout, Immutable, Unaligned)] #[repr(C)] struct Mac([u8; 6]); |
58 | | /// |
59 | | /// # #[derive(PartialEq, Copy, Clone, Debug)] |
60 | | /// #[derive(TryFromBytes, KnownLayout, Immutable)] |
61 | | /// #[repr(u16)] |
62 | | /// enum EtherType { |
63 | | /// Ipv4 = 0x0800u16.to_be(), |
64 | | /// Arp = 0x0806u16.to_be(), |
65 | | /// Ipv6 = 0x86DDu16.to_be(), |
66 | | /// # /* |
67 | | /// ... |
68 | | /// # */ |
69 | | /// } |
70 | | /// |
71 | | /// #[derive(TryFromBytes, KnownLayout, Immutable, Unaligned)] |
72 | | /// #[repr(C)] |
73 | | /// struct EthernetFrame { |
74 | | /// src: Mac, |
75 | | /// dst: Mac, |
76 | | /// ethertype: Unalign<EtherType>, |
77 | | /// payload: [u8], |
78 | | /// } |
79 | | /// |
80 | | /// let bytes = &[ |
81 | | /// # 0, 1, 2, 3, 4, 5, |
82 | | /// # 6, 7, 8, 9, 10, 11, |
83 | | /// # /* |
84 | | /// ... |
85 | | /// # */ |
86 | | /// 0x86, 0xDD, // EtherType |
87 | | /// 0xDE, 0xAD, 0xBE, 0xEF // Payload |
88 | | /// ][..]; |
89 | | /// |
90 | | /// // PANICS: Guaranteed not to panic because `bytes` is of the right |
91 | | /// // length, has the right contents, and `EthernetFrame` has no |
92 | | /// // alignment requirement. |
93 | | /// let packet = EthernetFrame::try_ref_from_bytes(&bytes).unwrap(); |
94 | | /// |
95 | | /// assert_eq!(packet.ethertype.get(), EtherType::Ipv6); |
96 | | /// assert_eq!(packet.payload, [0xDE, 0xAD, 0xBE, 0xEF]); |
97 | | /// ``` |
98 | | /// |
99 | | /// # Safety |
100 | | /// |
101 | | /// `Unalign<T>` is guaranteed to have the same size and bit validity as `T`, |
102 | | /// and to have [`UnsafeCell`]s covering the same byte ranges as `T`. |
103 | | /// `Unalign<T>` is guaranteed to have alignment 1. |
104 | | // NOTE: This type is sound to use with types that need to be dropped. The |
105 | | // reason is that the compiler-generated drop code automatically moves all |
106 | | // values to aligned memory slots before dropping them in-place. This is not |
107 | | // well-documented, but it's hinted at in places like [1] and [2]. However, this |
108 | | // also means that `T` must be `Sized`; unless something changes, we can never |
109 | | // support unsized `T`. [3] |
110 | | // |
111 | | // [1] https://github.com/rust-lang/rust/issues/54148#issuecomment-420529646 |
112 | | // [2] https://github.com/google/zerocopy/pull/126#discussion_r1018512323 |
113 | | // [3] https://github.com/google/zerocopy/issues/209 |
114 | | #[allow(missing_debug_implementations)] |
115 | | #[derive(Default, Copy)] |
116 | | #[cfg_attr(any(feature = "derive", test), derive(Immutable, FromBytes, IntoBytes, Unaligned))] |
117 | | #[repr(C, packed)] |
118 | | pub struct Unalign<T>(T); |
119 | | |
120 | | // We do not use `derive(KnownLayout)` on `Unalign`, because the derive is not |
121 | | // smart enough to realize that `Unalign<T>` is always sized and thus emits a |
122 | | // `KnownLayout` impl bounded on `T: KnownLayout.` This is overly restrictive. |
123 | | impl_known_layout!(T => Unalign<T>); |
124 | | |
125 | | // FIXME(https://github.com/rust-lang/rust-clippy/issues/16087): Move these |
126 | | // attributes below the comment once this Clippy bug is fixed. |
127 | | #[cfg_attr( |
128 | | all(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS, any(feature = "derive", test)), |
129 | | expect(unused_unsafe) |
130 | | )] |
131 | | #[cfg_attr( |
132 | | all( |
133 | | not(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS), |
134 | | any(feature = "derive", test) |
135 | | ), |
136 | | allow(unused_unsafe) |
137 | | )] |
138 | | // SAFETY: |
139 | | // - `Unalign<T>` promises to have alignment 1, and so we don't require that `T: |
140 | | // Unaligned`. |
141 | | // - `Unalign<T>` has the same bit validity as `T`, and so it is `FromZeros`, |
142 | | // `FromBytes`, or `IntoBytes` exactly when `T` is as well. |
143 | | // - `Immutable`: `Unalign<T>` has the same fields as `T`, so it permits interior |
144 | | // mutation exactly when `T` does. |
145 | | // - `TryFromBytes`: `Unalign<T>` has the same the same bit validity as `T`, so |
146 | | // `T::is_safe` is a sound implementation of `is_safe`. |
147 | | // |
148 | | #[allow(clippy::multiple_unsafe_ops_per_block)] |
149 | | const _: () = unsafe { |
150 | | impl_or_verify!(T => Unaligned for Unalign<T>); |
151 | | impl_or_verify!(T: Immutable => Immutable for Unalign<T>); |
152 | | impl_or_verify!( |
153 | | T: TryFromBytes => TryFromBytes for Unalign<T>; |
154 | | |c| T::is_safe(c.transmute::<_, _, BecauseImmutable>()) |
155 | | ); |
156 | | impl_or_verify!(T: FromZeros => FromZeros for Unalign<T>); |
157 | | impl_or_verify!(T: FromBytes => FromBytes for Unalign<T>); |
158 | | impl_or_verify!(T: IntoBytes => IntoBytes for Unalign<T>); |
159 | | }; |
160 | | |
161 | | // Note that `Unalign: Clone` only if `T: Copy`. Since the inner `T` may not be |
162 | | // aligned, there's no way to safely call `T::clone`, and so a `T: Clone` bound |
163 | | // is not sufficient to implement `Clone` for `Unalign`. |
164 | | impl<T: Copy> Clone for Unalign<T> { |
165 | | #[inline(always)] |
166 | | fn clone(&self) -> Unalign<T> { |
167 | | *self |
168 | | } |
169 | | } |
170 | | |
171 | | impl<T> Unalign<T> { |
172 | | /// Constructs a new `Unalign`. |
173 | | #[inline(always)] |
174 | | pub const fn new(val: T) -> Unalign<T> { |
175 | | Unalign(val) |
176 | | } |
177 | | |
178 | | /// Consumes `self`, returning the inner `T`. |
179 | | #[inline(always)] |
180 | 36.7k | pub const fn into_inner(self) -> T { |
181 | | // SAFETY: Since `Unalign` is `#[repr(C, packed)]`, it has the same size |
182 | | // and bit validity as `T`. |
183 | | // |
184 | | // We do this instead of just destructuring in order to prevent |
185 | | // `Unalign`'s `Drop::drop` from being run, since dropping is not |
186 | | // supported in `const fn`s. |
187 | | // |
188 | | // FIXME(https://github.com/rust-lang/rust/issues/73255): Destructure |
189 | | // instead of using unsafe. |
190 | 36.7k | unsafe { crate::util::transmute_unchecked(self) } |
191 | 36.7k | } <zerocopy::wrappers::Unalign<anise::naif::daf::summary_record::SummaryRecord>>::into_inner Line | Count | Source | 180 | 13.3k | pub const fn into_inner(self) -> T { | 181 | | // SAFETY: Since `Unalign` is `#[repr(C, packed)]`, it has the same size | 182 | | // and bit validity as `T`. | 183 | | // | 184 | | // We do this instead of just destructuring in order to prevent | 185 | | // `Unalign`'s `Drop::drop` from being run, since dropping is not | 186 | | // supported in `const fn`s. | 187 | | // | 188 | | // FIXME(https://github.com/rust-lang/rust/issues/73255): Destructure | 189 | | // instead of using unsafe. | 190 | 13.3k | unsafe { crate::util::transmute_unchecked(self) } | 191 | 13.3k | } |
<zerocopy::wrappers::Unalign<anise::naif::daf::name_record::NameRecord>>::into_inner Line | Count | Source | 180 | 8.62k | pub const fn into_inner(self) -> T { | 181 | | // SAFETY: Since `Unalign` is `#[repr(C, packed)]`, it has the same size | 182 | | // and bit validity as `T`. | 183 | | // | 184 | | // We do this instead of just destructuring in order to prevent | 185 | | // `Unalign`'s `Drop::drop` from being run, since dropping is not | 186 | | // supported in `const fn`s. | 187 | | // | 188 | | // FIXME(https://github.com/rust-lang/rust/issues/73255): Destructure | 189 | | // instead of using unsafe. | 190 | 8.62k | unsafe { crate::util::transmute_unchecked(self) } | 191 | 8.62k | } |
<zerocopy::wrappers::Unalign<anise::naif::daf::file_record::FileRecord>>::into_inner Line | Count | Source | 180 | 14.7k | pub const fn into_inner(self) -> T { | 181 | | // SAFETY: Since `Unalign` is `#[repr(C, packed)]`, it has the same size | 182 | | // and bit validity as `T`. | 183 | | // | 184 | | // We do this instead of just destructuring in order to prevent | 185 | | // `Unalign`'s `Drop::drop` from being run, since dropping is not | 186 | | // supported in `const fn`s. | 187 | | // | 188 | | // FIXME(https://github.com/rust-lang/rust/issues/73255): Destructure | 189 | | // instead of using unsafe. | 190 | 14.7k | unsafe { crate::util::transmute_unchecked(self) } | 191 | 14.7k | } |
|
192 | | |
193 | | /// Attempts to return a reference to the wrapped `T`, failing if `self` is |
194 | | /// not properly aligned. |
195 | | /// |
196 | | /// If `self` does not satisfy `align_of::<T>()`, then `try_deref` returns |
197 | | /// `Err`. |
198 | | /// |
199 | | /// If `T: Unaligned`, then `Unalign<T>` implements [`Deref`], and callers |
200 | | /// may prefer [`Deref::deref`], which is infallible. |
201 | | #[inline(always)] |
202 | | pub fn try_deref(&self) -> Result<&T, AlignmentError<&Self, T>> { |
203 | | let inner = Ptr::from_ref(self).transmute(); |
204 | | match inner.try_into_aligned() { |
205 | | Ok(aligned) => Ok(aligned.as_ref()), |
206 | | Err(err) => Err(err.map_src( |
207 | | #[inline(always)] |
208 | | |src| src.into_unalign().as_ref(), |
209 | | )), |
210 | | } |
211 | | } |
212 | | |
213 | | /// Attempts to return a mutable reference to the wrapped `T`, failing if |
214 | | /// `self` is not properly aligned. |
215 | | /// |
216 | | /// If `self` does not satisfy `align_of::<T>()`, then `try_deref` returns |
217 | | /// `Err`. |
218 | | /// |
219 | | /// If `T: Unaligned`, then `Unalign<T>` implements [`DerefMut`], and |
220 | | /// callers may prefer [`DerefMut::deref_mut`], which is infallible. |
221 | | #[inline(always)] |
222 | | pub fn try_deref_mut(&mut self) -> Result<&mut T, AlignmentError<&mut Self, T>> { |
223 | | let inner = Ptr::from_mut(self).transmute::<_, _, (_, (_, _))>(); |
224 | | match inner.try_into_aligned() { |
225 | | Ok(aligned) => Ok(aligned.as_mut()), |
226 | | Err(err) => Err(err.map_src(|src| src.into_unalign().as_mut())), |
227 | | } |
228 | | } |
229 | | |
230 | | /// Returns a reference to the wrapped `T` without checking alignment. |
231 | | /// |
232 | | /// If `T: Unaligned`, then `Unalign<T>` implements[ `Deref`], and callers |
233 | | /// may prefer [`Deref::deref`], which is safe. |
234 | | /// |
235 | | /// # Safety |
236 | | /// |
237 | | /// The caller must guarantee that `self` satisfies `align_of::<T>()`. |
238 | | #[inline(always)] |
239 | | pub const unsafe fn deref_unchecked(&self) -> &T { |
240 | | // SAFETY: `Unalign<T>` is `repr(transparent)`, so there is a valid `T` |
241 | | // at the same memory location as `self`. It has no alignment guarantee, |
242 | | // but the caller has promised that `self` is properly aligned, so we |
243 | | // know that it is sound to create a reference to `T` at this memory |
244 | | // location. |
245 | | // |
246 | | // We use `mem::transmute` instead of `&*self.get_ptr()` because |
247 | | // dereferencing pointers is not stable in `const` on our current MSRV |
248 | | // (1.56 as of this writing). |
249 | | unsafe { mem::transmute(self) } |
250 | | } |
251 | | |
252 | | /// Returns a mutable reference to the wrapped `T` without checking |
253 | | /// alignment. |
254 | | /// |
255 | | /// If `T: Unaligned`, then `Unalign<T>` implements[ `DerefMut`], and |
256 | | /// callers may prefer [`DerefMut::deref_mut`], which is safe. |
257 | | /// |
258 | | /// # Safety |
259 | | /// |
260 | | /// The caller must guarantee that `self` satisfies `align_of::<T>()`. |
261 | | #[inline(always)] |
262 | | pub unsafe fn deref_mut_unchecked(&mut self) -> &mut T { |
263 | | // SAFETY: `self.get_mut_ptr()` returns a raw pointer to a valid `T` at |
264 | | // the same memory location as `self`. It has no alignment guarantee, |
265 | | // but the caller has promised that `self` is properly aligned, so we |
266 | | // know that the pointer itself is aligned, and thus that it is sound to |
267 | | // create a reference to a `T` at this memory location. |
268 | | unsafe { &mut *self.get_mut_ptr() } |
269 | | } |
270 | | |
271 | | /// Gets an unaligned raw pointer to the inner `T`. |
272 | | /// |
273 | | /// # Safety |
274 | | /// |
275 | | /// The returned raw pointer is not necessarily aligned to |
276 | | /// `align_of::<T>()`. Most functions which operate on raw pointers require |
277 | | /// those pointers to be aligned, so calling those functions with the result |
278 | | /// of `get_ptr` will result in undefined behavior if alignment is not |
279 | | /// guaranteed using some out-of-band mechanism. In general, the only |
280 | | /// functions which are safe to call with this pointer are those which are |
281 | | /// explicitly documented as being sound to use with an unaligned pointer, |
282 | | /// such as [`read_unaligned`]. |
283 | | /// |
284 | | /// Even if the caller is permitted to mutate `self` (e.g. they have |
285 | | /// ownership or a mutable borrow), it is not guaranteed to be sound to |
286 | | /// write through the returned pointer. If writing is required, prefer |
287 | | /// [`get_mut_ptr`] instead. |
288 | | /// |
289 | | /// [`read_unaligned`]: core::ptr::read_unaligned |
290 | | /// [`get_mut_ptr`]: Unalign::get_mut_ptr |
291 | | #[inline(always)] |
292 | | pub const fn get_ptr(&self) -> *const T { |
293 | | ptr::addr_of!(self.0) |
294 | | } |
295 | | |
296 | | /// Gets an unaligned mutable raw pointer to the inner `T`. |
297 | | /// |
298 | | /// # Safety |
299 | | /// |
300 | | /// The returned raw pointer is not necessarily aligned to |
301 | | /// `align_of::<T>()`. Most functions which operate on raw pointers require |
302 | | /// those pointers to be aligned, so calling those functions with the result |
303 | | /// of `get_ptr` will result in undefined behavior if alignment is not |
304 | | /// guaranteed using some out-of-band mechanism. In general, the only |
305 | | /// functions which are safe to call with this pointer are those which are |
306 | | /// explicitly documented as being sound to use with an unaligned pointer, |
307 | | /// such as [`read_unaligned`]. |
308 | | /// |
309 | | /// [`read_unaligned`]: core::ptr::read_unaligned |
310 | | // FIXME(https://github.com/rust-lang/rust/issues/57349): Make this `const`. |
311 | | #[inline(always)] |
312 | | pub fn get_mut_ptr(&mut self) -> *mut T { |
313 | | ptr::addr_of_mut!(self.0) |
314 | | } |
315 | | |
316 | | /// Sets the inner `T`, dropping the previous value. |
317 | | // FIXME(https://github.com/rust-lang/rust/issues/57349): Make this `const`. |
318 | | #[inline(always)] |
319 | | pub fn set(&mut self, t: T) { |
320 | | *self = Unalign::new(t); |
321 | | } |
322 | | |
323 | | /// Updates the inner `T` by calling a function on it. |
324 | | /// |
325 | | /// If [`T: Unaligned`], then `Unalign<T>` implements [`DerefMut`], and that |
326 | | /// impl should be preferred over this method when performing updates, as it |
327 | | /// will usually be faster and more ergonomic. |
328 | | /// |
329 | | /// For large types, this method may be expensive, as it requires copying |
330 | | /// `2 * size_of::<T>()` bytes. \[1\] |
331 | | /// |
332 | | /// \[1\] Since the inner `T` may not be aligned, it would not be sound to |
333 | | /// invoke `f` on it directly. Instead, `update` moves it into a |
334 | | /// properly-aligned location in the local stack frame, calls `f` on it, and |
335 | | /// then moves it back to its original location in `self`. |
336 | | /// |
337 | | /// [`T: Unaligned`]: Unaligned |
338 | | #[inline] |
339 | | pub fn update<O, F: FnOnce(&mut T) -> O>(&mut self, f: F) -> O { |
340 | | if mem::align_of::<T>() == 1 { |
341 | | // While we advise callers to use `DerefMut` when `T: Unaligned`, |
342 | | // not all callers will be able to guarantee `T: Unaligned` in all |
343 | | // cases. In particular, callers who are themselves providing an API |
344 | | // which is generic over `T` may sometimes be called by *their* |
345 | | // callers with `T` such that `align_of::<T>() == 1`, but cannot |
346 | | // guarantee this in the general case. Thus, this optimization may |
347 | | // sometimes be helpful. |
348 | | |
349 | | // SAFETY: Since `T`'s alignment is 1, `self` satisfies its |
350 | | // alignment by definition. |
351 | | let t = unsafe { self.deref_mut_unchecked() }; |
352 | | return f(t); |
353 | | } |
354 | | |
355 | | // On drop, this moves `copy` out of itself and uses `ptr::write` to |
356 | | // overwrite `slf`. |
357 | | struct WriteBackOnDrop<T> { |
358 | | copy: ManuallyDrop<T>, |
359 | | slf: *mut Unalign<T>, |
360 | | } |
361 | | |
362 | | impl<T> Drop for WriteBackOnDrop<T> { |
363 | | fn drop(&mut self) { |
364 | | // SAFETY: We never use `copy` again as required by |
365 | | // `ManuallyDrop::take`. |
366 | | let copy = unsafe { ManuallyDrop::take(&mut self.copy) }; |
367 | | // SAFETY: `slf` is the raw pointer value of `self`. We know it |
368 | | // is valid for writes and properly aligned because `self` is a |
369 | | // mutable reference, which guarantees both of these properties. |
370 | | unsafe { ptr::write(self.slf, Unalign::new(copy)) }; |
371 | | } |
372 | | } |
373 | | |
374 | | // SAFETY: We know that `self` is valid for reads, properly aligned, and |
375 | | // points to an initialized `Unalign<T>` because it is a mutable |
376 | | // reference, which guarantees all of these properties. |
377 | | // |
378 | | // Since `T: !Copy`, it would be unsound in the general case to allow |
379 | | // both the original `Unalign<T>` and the copy to be used by safe code. |
380 | | // We guarantee that the copy is used to overwrite the original in the |
381 | | // `Drop::drop` impl of `WriteBackOnDrop`. So long as this `drop` is |
382 | | // called before any other safe code executes, soundness is upheld. |
383 | | // While this method can terminate in two ways (by returning normally or |
384 | | // by unwinding due to a panic in `f`), in both cases, `write_back` is |
385 | | // dropped - and its `drop` called - before any other safe code can |
386 | | // execute. |
387 | | let copy = unsafe { ptr::read(self) }.into_inner(); |
388 | | let mut write_back = WriteBackOnDrop { copy: ManuallyDrop::new(copy), slf: self }; |
389 | | |
390 | | let ret = f(&mut write_back.copy); |
391 | | |
392 | | drop(write_back); |
393 | | ret |
394 | | } |
395 | | } |
396 | | |
397 | | impl<T: Copy> Unalign<T> { |
398 | | /// Gets a copy of the inner `T`. |
399 | | // FIXME(https://github.com/rust-lang/rust/issues/57349): Make this `const`. |
400 | | #[inline(always)] |
401 | | pub fn get(&self) -> T { |
402 | | let Unalign(val) = *self; |
403 | | val |
404 | | } |
405 | | } |
406 | | |
407 | | impl<T: Unaligned> Deref for Unalign<T> { |
408 | | type Target = T; |
409 | | |
410 | | #[inline(always)] |
411 | | fn deref(&self) -> &T { |
412 | | Ptr::from_ref(self).transmute().bikeshed_recall_aligned().as_ref() |
413 | | } |
414 | | } |
415 | | |
416 | | impl<T: Unaligned> DerefMut for Unalign<T> { |
417 | | #[inline(always)] |
418 | | fn deref_mut(&mut self) -> &mut T { |
419 | | Ptr::from_mut(self).transmute::<_, _, (_, (_, _))>().bikeshed_recall_aligned().as_mut() |
420 | | } |
421 | | } |
422 | | |
423 | | impl<T: Unaligned + PartialOrd> PartialOrd<Unalign<T>> for Unalign<T> { |
424 | | #[inline(always)] |
425 | | fn partial_cmp(&self, other: &Unalign<T>) -> Option<Ordering> { |
426 | | PartialOrd::partial_cmp(self.deref(), other.deref()) |
427 | | } |
428 | | } |
429 | | |
430 | | impl<T: Unaligned + Ord> Ord for Unalign<T> { |
431 | | #[inline(always)] |
432 | | fn cmp(&self, other: &Unalign<T>) -> Ordering { |
433 | | Ord::cmp(self.deref(), other.deref()) |
434 | | } |
435 | | } |
436 | | |
437 | | impl<T: Unaligned + PartialEq> PartialEq<Unalign<T>> for Unalign<T> { |
438 | | #[inline(always)] |
439 | | fn eq(&self, other: &Unalign<T>) -> bool { |
440 | | PartialEq::eq(self.deref(), other.deref()) |
441 | | } |
442 | | } |
443 | | |
444 | | impl<T: Unaligned + Eq> Eq for Unalign<T> {} |
445 | | |
446 | | impl<T: Unaligned + Hash> Hash for Unalign<T> { |
447 | | #[inline(always)] |
448 | | fn hash<H>(&self, state: &mut H) |
449 | | where |
450 | | H: Hasher, |
451 | | { |
452 | | self.deref().hash(state); |
453 | | } |
454 | | } |
455 | | |
456 | | impl<T: Unaligned + Debug> Debug for Unalign<T> { |
457 | | #[inline(always)] |
458 | | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
459 | | Debug::fmt(self.deref(), f) |
460 | | } |
461 | | } |
462 | | |
463 | | impl<T: Unaligned + Display> Display for Unalign<T> { |
464 | | #[inline(always)] |
465 | | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
466 | | Display::fmt(self.deref(), f) |
467 | | } |
468 | | } |
469 | | |
470 | | /// A wrapper type to construct uninitialized instances of `T`. |
471 | | /// |
472 | | /// `MaybeUninit` is identical to the [standard library |
473 | | /// `MaybeUninit`][core-maybe-uninit] type except that it supports unsized |
474 | | /// types. |
475 | | /// |
476 | | /// # Layout |
477 | | /// |
478 | | /// The same layout guarantees and caveats apply to `MaybeUninit<T>` as apply to |
479 | | /// the [standard library `MaybeUninit`][core-maybe-uninit] with one exception: |
480 | | /// for `T: !Sized`, there is no single value for `T`'s size. Instead, for such |
481 | | /// types, the following are guaranteed: |
482 | | /// - Every [valid size][valid-size] for `T` is a valid size for |
483 | | /// `MaybeUninit<T>` and vice versa |
484 | | /// - Given `t: *const T` and `m: *const MaybeUninit<T>` with identical fat |
485 | | /// pointer metadata, `t` and `m` address the same number of bytes (and |
486 | | /// likewise for `*mut`) |
487 | | /// |
488 | | /// [core-maybe-uninit]: core::mem::MaybeUninit |
489 | | /// [valid-size]: crate::KnownLayout#what-is-a-valid-size |
490 | | #[repr(transparent)] |
491 | | #[doc(hidden)] |
492 | | pub struct MaybeUninit<T: ?Sized + KnownLayout>( |
493 | | // SAFETY: `MaybeUninit<T>` has the same size as `T`, because (by invariant |
494 | | // on `T::MaybeUninit`) `T::MaybeUninit` has `T::LAYOUT` identical to `T`, |
495 | | // and because (invariant on `T::LAYOUT`) we can trust that `LAYOUT` |
496 | | // accurately reflects the layout of `T`. By invariant on `T::MaybeUninit`, |
497 | | // it admits uninitialized bytes in all positions. Because `MaybeUninit` is |
498 | | // marked `repr(transparent)`, these properties additionally hold true for |
499 | | // `Self`. |
500 | | T::MaybeUninit, |
501 | | ); |
502 | | |
503 | | #[doc(hidden)] |
504 | | impl<T: ?Sized + KnownLayout> MaybeUninit<T> { |
505 | | /// Constructs a `MaybeUninit<T>` initialized with the given value. |
506 | | #[inline(always)] |
507 | | pub fn new(val: T) -> Self |
508 | | where |
509 | | T: Sized, |
510 | | Self: Sized, |
511 | | { |
512 | | // SAFETY: It is valid to transmute `val` to `MaybeUninit<T>` because it |
513 | | // is both valid to transmute `val` to `T::MaybeUninit`, and it is valid |
514 | | // to transmute from `T::MaybeUninit` to `MaybeUninit<T>`. |
515 | | // |
516 | | // First, it is valid to transmute `val` to `T::MaybeUninit` because, by |
517 | | // invariant on `T::MaybeUninit`: |
518 | | // - For `T: Sized`, `T` and `T::MaybeUninit` have the same size. |
519 | | // - All byte sequences of the correct size are valid values of |
520 | | // `T::MaybeUninit`. |
521 | | // |
522 | | // Second, it is additionally valid to transmute from `T::MaybeUninit` |
523 | | // to `MaybeUninit<T>`, because `MaybeUninit<T>` is a |
524 | | // `repr(transparent)` wrapper around `T::MaybeUninit`. |
525 | | // |
526 | | // These two transmutes are collapsed into one so we don't need to add a |
527 | | // `T::MaybeUninit: Sized` bound to this function's `where` clause. |
528 | | unsafe { crate::util::transmute_unchecked(val) } |
529 | | } |
530 | | |
531 | | /// Constructs an uninitialized `MaybeUninit<T>`. |
532 | | #[must_use] |
533 | | #[inline(always)] |
534 | | pub fn uninit() -> Self |
535 | | where |
536 | | T: Sized, |
537 | | Self: Sized, |
538 | | { |
539 | | let uninit = CoreMaybeUninit::<T>::uninit(); |
540 | | // SAFETY: It is valid to transmute from `CoreMaybeUninit<T>` to |
541 | | // `MaybeUninit<T>` since they both admit uninitialized bytes in all |
542 | | // positions, and they have the same size (i.e., that of `T`). |
543 | | // |
544 | | // `MaybeUninit<T>` has the same size as `T`, because (by invariant on |
545 | | // `T::MaybeUninit`) `T::MaybeUninit` has `T::LAYOUT` identical to `T`, |
546 | | // and because (invariant on `T::LAYOUT`) we can trust that `LAYOUT` |
547 | | // accurately reflects the layout of `T`. |
548 | | // |
549 | | // `CoreMaybeUninit<T>` has the same size as `T` [1] and admits |
550 | | // uninitialized bytes in all positions. |
551 | | // |
552 | | // [1] Per https://doc.rust-lang.org/1.81.0/std/mem/union.MaybeUninit.html#layout-1: |
553 | | // |
554 | | // `MaybeUninit<T>` is guaranteed to have the same size, alignment, |
555 | | // and ABI as `T` |
556 | | unsafe { crate::util::transmute_unchecked(uninit) } |
557 | | } |
558 | | |
559 | | /// Creates a `Box<MaybeUninit<T>>`. |
560 | | /// |
561 | | /// This function is useful for allocating large, uninit values on the heap |
562 | | /// without ever creating a temporary instance of `Self` on the stack. |
563 | | /// |
564 | | /// # Errors |
565 | | /// |
566 | | /// Returns an error on allocation failure. Allocation failure is guaranteed |
567 | | /// never to cause a panic or an abort. |
568 | | #[cfg(feature = "alloc")] |
569 | | #[inline] |
570 | | pub fn new_boxed_uninit(meta: T::PointerMetadata) -> Result<Box<Self>, AllocError> { |
571 | | // SAFETY: `alloc::alloc::alloc_zeroed` is a valid argument of |
572 | | // `new_box`. The referent of the pointer returned by `alloc` (and, |
573 | | // consequently, the `Box` derived from it) is a valid instance of |
574 | | // `Self`, because `Self` is `MaybeUninit` and thus admits arbitrary |
575 | | // (un)initialized bytes. |
576 | | unsafe { crate::util::new_box(meta, alloc::alloc::alloc) } |
577 | | } |
578 | | |
579 | | /// Extracts the value from the `MaybeUninit<T>` container. |
580 | | /// |
581 | | /// # Safety |
582 | | /// |
583 | | /// The caller must ensure that `self` is in an bit-valid state. Depending |
584 | | /// on subsequent use, it may also need to be in a library-valid state. |
585 | | #[inline(always)] |
586 | | pub unsafe fn assume_init(self) -> T |
587 | | where |
588 | | T: Sized, |
589 | | Self: Sized, |
590 | | { |
591 | | // SAFETY: The caller guarantees that `self` is in an bit-valid state. |
592 | | unsafe { crate::util::transmute_unchecked(self) } |
593 | | } |
594 | | } |
595 | | |
596 | | impl<T: ?Sized + KnownLayout> fmt::Debug for MaybeUninit<T> { |
597 | | #[inline] |
598 | | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
599 | | f.pad(core::any::type_name::<Self>()) |
600 | | } |
601 | | } |
602 | | |
603 | | #[allow(unreachable_pub)] // False positive on MSRV |
604 | | #[doc(hidden)] |
605 | | pub use read_only_def::*; |
606 | | mod read_only_def { |
607 | | /// A read-only wrapper. |
608 | | /// |
609 | | /// A `ReadOnly<T>` disables any interior mutability in `T`, ensuring that |
610 | | /// a `&ReadOnly<T>` is genuinely read-only. Thus, `ReadOnly<T>` is |
611 | | /// [`Immutable`] regardless of whether `T` is. |
612 | | /// |
613 | | /// Note that `&mut ReadOnly<T>` still permits mutation – the read-only |
614 | | /// property only applies to shared references. |
615 | | /// |
616 | | /// `ReadOnly<T>` implements [`Copy`] and [`Clone`] when `T: Copy`. Cloning |
617 | | /// copies the value without calling `T::clone`. Trait implementations that |
618 | | /// expose or operate on a shared reference to `T` require `T: Immutable`. |
619 | | /// Constructing a wrapper or accessing its contents through [`AsMut`] |
620 | | /// does not require `T: Immutable`. |
621 | | /// |
622 | | /// [`Immutable`]: crate::Immutable |
623 | | #[derive(Copy)] |
624 | | #[repr(transparent)] |
625 | | pub struct ReadOnly<T: ?Sized> { |
626 | | // INVARIANT: `inner` is never mutated through a `&ReadOnly<T>` |
627 | | // reference. |
628 | | inner: T, |
629 | | } |
630 | | |
631 | | impl<T> ReadOnly<T> { |
632 | | /// Creates a new `ReadOnly`. |
633 | | #[must_use] |
634 | | #[inline(always)] |
635 | | pub const fn new(t: T) -> ReadOnly<T> { |
636 | | ReadOnly { inner: t } |
637 | | } |
638 | | |
639 | | /// Returns the inner value. |
640 | | #[must_use] |
641 | | #[inline(always)] |
642 | | pub fn into_inner(r: ReadOnly<T>) -> T { |
643 | | r.inner |
644 | | } |
645 | | } |
646 | | |
647 | | impl<T: ?Sized> ReadOnly<T> { |
648 | | #[inline(always)] |
649 | | pub(crate) fn as_mut(r: &mut ReadOnly<T>) -> &mut T { |
650 | | // SAFETY: `r: &mut ReadOnly`, so this doesn't violate the invariant |
651 | | // that `inner` is never mutated through a `&ReadOnly<T>` reference. |
652 | | &mut r.inner |
653 | | } |
654 | | |
655 | | /// # Safety |
656 | | /// |
657 | | /// The caller promises not to mutate the referent (i.e., via interior |
658 | | /// mutation). |
659 | | pub(crate) const unsafe fn as_ref_unchecked(r: &ReadOnly<T>) -> &T { |
660 | | // SAFETY: The caller promises not to mutate the referent. |
661 | | &r.inner |
662 | | } |
663 | | } |
664 | | } |
665 | | |
666 | | // SAFETY: `ReadOnly<T>` is a `#[repr(transparent)` wrapper around `T`. |
667 | | const _: () = unsafe { |
668 | | unsafe_impl_known_layout!(T: ?Sized + KnownLayout => #[repr(T)] ReadOnly<T>); |
669 | | }; |
670 | | |
671 | | #[allow(clippy::multiple_unsafe_ops_per_block)] |
672 | | // SAFETY: |
673 | | // - `ReadOnly<T>` has the same alignment as `T`, and so it is `Unaligned` |
674 | | // exactly when `T` is as well. |
675 | | // - `ReadOnly<T>` has the same bit validity as `T`, and so this `is_safe` |
676 | | // implementation is correct, and thus the `TryFromBytes` impl is sound. |
677 | | // - `ReadOnly<T>` has the same bit validity as `T`, and so it is `FromZeros`, |
678 | | // `FromBytes`, and `IntoBytes` exactly when `T` is as well. |
679 | | const _: () = unsafe { |
680 | | unsafe_impl!(T: ?Sized + Unaligned => Unaligned for ReadOnly<T>); |
681 | | unsafe_impl!( |
682 | | T: ?Sized + TryFromBytes => TryFromBytes for ReadOnly<T>; |
683 | | |c| T::is_safe(c.cast::<_, <ReadOnly<T> as SizeEq<ReadOnly<ReadOnly<T>>>>::CastFrom, _>()) |
684 | | ); |
685 | | unsafe_impl!(T: ?Sized + FromZeros => FromZeros for ReadOnly<T>); |
686 | | unsafe_impl!(T: ?Sized + FromBytes => FromBytes for ReadOnly<T>); |
687 | | unsafe_impl!(T: ?Sized + IntoBytes => IntoBytes for ReadOnly<T>); |
688 | | }; |
689 | | |
690 | | // SAFETY: By invariant, `inner` is never mutated through a `&ReadOnly<T>` |
691 | | // reference. |
692 | | const _: () = unsafe { |
693 | | unsafe_impl!(T: ?Sized => Immutable for ReadOnly<T>); |
694 | | }; |
695 | | |
696 | | const _: () = { |
697 | | use crate::pointer::cast::CastExact; |
698 | | |
699 | | // SAFETY: `ReadOnly<T>` has the same layout as `T`. |
700 | | define_cast!(unsafe { pub CastFromReadOnly<T: ?Sized> = ReadOnly<T> => T}); |
701 | | // SAFETY: `ReadOnly<T>` has the same layout as `T`. |
702 | | unsafe impl<T: ?Sized> CastExact<ReadOnly<T>, T> for CastFromReadOnly {} |
703 | | // SAFETY: `ReadOnly<T>` has the same layout as `T`. |
704 | | define_cast!(unsafe { pub CastToReadOnly<T: ?Sized> = T => ReadOnly<T>}); |
705 | | // SAFETY: `ReadOnly<T>` has the same layout as `T`. |
706 | | unsafe impl<T: ?Sized> CastExact<T, ReadOnly<T>> for CastToReadOnly {} |
707 | | |
708 | | impl<T: ?Sized> SizeEq<ReadOnly<T>> for T { |
709 | | type CastFrom = CastFromReadOnly; |
710 | | } |
711 | | |
712 | | impl<T: ?Sized> SizeEq<T> for ReadOnly<T> { |
713 | | type CastFrom = CastToReadOnly; |
714 | | } |
715 | | }; |
716 | | |
717 | | // SAFETY: `ReadOnly<T>` is a `#[repr(transparent)]` wrapper around `T`, and so |
718 | | // it has the same bit validity as `T`. |
719 | | unsafe impl<T: ?Sized> TransmuteFrom<T, Safe, Safe> for ReadOnly<T> {} |
720 | | |
721 | | // SAFETY: `ReadOnly<T>` is a `#[repr(transparent)]` wrapper around `T`, and so |
722 | | // it has the same bit validity as `T`. |
723 | | unsafe impl<T: ?Sized> TransmuteFrom<ReadOnly<T>, Safe, Safe> for T {} |
724 | | |
725 | | impl<T: Default> Default for ReadOnly<T> { |
726 | | #[inline(always)] |
727 | | fn default() -> Self { |
728 | | Self::new(Default::default()) |
729 | | } |
730 | | } |
731 | | |
732 | | // Copying avoids calling `T::clone`, which could mutate through a shared |
733 | | // reference to `T` and violate `ReadOnly`'s invariant. |
734 | | impl<T: Copy> Clone for ReadOnly<T> { |
735 | | #[inline(always)] |
736 | | fn clone(&self) -> Self { |
737 | | *self |
738 | | } |
739 | | } |
740 | | |
741 | | impl<T> From<T> for ReadOnly<T> { |
742 | | #[inline(always)] |
743 | | fn from(t: T) -> Self { |
744 | | Self::new(t) |
745 | | } |
746 | | } |
747 | | |
748 | | impl<'a, T: ?Sized + Immutable> From<&'a T> for &'a ReadOnly<T> { |
749 | | #[inline(always)] |
750 | | fn from(t: &'a T) -> &'a ReadOnly<T> { |
751 | | let ro = Ptr::from_ref(t).transmute::<_, _, (_, _)>(); |
752 | | // SAFETY: `ReadOnly<T>` has the same alignment as `T`, and |
753 | | // `Ptr::from_ref` produces an aligned `Ptr`. |
754 | | let ro = unsafe { ro.assume_alignment() }; |
755 | | ro.as_ref() |
756 | | } |
757 | | } |
758 | | |
759 | | impl<T: ?Sized + Immutable> Deref for ReadOnly<T> { |
760 | | type Target = T; |
761 | | |
762 | | #[inline(always)] |
763 | | fn deref(&self) -> &Self::Target { |
764 | | // SAFETY: By `T: Immutable`, `&T` doesn't permit interior mutation. |
765 | | unsafe { ReadOnly::as_ref_unchecked(self) } |
766 | | } |
767 | | } |
768 | | |
769 | | impl<T: ?Sized + Immutable> DerefMut for ReadOnly<T> { |
770 | | #[inline(always)] |
771 | | fn deref_mut(&mut self) -> &mut Self::Target { |
772 | | ReadOnly::as_mut(self) |
773 | | } |
774 | | } |
775 | | |
776 | | impl<T: ?Sized + Immutable> AsRef<T> for ReadOnly<T> { |
777 | | #[inline(always)] |
778 | | fn as_ref(&self) -> &T { |
779 | | self.deref() |
780 | | } |
781 | | } |
782 | | |
783 | | impl<T: ?Sized + Immutable> Borrow<T> for ReadOnly<T> { |
784 | | #[inline(always)] |
785 | | fn borrow(&self) -> &T { |
786 | | self.deref() |
787 | | } |
788 | | } |
789 | | |
790 | | impl<T: ?Sized> AsMut<T> for ReadOnly<T> { |
791 | | #[inline(always)] |
792 | | fn as_mut(&mut self) -> &mut T { |
793 | | ReadOnly::as_mut(self) |
794 | | } |
795 | | } |
796 | | |
797 | | // Delegate shared access through `Deref`, which requires `T: Immutable` and |
798 | | // thus prevents interior mutation of the wrapped value by these trait methods. |
799 | | impl<T: ?Sized + Immutable + PartialEq> PartialEq for ReadOnly<T> { |
800 | | #[inline(always)] |
801 | | fn eq(&self, other: &Self) -> bool { |
802 | | self.deref().eq(other.deref()) |
803 | | } |
804 | | } |
805 | | |
806 | | impl<T: ?Sized + Immutable + Eq> Eq for ReadOnly<T> {} |
807 | | |
808 | | impl<T: ?Sized + Immutable + PartialOrd> PartialOrd for ReadOnly<T> { |
809 | | #[inline(always)] |
810 | | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
811 | | self.deref().partial_cmp(other.deref()) |
812 | | } |
813 | | } |
814 | | |
815 | | impl<T: ?Sized + Immutable + Ord> Ord for ReadOnly<T> { |
816 | | #[inline(always)] |
817 | | fn cmp(&self, other: &Self) -> Ordering { |
818 | | self.deref().cmp(other.deref()) |
819 | | } |
820 | | } |
821 | | |
822 | | impl<T: ?Sized + Immutable + Hash> Hash for ReadOnly<T> { |
823 | | #[inline(always)] |
824 | | fn hash<H: Hasher>(&self, state: &mut H) { |
825 | | self.deref().hash(state); |
826 | | } |
827 | | } |
828 | | |
829 | | macro_rules! impl_read_only_fmt { |
830 | | ($($trait:ident),* $(,)?) => {$( |
831 | | impl<T: ?Sized + Immutable + fmt::$trait> fmt::$trait for ReadOnly<T> { |
832 | | #[inline(always)] |
833 | | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
834 | | fmt::$trait::fmt(self.deref(), f) |
835 | | } |
836 | | } |
837 | | )*}; |
838 | | } |
839 | | |
840 | | impl_read_only_fmt!(Debug, Display, Binary, Octal, LowerHex, UpperHex, LowerExp, UpperExp); |
841 | | |
842 | | // SAFETY: See safety comment on `ProjectToTag`. |
843 | | unsafe impl<T: HasTag<Client> + ?Sized, Client> HasTag<Client> for ReadOnly<T> { |
844 | | #[allow(clippy::missing_inline_in_public_items)] |
845 | | fn only_derive_is_allowed_to_implement_this_trait() |
846 | | where |
847 | | Self: Sized, |
848 | | { |
849 | | } |
850 | | |
851 | | type Tag = <T as HasTag<Client>>::Tag; |
852 | | |
853 | | // SAFETY: `<T as SizeEq<ReadOnly<T>>>::CastFrom` is a no-op projection that |
854 | | // produces a pointer with the same referent. By invariant, for any |
855 | | // `Ptr<'_, T, I>` it is sound to use |
856 | | // `<T as HasTag<Client>>::ProjectToTag` to project to a |
857 | | // shared `Ptr<'_, <T as HasTag<Client>>::Tag, |
858 | | // (_, I::Alignment, I::Validity)>`. Since `ReadOnly<T>` has the same |
859 | | // layout and validity as `T`, the same is true of projecting from a |
860 | | // `Ptr<'_, ReadOnly<T>, I>`. |
861 | | type ProjectToTag = crate::pointer::cast::TransitiveProject< |
862 | | T, |
863 | | <T as SizeEq<ReadOnly<T>>>::CastFrom, |
864 | | <T as HasTag<Client>>::ProjectToTag, |
865 | | >; |
866 | | } |
867 | | |
868 | | // SAFETY: `ReadOnly<T>` is a `#[repr(transparent)]` wrapper around `T`, and so |
869 | | // has the same fields at the same offsets. Thus, it satisfies the safety |
870 | | // invariants of `HasField<Client, Field, VARIANT_ID, FIELD_ID>` for field `f` |
871 | | // exactly when `T` does, as guaranteed by the `T: HasField` bound: |
872 | | // - If `VARIANT_ID` is `STRUCT_VARIANT_ID`, `UNION_VARIANT_ID`, or |
873 | | // `REPR_C_UNION_VARIANT_ID`, then `T` has the layout of a struct, non-C union, |
874 | | // or C union type respectively. Since `ReadOnly<T>` is a transparent wrapper |
875 | | // around `T`, it does too. Otherwise, `VARIANT_ID` is the identifier ID of an |
876 | | // enum variant; `T` has the layout of an enum type, and `ReadOnly<T>` does too. |
877 | | // - By `T: HasField<_, _, _, FIELD_ID>`: |
878 | | // - `T` has a field `f` with name `n` such that |
879 | | // `FIELD_ID = zerocopy::ident_id!(n)` or at index `i` such that |
880 | | // `FIELD_ID = zerocopy::ident_id!(i)`. |
881 | | // - `Field` has the same visibility as `f`. |
882 | | // - `T::Type` has the same type as `f`. Thus, `ReadOnly<T::Type>` has the |
883 | | // same type as `f`, wrapped in `ReadOnly`. |
884 | | // |
885 | | // `project` satisfies its post-condition – namely, that the returned pointer |
886 | | // refers to a non-strict subset of the bytes of `slf`'s referent, and has the |
887 | | // same provenance as `slf` – because all intermediate operations satisfy those |
888 | | // same conditions. |
889 | | unsafe impl<T, Client, Field, const VARIANT_ID: i128, const FIELD_ID: i128> |
890 | | HasField<Client, Field, VARIANT_ID, FIELD_ID> for ReadOnly<T> |
891 | | where |
892 | | T: HasField<Client, Field, VARIANT_ID, FIELD_ID> + ?Sized, |
893 | | { |
894 | | #[allow(clippy::missing_inline_in_public_items)] |
895 | | fn only_derive_is_allowed_to_implement_this_trait() |
896 | | where |
897 | | Self: Sized, |
898 | | { |
899 | | } |
900 | | |
901 | | type Type = ReadOnly<T::Type>; |
902 | | |
903 | | #[inline(always)] |
904 | | fn project(slf: PtrInner<'_, Self>) -> *mut ReadOnly<T::Type> { |
905 | | slf.project::<_, <T as SizeEq<ReadOnly<T>>>::CastFrom>() |
906 | | .project::<_, crate::pointer::cast::Projection<Client, Field, VARIANT_ID, FIELD_ID>>() |
907 | | .project::<_, <ReadOnly<T::Type> as SizeEq<T::Type>>::CastFrom>() |
908 | | .as_non_null() |
909 | | .as_ptr() |
910 | | } |
911 | | } |
912 | | |
913 | | // SAFETY: `ReadOnly<T>` is a `#[repr(transparent)]` wrapper around `T`, and so |
914 | | // has the same fields at the same offsets. `is_projectable` simply delegates to |
915 | | // `T::is_projectable`, which is sound because a `Ptr<'_, ReadOnly<T>, I>` will |
916 | | // be projectable exactly when a `Ptr<'_, T, I>` referent is. |
917 | | unsafe impl<T, Client, Field, I, const VARIANT_ID: i128, const FIELD_ID: i128> |
918 | | ProjectField<Client, Field, I, VARIANT_ID, FIELD_ID> for ReadOnly<T> |
919 | | where |
920 | | T: ProjectField<Client, Field, I, VARIANT_ID, FIELD_ID> + ?Sized, |
921 | | I: invariant::Invariants, |
922 | | { |
923 | | #[allow(clippy::missing_inline_in_public_items)] |
924 | | fn only_derive_is_allowed_to_implement_this_trait() |
925 | | where |
926 | | Self: Sized, |
927 | | { |
928 | | } |
929 | | |
930 | | type Invariants = <T as ProjectField<Client, Field, I, VARIANT_ID, FIELD_ID>>::Invariants; |
931 | | |
932 | | type Error = <T as ProjectField<Client, Field, I, VARIANT_ID, FIELD_ID>>::Error; |
933 | | |
934 | | #[inline(always)] |
935 | | fn is_projectable<'a>( |
936 | | ptr: Ptr<'a, <Self as HasTag<Client>>::Tag, (invariant::Shared, I::Alignment, I::Validity)>, |
937 | | ) -> Result<(), Self::Error> { |
938 | | <T as ProjectField<Client, Field, I, VARIANT_ID, FIELD_ID>>::is_projectable(ptr) |
939 | | } |
940 | | } |
941 | | |
942 | | #[cfg(test)] |
943 | | mod tests { |
944 | | use core::panic::AssertUnwindSafe; |
945 | | |
946 | | use super::*; |
947 | | use crate::util::testutil::*; |
948 | | |
949 | | #[test] |
950 | | #[allow(clippy::clone_on_copy, clippy::non_canonical_clone_impl)] |
951 | | fn test_read_only_copy_clone() { |
952 | | // This type deliberately does not implement `Immutable`. Copying the |
953 | | // wrapper must not require it or invoke the inner `Clone` impl. |
954 | | #[derive(Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] |
955 | | struct PanickingClone(u16); |
956 | | |
957 | | impl Clone for PanickingClone { |
958 | | fn clone(&self) -> Self { |
959 | | panic!("ReadOnly must not call the inner Clone implementation") |
960 | | } |
961 | | } |
962 | | |
963 | | static_assertions::assert_not_impl_any!( |
964 | | ReadOnly<PanickingClone>: Debug, PartialEq, Eq, PartialOrd, Ord, |
965 | | Hash, AsRef<PanickingClone>, Borrow<PanickingClone> |
966 | | ); |
967 | | static_assertions::assert_not_impl_any!( |
968 | | ReadOnly<Cell<u8>>: Copy, Clone, Deref, AsRef<Cell<u8>>, Borrow<Cell<u8>> |
969 | | ); |
970 | | |
971 | | let value = ReadOnly::new(PanickingClone(42)); |
972 | | let copy = value; |
973 | | let clone = value.clone(); |
974 | | assert_eq!(ReadOnly::into_inner(copy).0, 42); |
975 | | assert_eq!(ReadOnly::into_inner(clone).0, 42); |
976 | | } |
977 | | |
978 | | #[test] |
979 | | fn test_read_only_mutable_access() { |
980 | | // Construction and exclusive access also support interior mutability. |
981 | | let mut value = ReadOnly::<Cell<u8>>::default(); |
982 | | let inner: &mut Cell<u8> = value.as_mut(); |
983 | | inner.set(42); |
984 | | assert_eq!(ReadOnly::into_inner(value).get(), 42); |
985 | | |
986 | | let value = ReadOnly::from(Cell::new(7)); |
987 | | assert_eq!(ReadOnly::into_inner(value).get(), 7); |
988 | | } |
989 | | |
990 | | #[test] |
991 | | fn test_read_only_borrowed_lookup() { |
992 | | // Borrowed lookups require equality, hashing, and ordering to agree |
993 | | // between the wrapper and the inner value. |
994 | | let mut hash_map = std::collections::HashMap::new(); |
995 | | hash_map.insert(ReadOnly::new(42u16), true); |
996 | | assert_eq!(hash_map.get(&42u16), Some(&true)); |
997 | | assert_eq!(hash_map.get(&43u16), None); |
998 | | |
999 | | let mut tree = std::collections::BTreeMap::new(); |
1000 | | tree.insert(ReadOnly::new(42u16), true); |
1001 | | assert_eq!(tree.get(&42u16), Some(&true)); |
1002 | | assert_eq!(tree.get(&43u16), None); |
1003 | | |
1004 | | // Preserve partial orders, including incomparable values. |
1005 | | let nan = ReadOnly::new(f32::NAN); |
1006 | | assert_eq!(nan.partial_cmp(&nan), None); |
1007 | | assert_ne!(nan, nan); |
1008 | | } |
1009 | | |
1010 | | #[test] |
1011 | | fn test_read_only_unsized() { |
1012 | | let bytes = [1u8, 2]; |
1013 | | let value: &ReadOnly<[u8]> = (&bytes[..]).into(); |
1014 | | let as_ref: &[u8] = value.as_ref(); |
1015 | | let borrowed: &[u8] = value.borrow(); |
1016 | | assert_eq!(as_ref, bytes); |
1017 | | assert_eq!(borrowed, bytes); |
1018 | | assert_eq!(value, value); |
1019 | | assert_eq!(value.cmp(value), Ordering::Equal); |
1020 | | |
1021 | | let text: &ReadOnly<str> = "hello".into(); |
1022 | | assert_eq!(format!("{:.3}", text), "hel"); |
1023 | | } |
1024 | | |
1025 | | #[test] |
1026 | | fn test_read_only_formatting() { |
1027 | | let value = ReadOnly::new(42u16); |
1028 | | // Check that formatting flags are forwarded as well as the value. |
1029 | | assert_eq!(format!("{:04?}", value), "0042"); |
1030 | | assert_eq!(format!("{:04}", value), "0042"); |
1031 | | assert_eq!(format!("{:#010b}", value), "0b00101010"); |
1032 | | assert_eq!(format!("{:#06o}", value), "0o0052"); |
1033 | | assert_eq!(format!("{:#06x}", value), "0x002a"); |
1034 | | assert_eq!(format!("{:#06X}", value), "0x002A"); |
1035 | | assert_eq!(format!("{:.2e}", value), "4.20e1"); |
1036 | | assert_eq!(format!("{:.2E}", value), "4.20E1"); |
1037 | | } |
1038 | | |
1039 | | #[test] |
1040 | | fn test_unalign() { |
1041 | | // Test methods that don't depend on alignment. |
1042 | | let mut u = Unalign::new(AU64(123)); |
1043 | | assert_eq!(u.get(), AU64(123)); |
1044 | | assert_eq!(u.into_inner(), AU64(123)); |
1045 | | assert_eq!(u.get_ptr(), <*const _>::cast::<AU64>(&u)); |
1046 | | assert_eq!(u.get_mut_ptr(), <*mut _>::cast::<AU64>(&mut u)); |
1047 | | u.set(AU64(321)); |
1048 | | assert_eq!(u.get(), AU64(321)); |
1049 | | |
1050 | | // Test methods that depend on alignment (when alignment is satisfied). |
1051 | | let mut u: Align<_, AU64> = Align::new(Unalign::new(AU64(123))); |
1052 | | assert_eq!(u.t.try_deref().unwrap(), &AU64(123)); |
1053 | | assert_eq!(u.t.try_deref_mut().unwrap(), &mut AU64(123)); |
1054 | | // SAFETY: The `Align<_, AU64>` guarantees proper alignment. |
1055 | | assert_eq!(unsafe { u.t.deref_unchecked() }, &AU64(123)); |
1056 | | // SAFETY: The `Align<_, AU64>` guarantees proper alignment. |
1057 | | assert_eq!(unsafe { u.t.deref_mut_unchecked() }, &mut AU64(123)); |
1058 | | *u.t.try_deref_mut().unwrap() = AU64(321); |
1059 | | assert_eq!(u.t.get(), AU64(321)); |
1060 | | |
1061 | | // Test methods that depend on alignment (when alignment is not |
1062 | | // satisfied). |
1063 | | let mut u: ForceUnalign<_, AU64> = ForceUnalign::new(Unalign::new(AU64(123))); |
1064 | | assert!(matches!(u.t.try_deref(), Err(AlignmentError { .. }))); |
1065 | | assert!(matches!(u.t.try_deref_mut(), Err(AlignmentError { .. }))); |
1066 | | |
1067 | | // Test methods that depend on `T: Unaligned`. |
1068 | | let mut u = Unalign::new(123u8); |
1069 | | assert_eq!(u.try_deref(), Ok(&123)); |
1070 | | assert_eq!(u.try_deref_mut(), Ok(&mut 123)); |
1071 | | assert_eq!(u.deref(), &123); |
1072 | | assert_eq!(u.deref_mut(), &mut 123); |
1073 | | *u = 21; |
1074 | | assert_eq!(u.get(), 21); |
1075 | | |
1076 | | // Test that some `Unalign` functions and methods are `const`. |
1077 | | const _UNALIGN: Unalign<u64> = Unalign::new(0); |
1078 | | const _UNALIGN_PTR: *const u64 = _UNALIGN.get_ptr(); |
1079 | | const _U64: u64 = _UNALIGN.into_inner(); |
1080 | | // Make sure all code is considered "used". |
1081 | | // |
1082 | | // FIXME(https://github.com/rust-lang/rust/issues/104084): Remove this |
1083 | | // attribute. |
1084 | | #[allow(dead_code)] |
1085 | | const _: () = { |
1086 | | let x: Align<_, AU64> = Align::new(Unalign::new(AU64(123))); |
1087 | | // Make sure that `deref_unchecked` is `const`. |
1088 | | // |
1089 | | // SAFETY: The `Align<_, AU64>` guarantees proper alignment. |
1090 | | let au64 = unsafe { x.t.deref_unchecked() }; |
1091 | | match au64 { |
1092 | | AU64(123) => {} |
1093 | | _ => const_unreachable!(), |
1094 | | } |
1095 | | }; |
1096 | | } |
1097 | | |
1098 | | #[test] |
1099 | | fn test_unalign_update() { |
1100 | | let mut u = Unalign::new(AU64(123)); |
1101 | | u.update(|a| a.0 += 1); |
1102 | | assert_eq!(u.get(), AU64(124)); |
1103 | | |
1104 | | // Test that, even if the callback panics, the original is still |
1105 | | // correctly overwritten. Use a `Box` so that Miri is more likely to |
1106 | | // catch any unsoundness (which would likely result in two `Box`es for |
1107 | | // the same heap object, which is the sort of thing that Miri would |
1108 | | // probably catch). |
1109 | | let mut u = Unalign::new(Box::new(AU64(123))); |
1110 | | let res = std::panic::catch_unwind(AssertUnwindSafe(|| { |
1111 | | u.update(|a| { |
1112 | | a.0 += 1; |
1113 | | panic!(); |
1114 | | }) |
1115 | | })); |
1116 | | assert!(res.is_err()); |
1117 | | assert_eq!(u.into_inner(), Box::new(AU64(124))); |
1118 | | |
1119 | | // Test the align_of::<T>() == 1 optimization. |
1120 | | let mut u = Unalign::new([0u8, 1]); |
1121 | | u.update(|a| a[0] += 1); |
1122 | | assert_eq!(u.get(), [1u8, 1]); |
1123 | | } |
1124 | | |
1125 | | #[test] |
1126 | | fn test_unalign_copy_clone() { |
1127 | | // Test that `Copy` and `Clone` do not cause soundness issues. This test |
1128 | | // is mainly meant to exercise UB that would be caught by Miri. |
1129 | | |
1130 | | // `u.t` is definitely not validly-aligned for `AU64`'s alignment of 8. |
1131 | | let u = ForceUnalign::<_, AU64>::new(Unalign::new(AU64(123))); |
1132 | | #[allow(clippy::clone_on_copy)] |
1133 | | let v = u.t.clone(); |
1134 | | let w = u.t; |
1135 | | assert_eq!(u.t.get(), v.get()); |
1136 | | assert_eq!(u.t.get(), w.get()); |
1137 | | assert_eq!(v.get(), w.get()); |
1138 | | } |
1139 | | |
1140 | | #[test] |
1141 | | fn test_unalign_trait_impls() { |
1142 | | let zero = Unalign::new(0u8); |
1143 | | let one = Unalign::new(1u8); |
1144 | | |
1145 | | assert!(zero < one); |
1146 | | assert_eq!(PartialOrd::partial_cmp(&zero, &one), Some(Ordering::Less)); |
1147 | | assert_eq!(Ord::cmp(&zero, &one), Ordering::Less); |
1148 | | |
1149 | | assert_ne!(zero, one); |
1150 | | assert_eq!(zero, zero); |
1151 | | assert!(!PartialEq::eq(&zero, &one)); |
1152 | | assert!(PartialEq::eq(&zero, &zero)); |
1153 | | |
1154 | | fn hash<T: Hash>(t: &T) -> u64 { |
1155 | | let mut h = std::collections::hash_map::DefaultHasher::new(); |
1156 | | t.hash(&mut h); |
1157 | | h.finish() |
1158 | | } |
1159 | | |
1160 | | assert_eq!(hash(&zero), hash(&0u8)); |
1161 | | assert_eq!(hash(&one), hash(&1u8)); |
1162 | | |
1163 | | assert_eq!(format!("{:?}", zero), format!("{:?}", 0u8)); |
1164 | | assert_eq!(format!("{:?}", one), format!("{:?}", 1u8)); |
1165 | | assert_eq!(format!("{}", zero), format!("{}", 0u8)); |
1166 | | assert_eq!(format!("{}", one), format!("{}", 1u8)); |
1167 | | } |
1168 | | |
1169 | | #[test] |
1170 | | #[allow(clippy::as_conversions)] |
1171 | | fn test_maybe_uninit() { |
1172 | | // int |
1173 | | { |
1174 | | let input = 42; |
1175 | | let uninit = MaybeUninit::new(input); |
1176 | | // SAFETY: `uninit` is in an initialized state |
1177 | | let output = unsafe { uninit.assume_init() }; |
1178 | | assert_eq!(input, output); |
1179 | | } |
1180 | | |
1181 | | // thin ref |
1182 | | { |
1183 | | let input = 42; |
1184 | | let uninit = MaybeUninit::new(&input); |
1185 | | // SAFETY: `uninit` is in an initialized state |
1186 | | let output = unsafe { uninit.assume_init() }; |
1187 | | assert_eq!(&input as *const _, output as *const _); |
1188 | | assert_eq!(input, *output); |
1189 | | } |
1190 | | |
1191 | | // wide ref |
1192 | | { |
1193 | | let input = [1, 2, 3, 4]; |
1194 | | let uninit = MaybeUninit::new(&input[..]); |
1195 | | // SAFETY: `uninit` is in an initialized state |
1196 | | let output = unsafe { uninit.assume_init() }; |
1197 | | assert_eq!(&input[..] as *const _, output as *const _); |
1198 | | assert_eq!(input, *output); |
1199 | | } |
1200 | | } |
1201 | | #[test] |
1202 | | fn test_maybe_uninit_uninit() { |
1203 | | let _uninit = MaybeUninit::<u8>::uninit(); |
1204 | | // Cannot check value, but can check it compiles and runs |
1205 | | } |
1206 | | |
1207 | | #[test] |
1208 | | #[cfg(feature = "alloc")] |
1209 | | fn test_maybe_uninit_new_boxed_uninit() { |
1210 | | let _boxed = MaybeUninit::<u8>::new_boxed_uninit(()).unwrap(); |
1211 | | } |
1212 | | |
1213 | | #[test] |
1214 | | fn test_maybe_uninit_debug() { |
1215 | | let uninit = MaybeUninit::<u8>::uninit(); |
1216 | | assert!(format!("{:?}", uninit).contains("MaybeUninit")); |
1217 | | } |
1218 | | } |