@@ -336,6 +336,54 @@ pub fn size_of_val<T: ?Sized>(val: &T) -> usize {
336336 intrinsics:: size_of_val ( val)
337337}
338338
339+ /// Returns the size of the pointed-to value in bytes.
340+ ///
341+ /// This is usually the same as `size_of::<T>()`. However, when `T` *has* no
342+ /// statically-known size, e.g., a slice [`[T]`][slice] or a [trait object],
343+ /// then `size_of_val_raw` can be used to get the dynamically-known size.
344+ ///
345+ /// # Safety
346+ ///
347+ /// This function is only safe to call if the following conditions hold:
348+ ///
349+ /// - If `T` is `Sized`, this function is always safe to call.
350+ /// - If the unsized tail of `T` is:
351+ /// - a [slice], then the length of the slice tail must be an intialized
352+ /// integer, and the size of the *entire value*
353+ /// (dynamic tail length + statically sized prefix) must fit in `isize`.
354+ /// - a [trait object], then the vtable part of the pointer must point
355+ /// to a valid vtable acquired by an unsizing coersion, and the size
356+ /// of the *entire value* (dynamic tail length + statically sized prefix)
357+ /// must fit in `isize`.
358+ /// - an (unstable) [extern type], then this function is always safe to
359+ /// call, but may panic or otherwise return the wrong value, as the
360+ /// extern type's layout is not known. This is the same behavior as
361+ /// [`size_of_val`] on a reference to an extern type tail.
362+ /// - otherwise, it is conservatively not allowed to call this function.
363+ ///
364+ /// [slice]: ../../std/primitive.slice.html
365+ /// [trait object]: ../../book/ch17-02-trait-objects.html
366+ /// [extern type]: ../../unstable-book/language-features/extern-types.html
367+ ///
368+ /// # Examples
369+ ///
370+ /// ```
371+ /// #![feature(layout_for_ptr)]
372+ /// use std::mem;
373+ ///
374+ /// assert_eq!(4, mem::size_of_val(&5i32));
375+ ///
376+ /// let x: [u8; 13] = [0; 13];
377+ /// let y: &[u8] = &x;
378+ /// assert_eq!(13, unsafe { mem::size_of_val_raw(y) });
379+ /// ```
380+ #[ inline]
381+ #[ cfg( not( bootstrap) ) ]
382+ #[ unstable( feature = "layout_for_ptr" , issue = "69835" ) ]
383+ pub unsafe fn size_of_val_raw < T : ?Sized > ( val : * const T ) -> usize {
384+ intrinsics:: size_of_val ( val)
385+ }
386+
339387/// Returns the [ABI]-required minimum alignment of a type.
340388///
341389/// Every reference to a value of the type `T` must be a multiple of this number.
@@ -423,6 +471,50 @@ pub fn align_of_val<T: ?Sized>(val: &T) -> usize {
423471 min_align_of_val ( val)
424472}
425473
474+ /// Returns the [ABI]-required minimum alignment of the type of the value that `val` points to.
475+ ///
476+ /// Every reference to a value of the type `T` must be a multiple of this number.
477+ ///
478+ /// [ABI]: https://en.wikipedia.org/wiki/Application_binary_interface
479+ ///
480+ /// # Safety
481+ ///
482+ /// This function is only safe to call if the following conditions hold:
483+ ///
484+ /// - If `T` is `Sized`, this function is always safe to call.
485+ /// - If the unsized tail of `T` is:
486+ /// - a [slice], then the length of the slice tail must be an intialized
487+ /// integer, and the size of the *entire value*
488+ /// (dynamic tail length + statically sized prefix) must fit in `isize`.
489+ /// - a [trait object], then the vtable part of the pointer must point
490+ /// to a valid vtable acquired by an unsizing coersion, and the size
491+ /// of the *entire value* (dynamic tail length + statically sized prefix)
492+ /// must fit in `isize`.
493+ /// - an (unstable) [extern type], then this function is always safe to
494+ /// call, but may panic or otherwise return the wrong value, as the
495+ /// extern type's layout is not known. This is the same behavior as
496+ /// [`align_of_val`] on a reference to an extern type tail.
497+ /// - otherwise, it is conservatively not allowed to call this function.
498+ ///
499+ /// [slice]: ../../std/primitive.slice.html
500+ /// [trait object]: ../../book/ch17-02-trait-objects.html
501+ /// [extern type]: ../../unstable-book/language-features/extern-types.html
502+ ///
503+ /// # Examples
504+ ///
505+ /// ```
506+ /// #![feature(layout_for_ptr)]
507+ /// use std::mem;
508+ ///
509+ /// assert_eq!(4, unsafe { mem::align_of_val_raw(&5i32) });
510+ /// ```
511+ #[ inline]
512+ #[ cfg( not( bootstrap) ) ]
513+ #[ unstable( feature = "layout_for_ptr" , issue = "69835" ) ]
514+ pub unsafe fn align_of_val_raw < T : ?Sized > ( val : * const T ) -> usize {
515+ intrinsics:: min_align_of_val ( val)
516+ }
517+
426518/// Returns `true` if dropping values of type `T` matters.
427519///
428520/// This is purely an optimization hint, and may be implemented conservatively:
0 commit comments