@@ -45,16 +45,12 @@ where S: Data
4545
4646 fn deref ( & self ) -> & Self :: Target
4747 {
48- // SAFETY: The pointer will hold all the guarantees of `as_ref`:
49- // - The pointer is aligned because neither type use repr(align)
50- // - It is "dereferencable" because it just points to self
48+ // SAFETY:
49+ // - The pointer is aligned because neither type uses repr(align)
50+ // - It is "dereferencable" because it comes from a reference
5151 // - For the same reason, it is initialized
52- unsafe {
53- ( & self . layout as * const LayoutRef < S :: Elem , D > )
54- . cast :: < ArrayRef < S :: Elem , D > > ( )
55- . as_ref ( )
56- }
57- . expect ( "References are always non-null" )
52+ // - The cast is valid because ArrayRef uses #[repr(transparent)]
53+ unsafe { & * ( & self . layout as * const LayoutRef < S :: Elem , D > ) . cast :: < ArrayRef < S :: Elem , D > > ( ) }
5854 }
5955}
6056
@@ -67,16 +63,12 @@ where
6763 fn deref_mut ( & mut self ) -> & mut Self :: Target
6864 {
6965 self . ensure_unique ( ) ;
70- // SAFETY: The pointer will hold all the guarantees of `as_ref`:
71- // - The pointer is aligned because neither type use repr(align)
72- // - It is "dereferencable" because it just points to self
66+ // SAFETY:
67+ // - The pointer is aligned because neither type uses repr(align)
68+ // - It is "dereferencable" because it comes from a reference
7369 // - For the same reason, it is initialized
74- unsafe {
75- ( & mut self . layout as * mut LayoutRef < S :: Elem , D > )
76- . cast :: < ArrayRef < S :: Elem , D > > ( )
77- . as_mut ( )
78- }
79- . expect ( "References are always non-null" )
70+ // - The cast is valid because ArrayRef uses #[repr(transparent)]
71+ unsafe { & mut * ( & mut self . layout as * mut LayoutRef < S :: Elem , D > ) . cast :: < ArrayRef < S :: Elem , D > > ( ) }
8072 }
8173}
8274
@@ -87,12 +79,12 @@ impl<A, D> Deref for ArrayRef<A, D>
8779
8880 fn deref ( & self ) -> & Self :: Target
8981 {
90- unsafe {
91- ( self as * const ArrayRef < A , D > )
92- . cast :: < RawRef < A , D > > ( )
93- . as_ref ( )
94- }
95- . expect ( "References are always non-null" )
82+ // SAFETY:
83+ // - The pointer is aligned because neither type uses repr(align )
84+ // - It is "dereferencable" because it comes from a reference
85+ // - For the same reason, it is initialized
86+ // - The cast is valid because ArrayRef uses #[repr(transparent)]
87+ unsafe { & * ( self as * const ArrayRef < A , D > ) . cast :: < RawRef < A , D > > ( ) }
9688 }
9789}
9890
@@ -101,12 +93,12 @@ impl<A, D> DerefMut for ArrayRef<A, D>
10193{
10294 fn deref_mut ( & mut self ) -> & mut Self :: Target
10395 {
104- unsafe {
105- ( self as * mut ArrayRef < A , D > )
106- . cast :: < RawRef < A , D > > ( )
107- . as_mut ( )
108- }
109- . expect ( "References are always non-null" )
96+ // SAFETY:
97+ // - The pointer is aligned because neither type uses repr(align )
98+ // - It is "dereferencable" because it comes from a reference
99+ // - For the same reason, it is initialized
100+ // - The cast is valid because ArrayRef uses #[repr(transparent)]
101+ unsafe { & mut * ( self as * mut ArrayRef < A , D > ) . cast :: < RawRef < A , D > > ( ) }
110102 }
111103}
112104
@@ -136,12 +128,12 @@ where S: RawData<Elem = A>
136128{
137129 fn as_ref ( & self ) -> & RawRef < A , D >
138130 {
139- unsafe {
140- ( & self . layout as * const LayoutRef < A , D > )
141- . cast :: < RawRef < A , D > > ( )
142- . as_ref ( )
143- }
144- . expect ( "References are always non-null" )
131+ // SAFETY:
132+ // - The pointer is aligned because neither type uses repr(align )
133+ // - It is "dereferencable" because it comes from a reference
134+ // - For the same reason, it is initialized
135+ // - The cast is valid because ArrayRef uses #[repr(transparent)]
136+ unsafe { & * ( & self . layout as * const LayoutRef < A , D > ) . cast :: < RawRef < A , D > > ( ) }
145137 }
146138}
147139
@@ -151,12 +143,12 @@ where S: RawDataMut<Elem = A>
151143{
152144 fn as_mut ( & mut self ) -> & mut RawRef < A , D >
153145 {
154- unsafe {
155- ( & mut self . layout as * mut LayoutRef < A , D > )
156- . cast :: < RawRef < A , D > > ( )
157- . as_mut ( )
158- }
159- . expect ( "References are always non-null" )
146+ // SAFETY:
147+ // - The pointer is aligned because neither type uses repr(align )
148+ // - It is "dereferencable" because it comes from a reference
149+ // - For the same reason, it is initialized
150+ // - The cast is valid because ArrayRef uses #[repr(transparent)]
151+ unsafe { & mut * ( & mut self . layout as * mut LayoutRef < A , D > ) . cast :: < RawRef < A , D > > ( ) }
160152 }
161153}
162154
0 commit comments