@@ -821,7 +821,7 @@ pub unsafe trait KnownLayout {
821821 let meta = Self :: pointer_to_metadata ( ptr. as_ptr ( ) ) ;
822822 // SAFETY: `size_for_metadata` promises to only return `None` if the
823823 // resulting size would not fit in a `usize`.
824- meta . size_for_metadata ( Self :: LAYOUT )
824+ Self :: size_for_metadata ( meta )
825825 }
826826
827827 #[ doc( hidden) ]
@@ -831,6 +831,37 @@ pub unsafe trait KnownLayout {
831831 let meta = Self :: PointerMetadata :: from_elem_count ( 0 ) ;
832832 Self :: raw_from_ptr_len ( NonNull :: dangling ( ) , meta)
833833 }
834+
835+ /// Computes the size of an object of type `Self` with the given pointer
836+ /// metadata.
837+ ///
838+ /// # Safety
839+ ///
840+ /// `size_for_metadata` promises to return `None` if and only if the
841+ /// resulting size would not fit in a `usize`. Note that the returned size
842+ /// could exceed the actual maximum valid size of an allocated object,
843+ /// `isize::MAX`.
844+ ///
845+ /// # Examples
846+ ///
847+ /// ```
848+ /// use zerocopy::KnownLayout;
849+ ///
850+ /// assert_eq!(u8::size_for_metadata(()), Some(1));
851+ /// assert_eq!(u16::size_for_metadata(()), Some(2));
852+ /// assert_eq!(<[u8]>::size_for_metadata(42), Some(42));
853+ /// assert_eq!(<[u16]>::size_for_metadata(42), Some(84));
854+ ///
855+ /// // This size exceeds the maximum valid object size (`isize::MAX`):
856+ /// assert_eq!(<[u8]>::size_for_metadata(usize::MAX), Some(usize::MAX));
857+ ///
858+ /// // This size, if computed, would exceed `usize::MAX`:
859+ /// assert_eq!(<[u16]>::size_for_metadata(usize::MAX), None);
860+ /// ```
861+ #[ inline( always) ]
862+ fn size_for_metadata ( meta : Self :: PointerMetadata ) -> Option < usize > {
863+ meta. size_for_metadata ( Self :: LAYOUT )
864+ }
834865}
835866
836867/// Efficiently produces the [`TrailingSliceLayout`] of `T`.
0 commit comments