Lines Matching refs:T
62 pub struct Ref<B, T: ?Sized>(
67 PhantomData<T>,
70 impl<B, T: ?Sized> Ref<B, T> {
82 pub(crate) unsafe fn new_unchecked(bytes: B) -> Ref<B, T> { in new_unchecked() argument
89 impl<B: ByteSlice, T: ?Sized> Ref<B, T> {
112 impl<B: ByteSliceMut, T: ?Sized> Ref<B, T> {
135 impl<'a, B: IntoByteSlice<'a>, T: ?Sized> Ref<B, T> {
158 impl<'a, B: IntoByteSliceMut<'a>, T: ?Sized> Ref<B, T> {
181 impl<B: CloneableByteSlice + Clone, T: ?Sized> Clone for Ref<B, T> {
183 fn clone(&self) -> Ref<B, T> { in clone() argument
194 impl<B: CopyableByteSlice + Copy, T: ?Sized> Copy for Ref<B, T> {}
205 impl<B, T> Ref<B, T>
210 pub(crate) fn sized_from(bytes: B) -> Result<Ref<B, T>, CastError<B, T>> { in sized_from() argument
211 if bytes.len() != mem::size_of::<T>() { in sized_from()
214 if let Err(err) = util::validate_aligned_to::<_, T>(bytes.deref()) { in sized_from()
223 impl<B, T> Ref<B, T>
228 pub(crate) fn sized_from_prefix(bytes: B) -> Result<(Ref<B, T>, B), CastError<B, T>> { in sized_from_prefix() argument
229 if bytes.len() < mem::size_of::<T>() { in sized_from_prefix()
232 if let Err(err) = util::validate_aligned_to::<_, T>(bytes.deref()) { in sized_from_prefix()
235 let (bytes, suffix) = bytes.split_at(mem::size_of::<T>()).map_err( in sized_from_prefix()
249 pub(crate) fn sized_from_suffix(bytes: B) -> Result<(B, Ref<B, T>), CastError<B, T>> { in sized_from_suffix() argument
251 let split_at = if let Some(split_at) = bytes_len.checked_sub(mem::size_of::<T>()) { in sized_from_suffix()
257 if let Err(err) = util::validate_aligned_to::<_, T>(bytes.deref()) { in sized_from_suffix()
271 impl<B, T> Ref<B, T>
274 T: KnownLayout + Immutable + ?Sized,
311 pub fn from_bytes(source: B) -> Result<Ref<B, T>, CastError<B, T>> { in from_bytes() argument
312 static_assert_dst_is_not_zst!(T); in from_bytes()
314 Ptr::from_ref(source.deref()).try_cast_into_no_leftover::<T, BecauseImmutable>(None) in from_bytes()
323 impl<B, T> Ref<B, T>
326 T: KnownLayout + Immutable + ?Sized,
365 pub fn from_prefix(source: B) -> Result<(Ref<B, T>, B), CastError<B, T>> { in from_prefix() argument
366 static_assert_dst_is_not_zst!(T); in from_prefix()
368 .try_cast_into::<T, BecauseImmutable>(CastType::Prefix, None) in from_prefix()
430 pub fn from_suffix(source: B) -> Result<(B, Ref<B, T>), CastError<B, T>> { in from_suffix() argument
431 static_assert_dst_is_not_zst!(T); in from_suffix()
433 .try_cast_into::<T, BecauseImmutable>(CastType::Suffix, None) in from_suffix()
453 impl<B, T> Ref<B, T>
456 T: KnownLayout<PointerMetadata = usize> + Immutable + ?Sized,
491 pub fn from_bytes_with_elems(source: B, count: usize) -> Result<Ref<B, T>, CastError<B, T>> { in from_bytes_with_elems() argument
492 static_assert_dst_is_not_zst!(T); in from_bytes_with_elems()
493 let expected_len = match T::size_for_metadata(count) { in from_bytes_with_elems()
504 impl<B, T> Ref<B, T>
507 T: KnownLayout<PointerMetadata = usize> + Immutable + ?Sized,
545 ) -> Result<(Ref<B, T>, B), CastError<B, T>> { in from_prefix_with_elems() argument
546 static_assert_dst_is_not_zst!(T); in from_prefix_with_elems()
547 let expected_len = match T::size_for_metadata(count) { in from_prefix_with_elems()
591 ) -> Result<(B, Ref<B, T>), CastError<B, T>> { in from_suffix_with_elems() argument
592 static_assert_dst_is_not_zst!(T); in from_suffix_with_elems()
593 let expected_len = match T::size_for_metadata(count) { in from_suffix_with_elems()
609 impl<'a, B, T> Ref<B, T>
612 T: FromBytes + KnownLayout + Immutable + ?Sized,
623 pub fn into_ref(r: Self) -> &'a T { in into_ref() argument
625 static_assert_dst_is_not_zst!(T); in into_ref()
632 if let crate::layout::SizeInfo::Sized { .. } = T::LAYOUT.size_info { in into_ref()
636 let ptr = unsafe { cast_for_sized::<T, _, _, _>(ptr) }; in into_ref()
650 .try_cast_into_no_leftover::<T, BecauseImmutable>(None) in into_ref()
657 impl<'a, B, T> Ref<B, T>
660 T: FromBytes + IntoBytes + KnownLayout + ?Sized,
671 pub fn into_mut(r: Self) -> &'a mut T { in into_mut() argument
673 static_assert_dst_is_not_zst!(T); in into_mut()
680 if let crate::layout::SizeInfo::Sized { .. } = T::LAYOUT.size_info { in into_mut()
686 T, in into_mut()
705 .try_cast_into_no_leftover::<T, BecauseExclusive>(None) in into_mut()
712 impl<B, T> Ref<B, T>
715 T: ?Sized,
730 impl<B, T> Ref<B, T>
733 T: ?Sized,
748 impl<B, T> Ref<B, T>
751 T: FromBytes,
760 pub fn read(r: &Self) -> T { in read() argument
769 unsafe { ptr::read(b.deref().as_ptr().cast::<T>()) } in read()
773 impl<B, T> Ref<B, T>
776 T: IntoBytes,
784 pub fn write(r: &mut Self, t: T) { in write() argument
794 unsafe { ptr::write(b.deref_mut().as_mut_ptr().cast::<T>(), t) } in write()
798 impl<B, T> Deref for Ref<B, T>
801 T: FromBytes + KnownLayout + Immutable + ?Sized,
803 type Target = T;
805 fn deref(&self) -> &T { in deref() argument
807 static_assert_dst_is_not_zst!(T); in deref()
814 if let crate::layout::SizeInfo::Sized { .. } = T::LAYOUT.size_info { in deref()
818 let ptr = unsafe { cast_for_sized::<T, _, _, _>(ptr) }; in deref()
831 .try_cast_into_no_leftover::<T, BecauseImmutable>(None) in deref()
838 impl<B, T> DerefMut for Ref<B, T>
844 T: FromBytes + IntoBytes + KnownLayout + Immutable + ?Sized,
847 fn deref_mut(&mut self) -> &mut T { in deref_mut() argument
849 static_assert_dst_is_not_zst!(T); in deref_mut()
856 if let crate::layout::SizeInfo::Sized { .. } = T::LAYOUT.size_info { in deref_mut()
862 T, in deref_mut()
881 .try_cast_into_no_leftover::<T, BecauseExclusive>(None) in deref_mut()
888 impl<T, B> Display for Ref<B, T>
891 T: FromBytes + Display + KnownLayout + Immutable + ?Sized,
895 let inner: &T = self; in fmt()
900 impl<T, B> Debug for Ref<B, T>
903 T: FromBytes + Debug + KnownLayout + Immutable + ?Sized,
907 let inner: &T = self; in fmt()
912 impl<T, B> Eq for Ref<B, T>
915 T: FromBytes + Eq + KnownLayout + Immutable + ?Sized,
919 impl<T, B> PartialEq for Ref<B, T>
922 T: FromBytes + PartialEq + KnownLayout + Immutable + ?Sized,
930 impl<T, B> Ord for Ref<B, T>
933 T: FromBytes + Ord + KnownLayout + Immutable + ?Sized,
937 let inner: &T = self; in cmp()
938 let other_inner: &T = other; in cmp()
943 impl<T, B> PartialOrd for Ref<B, T>
946 T: FromBytes + PartialOrd + KnownLayout + Immutable + ?Sized,
950 let inner: &T = self; in partial_cmp()
951 let other_inner: &T = other; in partial_cmp()
960 unsafe fn cast_for_sized<'a, T, A, R, S>( in cast_for_sized() argument
962 ) -> Ptr<'a, T, (A, Unaligned, Valid)> in cast_for_sized()
964 T: FromBytes + KnownLayout + ?Sized, in cast_for_sized()
966 [u8]: MutationCompatible<T, A, Initialized, Initialized, R>, in cast_for_sized() argument
967 T: TransmuteFromPtr<T, A, Initialized, Valid, crate::pointer::cast::IdCast, S>, in cast_for_sized() argument
977 unsafe impl<T: ?Sized + KnownLayout> Project<[u8], T> for CastForSized { in cast_for_sized()
979 fn project(src: PtrInner<'_, [u8]>) -> *mut T { in cast_for_sized() argument
980 T::raw_from_ptr_len( in cast_for_sized()
982 <T::PointerMetadata as crate::PointerMetadata>::from_elem_count(0), in cast_for_sized()
989 unsafe impl<T: ?Sized + KnownLayout> Cast<[u8], T> for CastForSized {} in cast_for_sized()