@@ -45,16 +45,12 @@ where S: Data
45
45
46
46
fn deref ( & self ) -> & Self :: Target
47
47
{
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
51
51
// - 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 > > ( ) }
58
54
}
59
55
}
60
56
@@ -67,16 +63,12 @@ where
67
63
fn deref_mut ( & mut self ) -> & mut Self :: Target
68
64
{
69
65
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
73
69
// - 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 > > ( ) }
80
72
}
81
73
}
82
74
@@ -87,12 +79,12 @@ impl<A, D> Deref for ArrayRef<A, D>
87
79
88
80
fn deref ( & self ) -> & Self :: Target
89
81
{
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 > > ( ) }
96
88
}
97
89
}
98
90
@@ -101,12 +93,12 @@ impl<A, D> DerefMut for ArrayRef<A, D>
101
93
{
102
94
fn deref_mut ( & mut self ) -> & mut Self :: Target
103
95
{
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 > > ( ) }
110
102
}
111
103
}
112
104
@@ -136,12 +128,12 @@ where S: RawData<Elem = A>
136
128
{
137
129
fn as_ref ( & self ) -> & RawRef < A , D >
138
130
{
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 > > ( ) }
145
137
}
146
138
}
147
139
@@ -151,12 +143,12 @@ where S: RawDataMut<Elem = A>
151
143
{
152
144
fn as_mut ( & mut self ) -> & mut RawRef < A , D >
153
145
{
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 > > ( ) }
160
152
}
161
153
}
162
154
0 commit comments