@@ -275,9 +275,9 @@ where
275
275
{
276
276
/// Constructs a new `Ref` from a byte slice.
277
277
///
278
- /// `from ` verifies that `bytes.len() == size_of::<T>()` and that `bytes` is
279
- /// aligned to `align_of::<T>()`, and constructs a new `Ref`. If either of
280
- /// these checks fail, it returns `None`.
278
+ /// `from_bytes ` verifies that `bytes.len() == size_of::<T>()` and that
279
+ /// `bytes` is aligned to `align_of::<T>()`, and constructs a new `Ref`. If
280
+ /// either of these checks fail, it returns `None`.
281
281
///
282
282
/// # Compile-Time Assertions
283
283
///
@@ -296,11 +296,11 @@ where
296
296
/// trailing_dst: [()],
297
297
/// }
298
298
///
299
- /// let _ = Ref::<_, ZSTy>::from (&b"UU"[..]); // ⚠ Compile Error!
299
+ /// let _ = Ref::<_, ZSTy>::from_bytes (&b"UU"[..]); // ⚠ Compile Error!
300
300
/// ```
301
301
#[ must_use = "has no side effects" ]
302
302
#[ inline]
303
- pub fn from ( bytes : B ) -> Result < Ref < B , T > , CastError < B , T > > {
303
+ pub fn from_bytes ( bytes : B ) -> Result < Ref < B , T > , CastError < B , T > > {
304
304
util:: assert_dst_is_not_zst :: < T > ( ) ;
305
305
if let Err ( e) =
306
306
Ptr :: from_ref ( bytes. deref ( ) ) . try_cast_into_no_leftover :: < T , BecauseImmutable > ( None )
@@ -429,6 +429,28 @@ where
429
429
}
430
430
}
431
431
432
+ impl < B , T > Ref < B , T >
433
+ where
434
+ B : ByteSlice ,
435
+ T : KnownLayout < PointerMetadata = usize > + Immutable + ?Sized ,
436
+ {
437
+ // TODO(#29), TODO(#871): Pick a name and make this public. Make sure to
438
+ // update references to this name in `#[deprecated]` attributes elsewhere.
439
+ #[ doc( hidden) ]
440
+ #[ inline]
441
+ pub fn from_bytes_with_elems ( bytes : B , count : usize ) -> Result < Ref < B , T > , CastError < B , T > > {
442
+ util:: assert_dst_is_not_zst :: < T > ( ) ;
443
+ let expected_len = match count. size_for_metadata ( T :: LAYOUT ) {
444
+ Some ( len) => len,
445
+ None => return Err ( SizeError :: new ( bytes) . into ( ) ) ,
446
+ } ;
447
+ if bytes. len ( ) != expected_len {
448
+ return Err ( SizeError :: new ( bytes) . into ( ) ) ;
449
+ }
450
+ Self :: from_bytes ( bytes)
451
+ }
452
+ }
453
+
432
454
impl < B , T > Ref < B , T >
433
455
where
434
456
B : SplitByteSlice ,
@@ -451,7 +473,7 @@ where
451
473
return Err ( SizeError :: new ( bytes) . into ( ) ) ;
452
474
}
453
475
let ( prefix, bytes) = bytes. split_at ( expected_len) ;
454
- Self :: from ( prefix) . map ( move |l| ( l, bytes) )
476
+ Self :: from_bytes ( prefix) . map ( move |l| ( l, bytes) )
455
477
}
456
478
}
457
479
@@ -479,7 +501,7 @@ where
479
501
return Err ( SizeError :: new ( bytes) . into ( ) ) ;
480
502
} ;
481
503
let ( bytes, suffix) = bytes. split_at ( split_at) ;
482
- Self :: from ( suffix) . map ( move |l| ( bytes, l) )
504
+ Self :: from_bytes ( suffix) . map ( move |l| ( bytes, l) )
483
505
}
484
506
}
485
507
@@ -517,7 +539,7 @@ where
517
539
#[ inline( always) ]
518
540
pub fn unaligned_from ( bytes : B ) -> Result < Ref < B , T > , SizeError < B , T > > {
519
541
util:: assert_dst_is_not_zst :: < T > ( ) ;
520
- match Ref :: from ( bytes) {
542
+ match Ref :: from_bytes ( bytes) {
521
543
Ok ( dst) => Ok ( dst) ,
522
544
Err ( CastError :: Size ( e) ) => Err ( e) ,
523
545
Err ( CastError :: Alignment ( _) ) => unreachable ! ( ) ,
@@ -608,6 +630,28 @@ where
608
630
}
609
631
}
610
632
633
+ impl < B , T > Ref < B , T >
634
+ where
635
+ B : ByteSlice ,
636
+ T : KnownLayout < PointerMetadata = usize > + Unaligned + Immutable + ?Sized ,
637
+ {
638
+ // TODO(#29), TODO(#871): Pick a name and make this public. Make sure to
639
+ // update references to this name in `#[deprecated]` attributes elsewhere.
640
+ #[ doc( hidden) ]
641
+ #[ inline]
642
+ pub fn unaligned_from_bytes_with_elems (
643
+ bytes : B ,
644
+ count : usize ,
645
+ ) -> Result < Ref < B , T > , SizeError < B , T > > {
646
+ util:: assert_dst_is_not_zst :: < T > ( ) ;
647
+ Self :: from_bytes_with_elems ( bytes, count) . map_err ( |e| match e {
648
+ CastError :: Size ( e) => e,
649
+ CastError :: Alignment ( _) => unreachable ! ( ) ,
650
+ CastError :: Validity ( i) => match i { } ,
651
+ } )
652
+ }
653
+ }
654
+
611
655
impl < B , T > Ref < B , T >
612
656
where
613
657
B : SplitByteSlice ,
@@ -917,13 +961,13 @@ mod tests {
917
961
// reference which points to the right region of memory.
918
962
919
963
let buf = [ 0 ] ;
920
- let r = Ref :: < _ , u8 > :: from ( & buf[ ..] ) . unwrap ( ) ;
964
+ let r = Ref :: < _ , u8 > :: from_bytes ( & buf[ ..] ) . unwrap ( ) ;
921
965
let buf_ptr = buf. as_ptr ( ) ;
922
966
let deref_ptr: * const u8 = r. deref ( ) ;
923
967
assert_eq ! ( buf_ptr, deref_ptr) ;
924
968
925
969
let buf = [ 0 ] ;
926
- let r = Ref :: < _ , [ u8 ] > :: from ( & buf[ ..] ) . unwrap ( ) ;
970
+ let r = Ref :: < _ , [ u8 ] > :: from_bytes ( & buf[ ..] ) . unwrap ( ) ;
927
971
let buf_ptr = buf. as_ptr ( ) ;
928
972
let deref_ptr = r. deref ( ) . as_ptr ( ) ;
929
973
assert_eq ! ( buf_ptr, deref_ptr) ;
@@ -1045,7 +1089,7 @@ mod tests {
1045
1089
// A buffer with an alignment of 8.
1046
1090
let mut buf = Align :: < [ u8 ; 8 ] , AU64 > :: default ( ) ;
1047
1091
// `buf.t` should be aligned to 8, so this should always succeed.
1048
- test_new_helper ( Ref :: < _ , AU64 > :: from ( & mut buf. t [ ..] ) . unwrap ( ) ) ;
1092
+ test_new_helper ( Ref :: < _ , AU64 > :: from_bytes ( & mut buf. t [ ..] ) . unwrap ( ) ) ;
1049
1093
{
1050
1094
// In a block so that `r` and `suffix` don't live too long.
1051
1095
buf. set_default ( ) ;
@@ -1068,7 +1112,7 @@ mod tests {
1068
1112
let mut buf = Align :: < [ u8 ; 24 ] , AU64 > :: default ( ) ;
1069
1113
// `buf.t` should be aligned to 8 and have a length which is a multiple
1070
1114
// of `size_of::<AU64>()`, so this should always succeed.
1071
- test_new_helper_slice ( Ref :: < _ , [ AU64 ] > :: from ( & mut buf. t [ ..] ) . unwrap ( ) , 3 ) ;
1115
+ test_new_helper_slice ( Ref :: < _ , [ AU64 ] > :: from_bytes ( & mut buf. t [ ..] ) . unwrap ( ) , 3 ) ;
1072
1116
let ascending: [ u8 ; 24 ] = ( 0 ..24 ) . collect :: < Vec < _ > > ( ) . try_into ( ) . unwrap ( ) ;
1073
1117
// 16 ascending bytes followed by 8 zeros.
1074
1118
let mut ascending_prefix = ascending;
@@ -1224,15 +1268,15 @@ mod tests {
1224
1268
// A buffer with an alignment of 8.
1225
1269
let buf = Align :: < [ u8 ; 16 ] , AU64 > :: default ( ) ;
1226
1270
// `buf.t` should be aligned to 8, so only the length check should fail.
1227
- assert ! ( Ref :: <_, AU64 >:: from ( & buf. t[ ..] ) . is_err( ) ) ;
1271
+ assert ! ( Ref :: <_, AU64 >:: from_bytes ( & buf. t[ ..] ) . is_err( ) ) ;
1228
1272
assert ! ( Ref :: <_, [ u8 ; 8 ] >:: unaligned_from( & buf. t[ ..] ) . is_err( ) ) ;
1229
1273
1230
1274
// Fail because the buffer is too small.
1231
1275
1232
1276
// A buffer with an alignment of 8.
1233
1277
let buf = Align :: < [ u8 ; 4 ] , AU64 > :: default ( ) ;
1234
1278
// `buf.t` should be aligned to 8, so only the length check should fail.
1235
- assert ! ( Ref :: <_, AU64 >:: from ( & buf. t[ ..] ) . is_err( ) ) ;
1279
+ assert ! ( Ref :: <_, AU64 >:: from_bytes ( & buf. t[ ..] ) . is_err( ) ) ;
1236
1280
assert ! ( Ref :: <_, [ u8 ; 8 ] >:: unaligned_from( & buf. t[ ..] ) . is_err( ) ) ;
1237
1281
assert ! ( Ref :: <_, AU64 >:: from_prefix( & buf. t[ ..] ) . is_err( ) ) ;
1238
1282
assert ! ( Ref :: <_, AU64 >:: from_suffix( & buf. t[ ..] ) . is_err( ) ) ;
@@ -1243,7 +1287,7 @@ mod tests {
1243
1287
1244
1288
let buf = Align :: < [ u8 ; 12 ] , AU64 > :: default ( ) ;
1245
1289
// `buf.t` has length 12, but element size is 8.
1246
- assert ! ( Ref :: <_, [ AU64 ] >:: from ( & buf. t[ ..] ) . is_err( ) ) ;
1290
+ assert ! ( Ref :: <_, [ AU64 ] >:: from_bytes ( & buf. t[ ..] ) . is_err( ) ) ;
1247
1291
assert ! ( Ref :: <_, [ [ u8 ; 8 ] ] >:: unaligned_from( & buf. t[ ..] ) . is_err( ) ) ;
1248
1292
1249
1293
// Fail because the buffer is too short.
@@ -1262,9 +1306,9 @@ mod tests {
1262
1306
let buf = Align :: < [ u8 ; 13 ] , AU64 > :: default ( ) ;
1263
1307
// Slicing from 1, we get a buffer with size 12 (so the length check
1264
1308
// should succeed) but an alignment of only 1, which is insufficient.
1265
- assert ! ( Ref :: <_, AU64 >:: from ( & buf. t[ 1 ..] ) . is_err( ) ) ;
1309
+ assert ! ( Ref :: <_, AU64 >:: from_bytes ( & buf. t[ 1 ..] ) . is_err( ) ) ;
1266
1310
assert ! ( Ref :: <_, AU64 >:: from_prefix( & buf. t[ 1 ..] ) . is_err( ) ) ;
1267
- assert ! ( Ref :: <_, [ AU64 ] >:: from ( & buf. t[ 1 ..] ) . is_err( ) ) ;
1311
+ assert ! ( Ref :: <_, [ AU64 ] >:: from_bytes ( & buf. t[ 1 ..] ) . is_err( ) ) ;
1268
1312
assert ! ( Ref :: <_, [ AU64 ] >:: from_prefix_with_elems( & buf. t[ 1 ..] , 1 ) . is_err( ) ) ;
1269
1313
assert ! ( Ref :: <_, [ AU64 ] >:: from_suffix_with_elems( & buf. t[ 1 ..] , 1 ) . is_err( ) ) ;
1270
1314
// Slicing is unnecessary here because `new_from_suffix` uses the suffix
@@ -1292,39 +1336,39 @@ mod tests {
1292
1336
#[ test]
1293
1337
fn test_display_debug ( ) {
1294
1338
let buf = Align :: < [ u8 ; 8 ] , u64 > :: default ( ) ;
1295
- let r = Ref :: < _ , u64 > :: from ( & buf. t [ ..] ) . unwrap ( ) ;
1339
+ let r = Ref :: < _ , u64 > :: from_bytes ( & buf. t [ ..] ) . unwrap ( ) ;
1296
1340
assert_eq ! ( format!( "{}" , r) , "0" ) ;
1297
1341
assert_eq ! ( format!( "{:?}" , r) , "Ref(0)" ) ;
1298
1342
1299
1343
let buf = Align :: < [ u8 ; 8 ] , u64 > :: default ( ) ;
1300
- let r = Ref :: < _ , [ u64 ] > :: from ( & buf. t [ ..] ) . unwrap ( ) ;
1344
+ let r = Ref :: < _ , [ u64 ] > :: from_bytes ( & buf. t [ ..] ) . unwrap ( ) ;
1301
1345
assert_eq ! ( format!( "{:?}" , r) , "Ref([0])" ) ;
1302
1346
}
1303
1347
1304
1348
#[ test]
1305
1349
fn test_eq ( ) {
1306
1350
let buf1 = 0_u64 ;
1307
- let r1 = Ref :: < _ , u64 > :: from ( buf1. as_bytes ( ) ) . unwrap ( ) ;
1351
+ let r1 = Ref :: < _ , u64 > :: from_bytes ( buf1. as_bytes ( ) ) . unwrap ( ) ;
1308
1352
let buf2 = 0_u64 ;
1309
- let r2 = Ref :: < _ , u64 > :: from ( buf2. as_bytes ( ) ) . unwrap ( ) ;
1353
+ let r2 = Ref :: < _ , u64 > :: from_bytes ( buf2. as_bytes ( ) ) . unwrap ( ) ;
1310
1354
assert_eq ! ( r1, r2) ;
1311
1355
}
1312
1356
1313
1357
#[ test]
1314
1358
fn test_ne ( ) {
1315
1359
let buf1 = 0_u64 ;
1316
- let r1 = Ref :: < _ , u64 > :: from ( buf1. as_bytes ( ) ) . unwrap ( ) ;
1360
+ let r1 = Ref :: < _ , u64 > :: from_bytes ( buf1. as_bytes ( ) ) . unwrap ( ) ;
1317
1361
let buf2 = 1_u64 ;
1318
- let r2 = Ref :: < _ , u64 > :: from ( buf2. as_bytes ( ) ) . unwrap ( ) ;
1362
+ let r2 = Ref :: < _ , u64 > :: from_bytes ( buf2. as_bytes ( ) ) . unwrap ( ) ;
1319
1363
assert_ne ! ( r1, r2) ;
1320
1364
}
1321
1365
1322
1366
#[ test]
1323
1367
fn test_ord ( ) {
1324
1368
let buf1 = 0_u64 ;
1325
- let r1 = Ref :: < _ , u64 > :: from ( buf1. as_bytes ( ) ) . unwrap ( ) ;
1369
+ let r1 = Ref :: < _ , u64 > :: from_bytes ( buf1. as_bytes ( ) ) . unwrap ( ) ;
1326
1370
let buf2 = 1_u64 ;
1327
- let r2 = Ref :: < _ , u64 > :: from ( buf2. as_bytes ( ) ) . unwrap ( ) ;
1371
+ let r2 = Ref :: < _ , u64 > :: from_bytes ( buf2. as_bytes ( ) ) . unwrap ( ) ;
1328
1372
assert ! ( r1 < r2) ;
1329
1373
}
1330
1374
}
0 commit comments