33
33
// * The `raw` and `bytes` submodules.
34
34
// * Boilerplate trait implementations.
35
35
36
- use mem:: transmute;
37
36
use clone:: Clone ;
38
37
use cmp:: { Ordering , PartialEq , PartialOrd , Eq , Ord } ;
39
38
use cmp:: Ordering :: { Less , Equal , Greater } ;
@@ -148,7 +147,7 @@ macro_rules! slice_ref {
148
147
// Use a non-null pointer value
149
148
& mut * ( 1 as * mut _)
150
149
} else {
151
- transmute( ptr)
150
+ mem :: transmute( ptr)
152
151
}
153
152
} } ;
154
153
}
@@ -261,7 +260,7 @@ impl<T> SliceExt for [T] {
261
260
262
261
#[ inline]
263
262
unsafe fn get_unchecked ( & self , index : usize ) -> & T {
264
- transmute ( self . repr ( ) . data . offset ( index as isize ) )
263
+ & * ( self . repr ( ) . data . offset ( index as isize ) )
265
264
}
266
265
267
266
#[ inline]
@@ -430,7 +429,7 @@ impl<T> SliceExt for [T] {
430
429
431
430
#[ inline]
432
431
unsafe fn get_unchecked_mut ( & mut self , index : usize ) -> & mut T {
433
- transmute ( ( self . repr ( ) . data as * mut T ) . offset ( index as isize ) )
432
+ & mut * ( self . repr ( ) . data as * mut T ) . offset ( index as isize )
434
433
}
435
434
436
435
#[ inline]
@@ -547,8 +546,7 @@ impl<T> ops::Index<usize> for [T] {
547
546
548
547
fn index ( & self , index : usize ) -> & T {
549
548
assert ! ( index < self . len( ) ) ;
550
-
551
- unsafe { mem:: transmute ( self . repr ( ) . data . offset ( index as isize ) ) }
549
+ unsafe { self . get_unchecked ( index) }
552
550
}
553
551
}
554
552
@@ -557,8 +555,7 @@ impl<T> ops::IndexMut<usize> for [T] {
557
555
#[ inline]
558
556
fn index_mut ( & mut self , index : usize ) -> & mut T {
559
557
assert ! ( index < self . len( ) ) ;
560
-
561
- unsafe { mem:: transmute ( self . repr ( ) . data . offset ( index as isize ) ) }
558
+ unsafe { self . get_unchecked_mut ( index) }
562
559
}
563
560
}
564
561
@@ -1427,7 +1424,7 @@ pub fn mut_ref_slice<'a, A>(s: &'a mut A) -> &'a mut [A] {
1427
1424
#[ inline]
1428
1425
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1429
1426
pub unsafe fn from_raw_parts < ' a , T > ( p : * const T , len : usize ) -> & ' a [ T ] {
1430
- transmute ( RawSlice { data : p, len : len } )
1427
+ mem :: transmute ( RawSlice { data : p, len : len } )
1431
1428
}
1432
1429
1433
1430
/// Performs the same functionality as `from_raw_parts`, except that a mutable
@@ -1439,7 +1436,38 @@ pub unsafe fn from_raw_parts<'a, T>(p: *const T, len: usize) -> &'a [T] {
1439
1436
#[ inline]
1440
1437
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1441
1438
pub unsafe fn from_raw_parts_mut < ' a , T > ( p : * mut T , len : usize ) -> & ' a mut [ T ] {
1442
- transmute ( RawSlice { data : p, len : len } )
1439
+ mem:: transmute ( RawSlice { data : p, len : len } )
1440
+ }
1441
+
1442
+ #[ inline]
1443
+ fn check_types < T , U > ( ) {
1444
+ assert ! ( mem:: size_of:: <T >( ) == mem:: size_of:: <U >( ) ) ;
1445
+ assert ! ( mem:: align_of:: <T >( ) % mem:: align_of:: <U >( ) == 0 )
1446
+ }
1447
+
1448
+ /// Reinterprets a slice of one type as a slice of another type.
1449
+ ///
1450
+ /// Both types have to have the same size and the type that is converted to
1451
+ /// must have equal or less restrictive alignment.
1452
+ ///
1453
+ /// # Panics
1454
+ ///
1455
+ /// This functions panics if the above preconditions about the types are not
1456
+ /// met.
1457
+ #[ inline]
1458
+ unsafe fn transmute < T , U > ( slice : & [ T ] ) -> & [ U ] {
1459
+ check_types :: < T , U > ( ) ;
1460
+ from_raw_parts ( slice. as_ptr ( ) as * const U , slice. len ( ) )
1461
+ }
1462
+
1463
+ /// Reinterprets a mutable slice of one type as a mutable slice of another
1464
+ /// type.
1465
+ ///
1466
+ /// Equivalent of `slice::transmute` for mutable slices.
1467
+ #[ inline]
1468
+ unsafe fn transmute_mut < T , U > ( slice : & mut [ T ] ) -> & mut [ U ] {
1469
+ check_types :: < T , U > ( ) ;
1470
+ from_raw_parts_mut ( slice. as_mut_ptr ( ) as * mut U , slice. len ( ) )
1443
1471
}
1444
1472
1445
1473
//
@@ -1580,9 +1608,9 @@ macro_rules! impl_int_slice {
1580
1608
#[ inline]
1581
1609
fn as_signed( & self ) -> & [ $s] { unsafe { transmute( self ) } }
1582
1610
#[ inline]
1583
- fn as_unsigned_mut( & mut self ) -> & mut [ $u] { unsafe { transmute ( self ) } }
1611
+ fn as_unsigned_mut( & mut self ) -> & mut [ $u] { unsafe { transmute_mut ( self ) } }
1584
1612
#[ inline]
1585
- fn as_signed_mut( & mut self ) -> & mut [ $s] { unsafe { transmute ( self ) } }
1613
+ fn as_signed_mut( & mut self ) -> & mut [ $s] { unsafe { transmute_mut ( self ) } }
1586
1614
}
1587
1615
}
1588
1616
}
0 commit comments