Lines Matching full:t
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
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`:
29 /// fail if the `Unalign` does not satisfy `T`'s alignment requirement at
33 /// the `Unalign` satisfies `T`'s alignment requirement
34 /// - (where `T: Unaligned`) infallibly by reference, using [`Deref::deref`] or
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.
108 // also means that `T` must be `Sized`; unless something changes, we can never
109 // support unsized `T`. [3]
118 pub struct Unalign<T>(T);
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>);
139 // - `Unalign<T>` promises to have alignment 1, and so we don't require that `T:
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
144 // interior mutation exactly when `T` does.
145 // - `TryFromBytes`: `Unalign<T>` has the same the same bit validity as `T`, so
146 // `T::is_bit_valid` is a sound implementation of `is_bit_valid`.
150 impl_or_verify!(T => Unaligned for Unalign<T>);
151 impl_or_verify!(T: Immutable => Immutable for Unalign<T>);
153 T: TryFromBytes => TryFromBytes for Unalign<T>;
154 |c| T::is_bit_valid(c.transmute::<_, _, BecauseImmutable>())
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>);
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
164 impl<T: Copy> Clone for Unalign<T> {
166 fn clone(&self) -> Unalign<T> { in clone() argument
171 impl<T> Unalign<T> {
174 pub const fn new(val: T) -> Unalign<T> { in new() argument
178 /// Consumes `self`, returning the inner `T`.
180 pub const fn into_inner(self) -> T { in into_inner() argument
182 // and bit validity as `T`. in into_inner()
193 /// Attempts to return a reference to the wrapped `T`, failing if `self` is
196 /// If `self` does not satisfy `align_of::<T>()`, then `try_deref` returns
199 /// If `T: Unaligned`, then `Unalign<T>` implements [`Deref`], and callers
202 pub fn try_deref(&self) -> Result<&T, AlignmentError<&Self, T>> { in try_deref() argument
213 /// Attempts to return a mutable reference to the wrapped `T`, failing if
216 /// If `self` does not satisfy `align_of::<T>()`, then `try_deref` returns
219 /// If `T: Unaligned`, then `Unalign<T>` implements [`DerefMut`], and
222 pub fn try_deref_mut(&mut self) -> Result<&mut T, AlignmentError<&mut Self, T>> { in try_deref_mut() argument
230 /// Returns a reference to the wrapped `T` without checking alignment.
232 /// If `T: Unaligned`, then `Unalign<T>` implements[ `Deref`], and callers
237 /// The caller must guarantee that `self` satisfies `align_of::<T>()`.
239 pub const unsafe fn deref_unchecked(&self) -> &T { in deref_unchecked() argument
240 // SAFETY: `Unalign<T>` is `repr(transparent)`, so there is a valid `T` in deref_unchecked()
243 // know that it is sound to create a reference to `T` at this memory in deref_unchecked()
252 /// Returns a mutable reference to the wrapped `T` without checking
255 /// If `T: Unaligned`, then `Unalign<T>` implements[ `DerefMut`], and
260 /// The caller must guarantee that `self` satisfies `align_of::<T>()`.
262 pub unsafe fn deref_mut_unchecked(&mut self) -> &mut T { in deref_mut_unchecked() argument
263 // SAFETY: `self.get_mut_ptr()` returns a raw pointer to a valid `T` at in deref_mut_unchecked()
267 // create a reference to a `T` at this memory location. in deref_mut_unchecked()
271 /// Gets an unaligned raw pointer to the inner `T`.
276 /// `align_of::<T>()`. Most functions which operate on raw pointers require
292 pub const fn get_ptr(&self) -> *const T { in get_ptr() argument
296 /// Gets an unaligned mutable raw pointer to the inner `T`.
301 /// `align_of::<T>()`. Most functions which operate on raw pointers require
312 pub fn get_mut_ptr(&mut self) -> *mut T { in get_mut_ptr() argument
316 /// Sets the inner `T`, dropping the previous value.
319 pub fn set(&mut self, t: T) { in set() argument
320 *self = Unalign::new(t); in set()
323 /// Updates the inner `T` by calling a function on it.
325 /// If [`T: Unaligned`], then `Unalign<T>` implements [`DerefMut`], and that
330 /// `2 * size_of::<T>()` bytes. \[1\]
332 /// \[1\] Since the inner `T` may not be aligned, it would not be sound to
337 /// [`T: Unaligned`]: Unaligned
339 pub fn update<O, F: FnOnce(&mut T) -> O>(&mut self, f: F) -> O { in update()
340 if mem::align_of::<T>() == 1 { in update()
341 // While we advise callers to use `DerefMut` when `T: Unaligned`, in update()
342 // not all callers will be able to guarantee `T: Unaligned` in all in update()
344 // which is generic over `T` may sometimes be called by *their* in update()
345 // callers with `T` such that `align_of::<T>() == 1`, but cannot in update()
349 // SAFETY: Since `T`'s alignment is 1, `self` satisfies its in update()
351 let t = unsafe { self.deref_mut_unchecked() }; in update() localVariable
352 return f(t); in update()
357 struct WriteBackOnDrop<T> { in update()
358 copy: ManuallyDrop<T>, in update()
359 slf: *mut Unalign<T>, in update()
362 impl<T> Drop for WriteBackOnDrop<T> { in update()
375 // points to an initialized `Unalign<T>` because it is a mutable in update()
378 // Since `T: !Copy`, it would be unsound in the general case to allow in update()
379 // both the original `Unalign<T>` and the copy to be used by safe code. in update()
397 impl<T: Copy> Unalign<T> {
398 /// Gets a copy of the inner `T`.
401 pub fn get(&self) -> T { in get() argument
407 impl<T: Unaligned> Deref for Unalign<T> {
408 type Target = T;
411 fn deref(&self) -> &T { in deref() argument
416 impl<T: Unaligned> DerefMut for Unalign<T> {
418 fn deref_mut(&mut self) -> &mut T { in deref_mut() argument
423 impl<T: Unaligned + PartialOrd> PartialOrd<Unalign<T>> for Unalign<T> {
425 fn partial_cmp(&self, other: &Unalign<T>) -> Option<Ordering> { in partial_cmp()
430 impl<T: Unaligned + Ord> Ord for Unalign<T> {
432 fn cmp(&self, other: &Unalign<T>) -> Ordering { in cmp()
437 impl<T: Unaligned + PartialEq> PartialEq<Unalign<T>> for Unalign<T> {
439 fn eq(&self, other: &Unalign<T>) -> bool { in eq()
444 impl<T: Unaligned + Eq> Eq for Unalign<T> {}
446 impl<T: Unaligned + Hash> Hash for Unalign<T> {
456 impl<T: Unaligned + Debug> Debug for Unalign<T> {
463 impl<T: Unaligned + Display> Display for Unalign<T> {
470 /// A wrapper type to construct uninitialized instances of `T`.
478 /// The same layout guarantees and caveats apply to `MaybeUninit<T>` as apply to
480 /// for `T: !Sized`, there is no single value for `T`'s size. Instead, for such
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
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`,
500 T::MaybeUninit,
504 impl<T: ?Sized + KnownLayout> MaybeUninit<T> {
505 /// Constructs a `MaybeUninit<T>` initialized with the given value.
507 pub fn new(val: T) -> Self in new()
509 T: Sized, in new()
512 // SAFETY: It is valid to transmute `val` to `MaybeUninit<T>` because it in new()
513 // is both valid to transmute `val` to `T::MaybeUninit`, and it is valid in new()
514 // to transmute from `T::MaybeUninit` to `MaybeUninit<T>`. in new()
516 // First, it is valid to transmute `val` to `T::MaybeUninit` because, by in new()
517 // invariant on `T::MaybeUninit`: in new()
518 // - For `T: Sized`, `T` and `T::MaybeUninit` have the same size. in new()
520 // `T::MaybeUninit`. in new()
522 // Second, it is additionally valid to transmute from `T::MaybeUninit` in new()
523 // to `MaybeUninit<T>`, because `MaybeUninit<T>` is a in new()
524 // `repr(transparent)` wrapper around `T::MaybeUninit`. in new()
526 // These two transmutes are collapsed into one so we don't need to add a in new()
527 // `T::MaybeUninit: Sized` bound to this function's `where` clause. in new()
531 /// Constructs an uninitialized `MaybeUninit<T>`.
536 T: Sized, in uninit()
539 let uninit = CoreMaybeUninit::<T>::uninit(); in uninit()
540 // SAFETY: It is valid to transmute from `CoreMaybeUninit<T>` to in uninit()
541 // `MaybeUninit<T>` since they both admit uninitialized bytes in all in uninit()
542 // positions, and they have the same size (i.e., that of `T`). in uninit()
544 // `MaybeUninit<T>` has the same size as `T`, because (by invariant on in uninit()
545 // `T::MaybeUninit`) `T::MaybeUninit` has `T::LAYOUT` identical to `T`, in uninit()
546 // and because (invariant on `T::LAYOUT`) we can trust that `LAYOUT` in uninit()
547 // accurately reflects the layout of `T`. in uninit()
549 // `CoreMaybeUninit<T>` has the same size as `T` [1] and admits in uninit()
554 // `MaybeUninit<T>` is guaranteed to have the same size, alignment, in uninit()
555 // and ABI as `T` in uninit()
559 /// Creates a `Box<MaybeUninit<T>>`.
570 pub fn new_boxed_uninit(meta: T::PointerMetadata) -> Result<Box<Self>, AllocError> { in new_boxed_uninit()
579 /// Extracts the value from the `MaybeUninit<T>` container.
586 pub unsafe fn assume_init(self) -> T in assume_init()
588 T: Sized, in assume_init()
596 impl<T: ?Sized + KnownLayout> fmt::Debug for MaybeUninit<T> {
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.
613 /// Note that `&mut ReadOnly<T>` still permits mutation – the read-only
618 pub struct ReadOnly<T: ?Sized> {
619 // INVARIANT: `inner` is never mutated through a `&ReadOnly<T>`
621 inner: T,
624 impl<T> ReadOnly<T> {
628 pub const fn new(t: T) -> ReadOnly<T> { in new() argument
629 ReadOnly { inner: t } in new()
635 pub fn into_inner(r: ReadOnly<T>) -> T { in into_inner() argument
640 impl<T: ?Sized> ReadOnly<T> {
642 pub(crate) fn as_mut(r: &mut ReadOnly<T>) -> &mut T { in as_mut() argument
643 // SAFETY: `r: &mut ReadOnly`, so this doesn't violate the invariant in as_mut()
644 // that `inner` is never mutated through a `&ReadOnly<T>` reference. in as_mut()
652 pub(crate) const unsafe fn as_ref_unchecked(r: &ReadOnly<T>) -> &T { in as_ref_unchecked() argument
659 // SAFETY: `ReadOnly<T>` is a `#[repr(transparent)` wrapper around `T`.
661 unsafe_impl_known_layout!(T: ?Sized + KnownLayout => #[repr(T)] ReadOnly<T>);
666 // - `ReadOnly<T>` has the same alignment as `T`, and so it is `Unaligned`
667 // exactly when `T` is as well.
668 // - `ReadOnly<T>` has the same bit validity as `T`, and so this `is_bit_valid`
670 // - `ReadOnly<T>` has the same bit validity as `T`, and so it is `FromZeros`,
671 // `FromBytes`, and `IntoBytes` exactly when `T` is as well.
673 unsafe_impl!(T: ?Sized + Unaligned => Unaligned for ReadOnly<T>);
675 T: ?Sized + TryFromBytes => TryFromBytes for ReadOnly<T>;
676 … |c| T::is_bit_valid(c.cast::<_, <ReadOnly<T> as SizeEq<ReadOnly<ReadOnly<T>>>>::CastFrom, _>())
678 unsafe_impl!(T: ?Sized + FromZeros => FromZeros for ReadOnly<T>);
679 unsafe_impl!(T: ?Sized + FromBytes => FromBytes for ReadOnly<T>);
680 unsafe_impl!(T: ?Sized + IntoBytes => IntoBytes for ReadOnly<T>);
683 // SAFETY: By invariant, `inner` is never mutated through a `&ReadOnly<T>`
686 unsafe_impl!(T: ?Sized => Immutable for ReadOnly<T>);
692 // SAFETY: `ReadOnly<T>` has the same layout as `T`.
693 define_cast!(unsafe { pub CastFromReadOnly<T: ?Sized> = ReadOnly<T> => T});
694 // SAFETY: `ReadOnly<T>` has the same layout as `T`.
695 unsafe impl<T: ?Sized> CastExact<ReadOnly<T>, T> for CastFromReadOnly {}
696 // SAFETY: `ReadOnly<T>` has the same layout as `T`.
697 define_cast!(unsafe { pub CastToReadOnly<T: ?Sized> = T => ReadOnly<T>});
698 // SAFETY: `ReadOnly<T>` has the same layout as `T`.
699 unsafe impl<T: ?Sized> CastExact<T, ReadOnly<T>> for CastToReadOnly {}
701 impl<T: ?Sized> SizeEq<ReadOnly<T>> for T { implementation
705 impl<T: ?Sized> SizeEq<T> for ReadOnly<T> {
710 // SAFETY: `ReadOnly<T>` is a `#[repr(transparent)]` wrapper around `T`, and so
711 // it has the same bit validity as `T`.
712 unsafe impl<T: ?Sized> TransmuteFrom<T, Valid, Valid> for ReadOnly<T> {}
714 // SAFETY: `ReadOnly<T>` is a `#[repr(transparent)]` wrapper around `T`, and so
715 // it has the same bit validity as `T`.
716 unsafe impl<T: ?Sized> TransmuteFrom<ReadOnly<T>, Valid, Valid> for T {} implementation
718 impl<'a, T: ?Sized + Immutable> From<&'a T> for &'a ReadOnly<T> {
720 fn from(t: &'a T) -> &'a ReadOnly<T> { in from() argument
721 let ro = Ptr::from_ref(t).transmute::<_, _, (_, _)>(); in from()
722 // SAFETY: `ReadOnly<T>` has the same alignment as `T`, and in from()
729 impl<T: ?Sized + Immutable> Deref for ReadOnly<T> {
730 type Target = T;
734 // SAFETY: By `T: Immutable`, `&T` doesn't permit interior mutation. in deref()
739 impl<T: ?Sized + Immutable> DerefMut for ReadOnly<T> {
746 impl<T: ?Sized + Immutable + Debug> Debug for ReadOnly<T> {
754 unsafe impl<T: HasTag + ?Sized> HasTag for ReadOnly<T> {
762 type Tag = T::Tag;
764 // SAFETY: `<T as SizeEq<ReadOnly<T>>>::CastFrom` is a no-op projection that
766 // T, I>` it is sound to use `T::ProjectToTag` to project to a `Ptr<'_,
767 // T::Tag, I>`. Since `ReadOnly<T>` has the same layout and validity as `T`,
768 // the same is true of projecting from a `Ptr<'_, ReadOnly<T>, I>`.
770 T,
771 <T as SizeEq<ReadOnly<T>>>::CastFrom,
772 T::ProjectToTag,
776 // SAFETY: `ReadOnly<T>` is a `#[repr(transparent)]` wrapper around `T`, and so
779 // when `T` does, as guaranteed by the `T: HasField` bound:
780 // - If `VARIANT_ID` is `STRUCT_VARIANT_ID` or `UNION_VARIANT_ID`, then `T` has
781 // the layout of a struct or union type. Since `ReadOnly<T>` is a transparent
782 // wrapper around `T`, it does too. Otherwise, if `VARIANT_ID` is an enum
783 // variant index, then `T` has the layout of an enum type, and `ReadOnly<T>`
785 // - By `T: HasField<_, _, FIELD_ID>`:
786 // - `T` has a field `f` with name `n` such that
790 // - `T::Type` has the same type as `f`. Thus, `ReadOnly<T::Type>` has the
797 unsafe impl<T, Field, const VARIANT_ID: i128, const FIELD_ID: i128>
798 HasField<Field, VARIANT_ID, FIELD_ID> for ReadOnly<T>
800 T: HasField<Field, VARIANT_ID, FIELD_ID> + ?Sized,
809 type Type = ReadOnly<T::Type>;
812 fn project(slf: PtrInner<'_, Self>) -> *mut ReadOnly<T::Type> { in project()
813 slf.project::<_, <T as SizeEq<ReadOnly<T>>>::CastFrom>() in project()
815 .project::<_, <ReadOnly<T::Type> as SizeEq<T::Type>>::CastFrom>() in project()
821 // SAFETY: `ReadOnly<T>` is a `#[repr(transparent)]` wrapper around `T`, and so
823 // `T::is_projectable`, which is sound because a `Ptr<'_, ReadOnly<T>, I>` will
824 // be projectable exactly when a `Ptr<'_, T, I>` referent is.
825 unsafe impl<T, Field, I, const VARIANT_ID: i128, const FIELD_ID: i128>
826 ProjectField<Field, I, VARIANT_ID, FIELD_ID> for ReadOnly<T>
828 T: ProjectField<Field, I, VARIANT_ID, FIELD_ID> + ?Sized,
838 type Invariants = T::Invariants;
840 type Error = T::Error;
844 T::is_projectable(ptr) in is_projectable()
857 // Test methods that don't depend on alignment. in test_unalign()
868 assert_eq!(u.t.try_deref().unwrap(), &AU64(123)); in test_unalign()
869 assert_eq!(u.t.try_deref_mut().unwrap(), &mut AU64(123)); in test_unalign()
871 assert_eq!(unsafe { u.t.deref_unchecked() }, &AU64(123)); in test_unalign()
873 assert_eq!(unsafe { u.t.deref_mut_unchecked() }, &mut AU64(123)); in test_unalign()
874 *u.t.try_deref_mut().unwrap() = AU64(321); in test_unalign()
875 assert_eq!(u.t.get(), AU64(321)); in test_unalign()
880 assert!(matches!(u.t.try_deref(), Err(AlignmentError { .. }))); in test_unalign()
881 assert!(matches!(u.t.try_deref_mut(), Err(AlignmentError { .. }))); in test_unalign()
883 // Test methods that depend on `T: Unaligned`. in test_unalign()
906 let au64 = unsafe { x.t.deref_unchecked() }; in test_unalign()
935 // Test the align_of::<T>() == 1 optimization. in test_unalign_update()
946 // `u.t` is definitely not validly-aligned for `AU64`'s alignment of 8. in test_unalign_copy_clone()
949 let v = u.t.clone(); in test_unalign_copy_clone()
950 let w = u.t; in test_unalign_copy_clone()
951 assert_eq!(u.t.get(), v.get()); in test_unalign_copy_clone()
952 assert_eq!(u.t.get(), w.get()); in test_unalign_copy_clone()
970 fn hash<T: Hash>(t: &T) -> u64 { in test_unalign_trait_impls()
972 t.hash(&mut h); in test_unalign_trait_impls()