@@ -303,6 +303,54 @@ pub fn size_of_val<T: ?Sized>(val: &T) -> usize {
303
303
intrinsics:: size_of_val ( val)
304
304
}
305
305
306
+ /// Returns the size of the pointed-to value in bytes.
307
+ ///
308
+ /// This is usually the same as `size_of::<T>()`. However, when `T` *has* no
309
+ /// statically-known size, e.g., a slice [`[T]`][slice] or a [trait object],
310
+ /// then `size_of_val_raw` can be used to get the dynamically-known size.
311
+ ///
312
+ /// # Safety
313
+ ///
314
+ /// This function is only safe to call if the following conditions hold:
315
+ ///
316
+ /// - If `T` is `Sized`, this function is always safe to call.
317
+ /// - If the unsized tail of `T` is:
318
+ /// - a [slice], then the length of the slice tail must be an intialized
319
+ /// integer, and the size of the *entire value*
320
+ /// (dynamic tail length + statically sized prefix) must fit in `isize`.
321
+ /// - a [trait object], then the vtable part of the pointer must point
322
+ /// to a valid vtable acquired by an unsizing coersion, and the size
323
+ /// of the *entire value* (dynamic tail length + statically sized prefix)
324
+ /// must fit in `isize`.
325
+ /// - an (unstable) [extern type], then this function is always safe to
326
+ /// call, but may panic or otherwise return the wrong value, as the
327
+ /// extern type's layout is not known. This is the same behavior as
328
+ /// [`size_of_val`] on a reference to an extern type tail.
329
+ /// - otherwise, it is conservatively not allowed to call this function.
330
+ ///
331
+ /// [slice]: ../../std/primitive.slice.html
332
+ /// [trait object]: ../../book/ch17-02-trait-objects.html
333
+ /// [extern type]: ../../unstable-book/language-features/extern-types.html
334
+ ///
335
+ /// # Examples
336
+ ///
337
+ /// ```
338
+ /// #![feature(layout_for_ptr)]
339
+ /// use std::mem;
340
+ ///
341
+ /// assert_eq!(4, mem::size_of_val(&5i32));
342
+ ///
343
+ /// let x: [u8; 13] = [0; 13];
344
+ /// let y: &[u8] = &x;
345
+ /// assert_eq!(13, unsafe { mem::size_of_val_raw(y) });
346
+ /// ```
347
+ #[ inline]
348
+ #[ cfg( not( bootstrap) ) ]
349
+ #[ unstable( feature = "layout_for_ptr" , issue = "69835" ) ]
350
+ pub unsafe fn size_of_val_raw < T : ?Sized > ( val : * const T ) -> usize {
351
+ intrinsics:: size_of_val ( val)
352
+ }
353
+
306
354
/// Returns the [ABI]-required minimum alignment of a type.
307
355
///
308
356
/// Every reference to a value of the type `T` must be a multiple of this number.
@@ -390,6 +438,50 @@ pub fn align_of_val<T: ?Sized>(val: &T) -> usize {
390
438
min_align_of_val ( val)
391
439
}
392
440
441
+ /// Returns the [ABI]-required minimum alignment of the type of the value that `val` points to.
442
+ ///
443
+ /// Every reference to a value of the type `T` must be a multiple of this number.
444
+ ///
445
+ /// [ABI]: https://en.wikipedia.org/wiki/Application_binary_interface
446
+ ///
447
+ /// # Safety
448
+ ///
449
+ /// This function is only safe to call if the following conditions hold:
450
+ ///
451
+ /// - If `T` is `Sized`, this function is always safe to call.
452
+ /// - If the unsized tail of `T` is:
453
+ /// - a [slice], then the length of the slice tail must be an intialized
454
+ /// integer, and the size of the *entire value*
455
+ /// (dynamic tail length + statically sized prefix) must fit in `isize`.
456
+ /// - a [trait object], then the vtable part of the pointer must point
457
+ /// to a valid vtable acquired by an unsizing coersion, and the size
458
+ /// of the *entire value* (dynamic tail length + statically sized prefix)
459
+ /// must fit in `isize`.
460
+ /// - an (unstable) [extern type], then this function is always safe to
461
+ /// call, but may panic or otherwise return the wrong value, as the
462
+ /// extern type's layout is not known. This is the same behavior as
463
+ /// [`align_of_val`] on a reference to an extern type tail.
464
+ /// - otherwise, it is conservatively not allowed to call this function.
465
+ ///
466
+ /// [slice]: ../../std/primitive.slice.html
467
+ /// [trait object]: ../../book/ch17-02-trait-objects.html
468
+ /// [extern type]: ../../unstable-book/language-features/extern-types.html
469
+ ///
470
+ /// # Examples
471
+ ///
472
+ /// ```
473
+ /// #![feature(layout_for_ptr)]
474
+ /// use std::mem;
475
+ ///
476
+ /// assert_eq!(4, unsafe { mem::align_of_val_raw(&5i32) });
477
+ /// ```
478
+ #[ inline]
479
+ #[ cfg( not( bootstrap) ) ]
480
+ #[ unstable( feature = "layout_for_ptr" , issue = "69835" ) ]
481
+ pub unsafe fn align_of_val_raw < T : ?Sized > ( val : * const T ) -> usize {
482
+ intrinsics:: min_align_of_val ( val)
483
+ }
484
+
393
485
/// Returns `true` if dropping values of type `T` matters.
394
486
///
395
487
/// This is purely an optimization hint, and may be implemented conservatively:
0 commit comments