@@ -29,8 +29,8 @@ use core::mem::size_of;
29
29
use num_traits:: FromPrimitive ;
30
30
use num_traits:: ToPrimitive ;
31
31
use static_assertions:: const_assert;
32
- use zerocopy:: AsBytes ;
33
- use zerocopy:: LayoutVerified ;
32
+ use zerocopy:: Ref ;
33
+ use zerocopy:: { Immutable , IntoBytes , KnownLayout } ;
34
34
35
35
// The following imports are only used for std enviroments and serde.
36
36
#[ cfg( feature = "std" ) ]
@@ -443,52 +443,50 @@ impl<'a> Apcb<'a> {
443
443
size_of :: < V2_HEADER > ( ) + size_of :: < V3_HEADER_EXT > ( ) ;
444
444
pub const MAX_SIZE : usize = 0x10000 ;
445
445
446
- pub fn header ( & self ) -> Result < LayoutVerified < & [ u8 ] , V2_HEADER > > {
447
- LayoutVerified :: < & [ u8 ] , V2_HEADER > :: new_unaligned_from_prefix (
448
- & * self . backing_store ,
449
- )
450
- . map ( | ( layout , _ ) | layout )
451
- . ok_or ( Error :: FileSystem (
452
- FileSystemError :: InconsistentHeader ,
453
- "V2_HEADER" ,
454
- ) )
446
+ pub fn header ( & self ) -> Result < Ref < & [ u8 ] , V2_HEADER > > {
447
+ Ref :: < & [ u8 ] , V2_HEADER > :: from_prefix ( & * self . backing_store )
448
+ . map ( | ( layout , _ ) | layout )
449
+ . map_err ( |_| {
450
+ Error :: FileSystem (
451
+ FileSystemError :: InconsistentHeader ,
452
+ "V2_HEADER" ,
453
+ )
454
+ } )
455
455
}
456
456
457
- pub fn header_mut (
458
- & mut self ,
459
- ) -> Result < LayoutVerified < & mut [ u8 ] , V2_HEADER > > {
457
+ pub fn header_mut ( & mut self ) -> Result < Ref < & mut [ u8 ] , V2_HEADER > > {
460
458
#[ cfg( not( feature = "std" ) ) ]
461
459
let bs: & mut [ u8 ] = self . backing_store ;
462
460
#[ cfg( feature = "std" ) ]
463
461
let bs: & mut [ u8 ] = self . backing_store . to_mut ( ) ;
464
- LayoutVerified :: < & mut [ u8 ] , V2_HEADER > :: new_unaligned_from_prefix ( bs)
462
+ Ref :: < & mut [ u8 ] , V2_HEADER > :: from_prefix ( bs)
465
463
. map ( |( layout, _) | layout)
466
- . ok_or ( Error :: FileSystem (
467
- FileSystemError :: InconsistentHeader ,
468
- "V2_HEADER" ,
469
- ) )
464
+ . map_err ( |_| {
465
+ Error :: FileSystem (
466
+ FileSystemError :: InconsistentHeader ,
467
+ "V2_HEADER" ,
468
+ )
469
+ } )
470
470
}
471
471
472
- pub fn v3_header_ext (
473
- & self ,
474
- ) -> Result < Option < LayoutVerified < & [ u8 ] , V3_HEADER_EXT > > > {
472
+ pub fn v3_header_ext ( & self ) -> Result < Option < Ref < & [ u8 ] , V3_HEADER_EXT > > > {
475
473
let ( header, rest) =
476
- LayoutVerified :: < & [ u8 ] , V2_HEADER > :: new_unaligned_from_prefix (
477
- & * self . backing_store ,
478
- )
479
- . ok_or ( Error :: FileSystem (
480
- FileSystemError :: InconsistentHeader ,
481
- "V2_HEADER" ,
482
- ) ) ?;
474
+ Ref :: < & [ u8 ] , V2_HEADER > :: from_prefix ( & * self . backing_store )
475
+ . map_err ( |_| {
476
+ Error :: FileSystem (
477
+ FileSystemError :: InconsistentHeader ,
478
+ "V2_HEADER" ,
479
+ )
480
+ } ) ?;
483
481
let v3_header_ext =
484
482
if usize:: from ( header. header_size ) == Self :: V3_HEADER_EXT_SIZE {
485
- let ( ext, _) =
486
- LayoutVerified :: < & [ u8 ] , V3_HEADER_EXT >
487
- :: new_unaligned_from_prefix ( rest)
488
- . ok_or ( Error :: FileSystem (
483
+ let ( ext, _) = Ref :: < & [ u8 ] , V3_HEADER_EXT > :: from_prefix ( rest)
484
+ . map_err ( |_| {
485
+ Error :: FileSystem (
489
486
FileSystemError :: InconsistentHeader ,
490
487
"V3_HEADER_EXT" ,
491
- ) ) ?;
488
+ )
489
+ } ) ?;
492
490
Some ( ext)
493
491
} else {
494
492
None
@@ -498,28 +496,28 @@ impl<'a> Apcb<'a> {
498
496
499
497
pub fn v3_header_ext_mut (
500
498
& mut self ,
501
- ) -> Result < Option < LayoutVerified < & mut [ u8 ] , V3_HEADER_EXT > > > {
499
+ ) -> Result < Option < Ref < & mut [ u8 ] , V3_HEADER_EXT > > > {
502
500
#[ cfg( not( feature = "std" ) ) ]
503
501
let bs: & mut [ u8 ] = self . backing_store ;
504
502
#[ cfg( feature = "std" ) ]
505
503
let bs: & mut [ u8 ] = self . backing_store . to_mut ( ) ;
506
- let ( header, rest) =
507
- LayoutVerified :: < & mut [ u8 ] , V2_HEADER > :: new_unaligned_from_prefix (
508
- bs,
509
- )
510
- . ok_or ( Error :: FileSystem (
511
- FileSystemError :: InconsistentHeader ,
512
- "V2_HEADER" ,
513
- ) ) ?;
504
+ let ( header, rest) = Ref :: < & mut [ u8 ] , V2_HEADER > :: from_prefix ( bs)
505
+ . map_err ( |_| {
506
+ Error :: FileSystem (
507
+ FileSystemError :: InconsistentHeader ,
508
+ "V2_HEADER" ,
509
+ )
510
+ } ) ?;
514
511
let v3_header_ext =
515
512
if usize:: from ( header. header_size ) == Self :: V3_HEADER_EXT_SIZE {
516
513
let ( header_ext, _) =
517
- LayoutVerified :: < & mut [ u8 ] , V3_HEADER_EXT >
518
- :: new_unaligned_from_prefix ( rest)
519
- . ok_or ( Error :: FileSystem (
520
- FileSystemError :: InconsistentHeader ,
521
- "V3_HEADER_EXT" ,
522
- ) ) ?;
514
+ Ref :: < & mut [ u8 ] , V3_HEADER_EXT > :: from_prefix ( rest)
515
+ . map_err ( |_| {
516
+ Error :: FileSystem (
517
+ FileSystemError :: InconsistentHeader ,
518
+ "V3_HEADER_EXT" ,
519
+ )
520
+ } ) ?;
523
521
Some ( header_ext)
524
522
} else {
525
523
None
@@ -830,7 +828,9 @@ impl<'a> Apcb<'a> {
830
828
/// a enum of struct refs (PlatformSpecificElementRef,
831
829
/// PlatformTuningElementRef) or just one struct. Note: Currently,
832
830
/// INSTANCE_ID is always supposed to be 0.
833
- pub fn insert_struct_array_as_entry < T : EntryCompatible + AsBytes > (
831
+ pub fn insert_struct_array_as_entry <
832
+ T : EntryCompatible + IntoBytes + Immutable + KnownLayout ,
833
+ > (
834
834
& mut self ,
835
835
entry_id : EntryId ,
836
836
instance_id : u16 ,
@@ -871,7 +871,7 @@ impl<'a> Apcb<'a> {
871
871
/// it. TAIL is allowed to be &[], and often has to be.
872
872
/// Note: Currently, INSTANCE_ID is always supposed to be 0.
873
873
pub fn insert_struct_entry <
874
- H : EntryCompatible + AsBytes + HeaderWithTail ,
874
+ H : EntryCompatible + IntoBytes + Immutable + KnownLayout + HeaderWithTail ,
875
875
> (
876
876
& mut self ,
877
877
entry_id : EntryId ,
@@ -1146,22 +1146,22 @@ impl<'a> Apcb<'a> {
1146
1146
}
1147
1147
1148
1148
pub ( crate ) fn calculate_checksum (
1149
- header : & LayoutVerified < & ' _ [ u8 ] , V2_HEADER > ,
1150
- v3_header_ext : & Option < LayoutVerified < & ' _ [ u8 ] , V3_HEADER_EXT > > ,
1149
+ header : & Ref < & ' _ [ u8 ] , V2_HEADER > ,
1150
+ v3_header_ext : & Option < Ref < & ' _ [ u8 ] , V3_HEADER_EXT > > ,
1151
1151
beginning_of_groups : & [ u8 ] ,
1152
1152
) -> Result < u8 > {
1153
1153
let mut checksum_byte = 0u8 ;
1154
1154
let stored_checksum_byte = header. checksum_byte ;
1155
- for c in header. bytes ( ) {
1155
+ for c in header. as_bytes ( ) {
1156
1156
checksum_byte = checksum_byte. wrapping_add ( * c) ;
1157
1157
}
1158
- let mut offset = header. bytes ( ) . len ( ) ;
1158
+ let mut offset = header. as_bytes ( ) . len ( ) ;
1159
1159
if let Some ( v3_header_ext) = & v3_header_ext {
1160
- for c in v3_header_ext. bytes ( ) {
1160
+ for c in v3_header_ext. as_bytes ( ) {
1161
1161
checksum_byte = checksum_byte. wrapping_add ( * c) ;
1162
1162
}
1163
1163
offset = offset
1164
- . checked_add ( v3_header_ext. bytes ( ) . len ( ) )
1164
+ . checked_add ( v3_header_ext. as_bytes ( ) . len ( ) )
1165
1165
. ok_or ( Error :: OutOfSpace ) ?;
1166
1166
}
1167
1167
let apcb_size = header. apcb_size . get ( ) ;
@@ -1196,14 +1196,12 @@ impl<'a> Apcb<'a> {
1196
1196
#[ cfg( feature = "std" ) ]
1197
1197
let backing_store: & mut [ u8 ] = bs. to_mut ( ) ;
1198
1198
1199
- let ( header, mut rest) =
1200
- LayoutVerified :: < & [ u8 ] , V2_HEADER > :: new_unaligned_from_prefix (
1201
- & * backing_store,
1202
- )
1203
- . ok_or ( Error :: FileSystem (
1204
- FileSystemError :: InconsistentHeader ,
1205
- "V2_HEADER" ,
1206
- ) ) ?;
1199
+ let ( header, mut rest) = Ref :: < & [ u8 ] , V2_HEADER > :: from_prefix (
1200
+ & * backing_store,
1201
+ )
1202
+ . map_err ( |_| {
1203
+ Error :: FileSystem ( FileSystemError :: InconsistentHeader , "V2_HEADER" )
1204
+ } ) ?;
1207
1205
1208
1206
if header. signature != * b"APCB" {
1209
1207
return Err ( Error :: FileSystem (
@@ -1232,12 +1230,15 @@ impl<'a> Apcb<'a> {
1232
1230
let v3_header_ext = if usize:: from ( header. header_size )
1233
1231
== size_of :: < V2_HEADER > ( ) + size_of :: < V3_HEADER_EXT > ( )
1234
1232
{
1235
- let ( header_ext, restb) = LayoutVerified :: < & [ u8 ] , V3_HEADER_EXT >
1236
- :: new_unaligned_from_prefix ( rest)
1237
- . ok_or ( Error :: FileSystem (
1238
- FileSystemError :: InconsistentHeader ,
1239
- "V3_HEADER_EXT" ,
1240
- ) ) ?;
1233
+ let ( header_ext, restb) = Ref :: < & [ u8 ] , V3_HEADER_EXT > :: from_prefix (
1234
+ rest,
1235
+ )
1236
+ . map_err ( |_| {
1237
+ Error :: FileSystem (
1238
+ FileSystemError :: InconsistentHeader ,
1239
+ "V3_HEADER_EXT" ,
1240
+ )
1241
+ } ) ?;
1241
1242
let value = & * header_ext;
1242
1243
rest = restb;
1243
1244
if value. signature == * b"ECB2" {
@@ -1403,15 +1404,14 @@ impl<'a> Apcb<'a> {
1403
1404
header. apcb_size = ( header. header_size . get ( ) as u32 ) . into ( ) ;
1404
1405
}
1405
1406
let ( header, rest) =
1406
- LayoutVerified :: < & ' _ [ u8 ] , V2_HEADER > :: new_unaligned_from_prefix (
1407
- & * backing_store,
1408
- )
1409
- . unwrap ( ) ;
1410
- let ( v3_header_ext, rest) = LayoutVerified :: < & ' _ [ u8 ] , V3_HEADER_EXT > :: new_unaligned_from_prefix ( rest) . unwrap ( ) ;
1407
+ Ref :: < & ' _ [ u8 ] , V2_HEADER > :: from_prefix ( & * backing_store) . unwrap ( ) ;
1408
+ let ( v3_header_ext, rest) =
1409
+ Ref :: < & ' _ [ u8 ] , V3_HEADER_EXT > :: from_prefix ( rest) . unwrap ( ) ;
1411
1410
let checksum_byte =
1412
1411
Self :: calculate_checksum ( & header, & Some ( v3_header_ext) , rest) ?;
1413
1412
1414
- let ( mut header, _) = LayoutVerified :: < & ' _ mut [ u8 ] , V2_HEADER > :: new_unaligned_from_prefix ( backing_store) . unwrap ( ) ;
1413
+ let ( mut header, _) =
1414
+ Ref :: < & ' _ mut [ u8 ] , V2_HEADER > :: from_prefix ( backing_store) . unwrap ( ) ;
1415
1415
header. checksum_byte = checksum_byte;
1416
1416
Self :: load ( bs, options)
1417
1417
}
0 commit comments