@@ -60,7 +60,7 @@ pub struct Array<'a, T: FromDatum> {
60
60
datum_palloc : Option < NonNull < pg_sys:: Datum > > ,
61
61
elem_slice : & ' a [ pg_sys:: Datum ] ,
62
62
null_slice : NullKind < ' a > ,
63
- elem_layout : Option < Layout > ,
63
+ elem_layout : Layout ,
64
64
_marker : PhantomData < T > ,
65
65
}
66
66
@@ -129,7 +129,7 @@ impl<'a, T: FromDatum> Array<'a, T> {
129
129
unsafe fn deconstruct_from (
130
130
ptr : NonNull < pg_sys:: varlena > ,
131
131
raw : RawArray ,
132
- layout : Layout ,
132
+ elem_layout : Layout ,
133
133
) -> Array < ' a , T > {
134
134
let oid = raw. oid ( ) ;
135
135
let len = raw. len ( ) ;
@@ -153,9 +153,9 @@ impl<'a, T: FromDatum> Array<'a, T> {
153
153
pg_sys:: deconstruct_array (
154
154
array,
155
155
oid,
156
- layout . size . as_typlen ( ) . into ( ) ,
157
- layout . passbyval ,
158
- layout . align . as_typalign ( ) ,
156
+ elem_layout . size . as_typlen ( ) . into ( ) ,
157
+ matches ! ( elem_layout . pass , PassBy :: Value ) ,
158
+ elem_layout . align . as_typalign ( ) ,
159
159
& mut elements,
160
160
& mut nulls,
161
161
& mut nelems,
@@ -195,7 +195,7 @@ impl<'a, T: FromDatum> Array<'a, T> {
195
195
datum_palloc : NonNull :: new ( elements) ,
196
196
elem_slice : /* SAFETY: &[Datum] from palloc'd [Datum] */ unsafe { slice:: from_raw_parts ( elements, nelems) } ,
197
197
null_slice,
198
- elem_layout : Some ( layout ) ,
198
+ elem_layout,
199
199
_marker : PhantomData ,
200
200
}
201
201
}
@@ -217,33 +217,16 @@ impl<'a, T: FromDatum> Array<'a, T> {
217
217
if you are sure your usage is sound, consider RawArray"
218
218
) ]
219
219
pub fn as_slice ( & self ) -> & [ T ] {
220
- if let Some ( Layout { size, passbyval, .. } ) = & self . elem_layout {
221
- if self . null_slice . any ( ) {
222
- panic ! ( "null detected: can't expose potentially uninit data as a slice!" )
223
- }
224
- const DATUM_SIZE : usize = mem:: size_of :: < pg_sys:: Datum > ( ) ;
225
- let sizeof_type = match ( passbyval, mem:: size_of :: < T > ( ) , size. try_as_usize ( ) ) {
226
- ( true , rs @ ( 1 | 2 | 4 | 8 ) , Some ( pg @ ( 1 | 2 | 4 | 8 ) ) ) if rs == pg => rs,
227
- ( true , _, _) => panic ! ( "invalid sizes for pass-by-value datum" ) ,
228
- ( false , DATUM_SIZE , _) => DATUM_SIZE ,
229
- ( false , _, _) => panic ! ( "invalid sizes for pass-by-reference datum" ) ,
230
- } ;
231
- match ( sizeof_type, self . raw . as_ref ( ) ) {
232
- // SAFETY: Rust slice layout matches Postgres data layout and this array is "owned"
233
- ( 1 | 2 | 4 , Some ( raw) ) => unsafe { raw. assume_init_data_slice :: < T > ( ) } ,
234
- ( DATUM_SIZE , _) => {
235
- let sizeof_datums = mem:: size_of_val ( self . elem_slice ) ;
236
- unsafe {
237
- slice:: from_raw_parts (
238
- self . elem_slice . as_ptr ( ) as * const T ,
239
- sizeof_datums / sizeof_type,
240
- )
241
- }
242
- }
243
- ( _, _) => panic ! ( "no correctly-sized slice exists" ) ,
244
- }
245
- } else {
246
- panic ! ( "not enough type information to slice correctly" )
220
+ const DATUM_SIZE : usize = mem:: size_of :: < pg_sys:: Datum > ( ) ;
221
+ if self . null_slice . any ( ) {
222
+ panic ! ( "null detected: can't expose potentially uninit data as a slice!" )
223
+ }
224
+ match ( self . elem_layout . size_matches :: < T > ( ) , self . raw . as_ref ( ) ) {
225
+ // SAFETY: Rust slice layout matches Postgres data layout and this array is "owned"
226
+ ( Some ( 1 | 2 | 4 | DATUM_SIZE ) , Some ( raw) ) => unsafe {
227
+ raw. assume_init_data_slice :: < T > ( )
228
+ } ,
229
+ ( _, _) => panic ! ( "no correctly-sized slice exists" ) ,
247
230
}
248
231
}
249
232
0 commit comments