@@ -59,9 +59,16 @@ mod rotate;
59
59
mod sort;
60
60
61
61
#[ 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 ,
65
72
}
66
73
67
74
//
@@ -119,9 +126,10 @@ impl<T> [T] {
119
126
/// ```
120
127
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
121
128
#[ inline]
122
- pub fn len ( & self ) -> usize {
129
+ #[ rustc_const_unstable( feature = "const_slice_len" ) ]
130
+ pub const fn len ( & self ) -> usize {
123
131
unsafe {
124
- mem :: transmute :: < & [ T ] , Repr < T > > ( self ) . len
132
+ Repr { rust : self } . raw . len
125
133
}
126
134
}
127
135
@@ -135,7 +143,8 @@ impl<T> [T] {
135
143
/// ```
136
144
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
137
145
#[ inline]
138
- pub fn is_empty ( & self ) -> bool {
146
+ #[ rustc_const_unstable( feature = "const_slice_len" ) ]
147
+ pub const fn is_empty ( & self ) -> bool {
139
148
self . len ( ) == 0
140
149
}
141
150
@@ -418,7 +427,8 @@ impl<T> [T] {
418
427
/// ```
419
428
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
420
429
#[ 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 {
422
432
self as * const [ T ] as * const T
423
433
}
424
434
@@ -3856,8 +3866,8 @@ unsafe impl<'a, T> TrustedRandomAccess for ExactChunksMut<'a, T> {
3856
3866
/// ```
3857
3867
#[ inline]
3858
3868
#[ 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
3861
3871
}
3862
3872
3863
3873
/// 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] {
3869
3879
/// `from_raw_parts`.
3870
3880
#[ inline]
3871
3881
#[ 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
3874
3884
}
3875
3885
3876
3886
/// 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 " ) ]
3878
3888
pub fn from_ref < T > ( s : & T ) -> & [ T ] {
3879
3889
unsafe {
3880
3890
from_raw_parts ( s, 1 )
3881
3891
}
3882
3892
}
3883
3893
3884
3894
/// 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 ] {
3887
3897
unsafe {
3888
3898
from_raw_parts_mut ( s, 1 )
3889
3899
}
0 commit comments