|
6 | 6 | extern_types, |
7 | 7 | decl_macro, |
8 | 8 | rustc_attrs, |
| 9 | + rustc_private, |
9 | 10 | transparent_unions, |
10 | 11 | auto_traits, |
11 | 12 | freeze_impls, |
@@ -596,7 +597,7 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {} |
596 | 597 | impl<T> Box<T> { |
597 | 598 | pub fn new(val: T) -> Box<T> { |
598 | 599 | unsafe { |
599 | | - let size = intrinsics::size_of::<T>(); |
| 600 | + let size = size_of::<T>(); |
600 | 601 | let ptr = libc::malloc(size); |
601 | 602 | intrinsics::copy(&val as *const T as *const u8, ptr, size); |
602 | 603 | Box(Unique { pointer: NonNull(ptr as *const T), _marker: PhantomData }, Global) |
@@ -648,11 +649,11 @@ pub mod intrinsics { |
648 | 649 | #[rustc_intrinsic] |
649 | 650 | pub fn abort() -> !; |
650 | 651 | #[rustc_intrinsic] |
651 | | - pub fn size_of<T>() -> usize; |
| 652 | + pub const fn size_of<T>() -> usize; |
652 | 653 | #[rustc_intrinsic] |
653 | 654 | pub unsafe fn size_of_val<T: ?crate::Sized>(val: *const T) -> usize; |
654 | 655 | #[rustc_intrinsic] |
655 | | - pub fn align_of<T>() -> usize; |
| 656 | + pub const fn align_of<T>() -> usize; |
656 | 657 | #[rustc_intrinsic] |
657 | 658 | pub unsafe fn align_of_val<T: ?crate::Sized>(val: *const T) -> usize; |
658 | 659 | #[rustc_intrinsic] |
@@ -717,6 +718,23 @@ impl<T> Index<usize> for [T] { |
717 | 718 | } |
718 | 719 | } |
719 | 720 |
|
| 721 | +pub const fn size_of<T>() -> usize { |
| 722 | + <T as SizedTypeProperties>::SIZE |
| 723 | +} |
| 724 | + |
| 725 | +pub const fn align_of<T>() -> usize { |
| 726 | + <T as SizedTypeProperties>::ALIGN |
| 727 | +} |
| 728 | + |
| 729 | +trait SizedTypeProperties: Sized { |
| 730 | + #[lang = "mem_size_const"] |
| 731 | + const SIZE: usize = intrinsics::size_of::<Self>(); |
| 732 | + |
| 733 | + #[lang = "mem_align_const"] |
| 734 | + const ALIGN: usize = intrinsics::align_of::<Self>(); |
| 735 | +} |
| 736 | +impl<T> SizedTypeProperties for T {} |
| 737 | + |
720 | 738 | unsafe extern "C" { |
721 | 739 | type VaListImpl; |
722 | 740 | } |
|
0 commit comments