@@ -43,7 +43,6 @@ use std::cell::RefCell;
43
43
use std:: cmp:: { self , Ordering } ;
44
44
use std:: fmt;
45
45
use std:: hash:: { Hash , Hasher } ;
46
- use std:: marker:: PhantomData ;
47
46
use std:: ops:: Deref ;
48
47
use rustc_data_structures:: sync:: Lrc ;
49
48
use std:: slice;
@@ -592,20 +591,13 @@ extern {
592
591
/// A wrapper for slices with the additional invariant
593
592
/// that the slice is interned and no other slice with
594
593
/// the same contents can exist in the same context.
595
- /// This means we can use pointer + length for both
594
+ /// This means we can use pointer for both
596
595
/// equality comparisons and hashing.
597
- pub struct Slice < T > ( PhantomData < T > , OpaqueSliceContents ) ;
598
-
599
- impl < T > Slice < T > {
600
- /// Returns the offset of the array
601
- #[ inline( always) ]
602
- fn offset ( ) -> usize {
603
- // Align up the size of the len (usize) field
604
- let align = mem:: align_of :: < T > ( ) ;
605
- let align_mask = align - 1 ;
606
- let offset = mem:: size_of :: < usize > ( ) ;
607
- ( offset + align_mask) & !align_mask
608
- }
596
+ #[ repr( C ) ]
597
+ pub struct Slice < T > {
598
+ len : usize ,
599
+ data : [ T ; 0 ] ,
600
+ opaque : OpaqueSliceContents ,
609
601
}
610
602
611
603
impl < T : Copy > Slice < T > {
@@ -615,24 +607,27 @@ impl<T: Copy> Slice<T> {
615
607
assert ! ( mem:: size_of:: <T >( ) != 0 ) ;
616
608
assert ! ( slice. len( ) != 0 ) ;
617
609
618
- let offset = Slice :: < T > :: offset ( ) ;
610
+ // Align up the size of the len (usize) field
611
+ let align = mem:: align_of :: < T > ( ) ;
612
+ let align_mask = align - 1 ;
613
+ let offset = mem:: size_of :: < usize > ( ) ;
614
+ let offset = ( offset + align_mask) & !align_mask;
615
+
619
616
let size = offset + slice. len ( ) * mem:: size_of :: < T > ( ) ;
620
617
621
- let mem: * mut u8 = arena. alloc_raw (
618
+ let mem = arena. alloc_raw (
622
619
size,
623
- cmp:: max ( mem:: align_of :: < T > ( ) , mem:: align_of :: < usize > ( ) ) ) . as_mut_ptr ( ) ;
624
-
620
+ cmp:: max ( mem:: align_of :: < T > ( ) , mem:: align_of :: < usize > ( ) ) ) ;
625
621
unsafe {
622
+ let result = & mut * ( mem. as_mut_ptr ( ) as * mut Slice < T > ) ;
626
623
// Write the length
627
- * ( mem as * mut usize ) = slice. len ( ) ;
624
+ result . len = slice. len ( ) ;
628
625
629
626
// Write the elements
630
- let arena_slice = slice:: from_raw_parts_mut (
631
- mem. offset ( offset as isize ) as * mut T ,
632
- slice. len ( ) ) ;
627
+ let arena_slice = slice:: from_raw_parts_mut ( result. data . as_mut_ptr ( ) , result. len ) ;
633
628
arena_slice. copy_from_slice ( slice) ;
634
629
635
- & * ( mem as * const Slice < T > )
630
+ result
636
631
}
637
632
}
638
633
}
@@ -686,10 +681,7 @@ impl<T> Deref for Slice<T> {
686
681
#[ inline( always) ]
687
682
fn deref ( & self ) -> & [ T ] {
688
683
unsafe {
689
- let raw = self as * const _ as * const u8 ;
690
- let len = * ( raw as * const usize ) ;
691
- let slice = raw. offset ( Slice :: < T > :: offset ( ) as isize ) ;
692
- slice:: from_raw_parts ( slice as * const T , len)
684
+ slice:: from_raw_parts ( self . data . as_ptr ( ) , self . len )
693
685
}
694
686
}
695
687
}
0 commit comments