@@ -59,9 +59,16 @@ mod rotate;
5959mod sort;
6060
6161#[ repr( C ) ]
62- struct Repr < T > {
63- pub data : * const T ,
64- pub len : usize ,
62+ union Repr < ' a , T : ' a > {
63+ rust : & ' a [ T ] ,
64+ rust_mut : & ' a mut [ T ] ,
65+ raw : FatPtr < T > ,
66+ }
67+
68+ #[ repr( C ) ]
69+ struct FatPtr < T > {
70+ data : * const T ,
71+ len : usize ,
6572}
6673
6774//
@@ -119,9 +126,10 @@ impl<T> [T] {
119126 /// ```
120127 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
121128 #[ inline]
122- pub fn len ( & self ) -> usize {
129+ #[ rustc_const_unstable( feature = "const_slice_len" ) ]
130+ pub const fn len ( & self ) -> usize {
123131 unsafe {
124- mem :: transmute :: < & [ T ] , Repr < T > > ( self ) . len
132+ Repr { rust : self } . raw . len
125133 }
126134 }
127135
@@ -135,7 +143,8 @@ impl<T> [T] {
135143 /// ```
136144 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
137145 #[ inline]
138- pub fn is_empty ( & self ) -> bool {
146+ #[ rustc_const_unstable( feature = "const_slice_len" ) ]
147+ pub const fn is_empty ( & self ) -> bool {
139148 self . len ( ) == 0
140149 }
141150
@@ -418,7 +427,8 @@ impl<T> [T] {
418427 /// ```
419428 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
420429 #[ inline]
421- pub fn as_ptr ( & self ) -> * const T {
430+ #[ rustc_const_unstable( feature = "const_slice_as_ptr" ) ]
431+ pub const fn as_ptr ( & self ) -> * const T {
422432 self as * const [ T ] as * const T
423433 }
424434
@@ -3856,8 +3866,8 @@ unsafe impl<'a, T> TrustedRandomAccess for ExactChunksMut<'a, T> {
38563866/// ```
38573867#[ inline]
38583868#[ stable( feature = "rust1" , since = "1.0.0" ) ]
3859- pub unsafe fn from_raw_parts < ' a , T > ( p : * const T , len : usize ) -> & ' a [ T ] {
3860- mem :: transmute ( Repr { data : p , len : len } )
3869+ pub unsafe fn from_raw_parts < ' a , T > ( data : * const T , len : usize ) -> & ' a [ T ] {
3870+ Repr { raw : FatPtr { data , len } } . rust
38613871}
38623872
38633873/// Performs the same functionality as `from_raw_parts`, except that a mutable
@@ -3869,21 +3879,21 @@ pub unsafe fn from_raw_parts<'a, T>(p: *const T, len: usize) -> &'a [T] {
38693879/// `from_raw_parts`.
38703880#[ inline]
38713881#[ stable( feature = "rust1" , since = "1.0.0" ) ]
3872- pub unsafe fn from_raw_parts_mut < ' a , T > ( p : * mut T , len : usize ) -> & ' a mut [ T ] {
3873- mem :: transmute ( Repr { data : p , len : len } )
3882+ pub unsafe fn from_raw_parts_mut < ' a , T > ( data : * mut T , len : usize ) -> & ' a mut [ T ] {
3883+ Repr { raw : FatPtr { data , len} } . rust_mut
38743884}
38753885
38763886/// Converts a reference to T into a slice of length 1 (without copying).
3877- #[ unstable ( feature = "from_ref" , issue = "45703 " ) ]
3887+ #[ stable ( feature = "from_ref" , since = "1.28.0 " ) ]
38783888pub fn from_ref < T > ( s : & T ) -> & [ T ] {
38793889 unsafe {
38803890 from_raw_parts ( s, 1 )
38813891 }
38823892}
38833893
38843894/// Converts a reference to T into a slice of length 1 (without copying).
3885- #[ unstable ( feature = "from_ref" , issue = "45703 " ) ]
3886- pub fn from_ref_mut < T > ( s : & mut T ) -> & mut [ T ] {
3895+ #[ stable ( feature = "from_ref" , since = "1.28.0 " ) ]
3896+ pub fn from_mut < T > ( s : & mut T ) -> & mut [ T ] {
38873897 unsafe {
38883898 from_raw_parts_mut ( s, 1 )
38893899 }
0 commit comments