156
156
157
157
#![ deny( missing_docs) ]
158
158
159
- use bincode:: { deserialize_from, serialize_into} ;
159
+ use bincode:: config:: legacy;
160
+ use bincode:: error:: { DecodeError , EncodeError } ;
161
+ use bincode:: serde:: { decode_from_std_read, encode_into_std_write} ;
160
162
use bitvec:: prelude:: * ;
161
163
use serde:: de:: { SeqAccess , Visitor } ;
162
164
use serde:: ser:: SerializeTuple ;
@@ -196,7 +198,10 @@ pub enum Error {
196
198
LBAExceedsMaximumCylinders ,
197
199
/// Deserialization errors.
198
200
#[ error( "deserialization failed" ) ]
199
- Deserialize ( #[ from] bincode:: Error ) ,
201
+ Deserialize ( #[ from] DecodeError ) ,
202
+ /// Serialization errors.
203
+ #[ error( "Serialization failed" ) ]
204
+ Serialize ( #[ from] EncodeError ) ,
200
205
/// I/O errors.
201
206
#[ error( "generic I/O error" ) ]
202
207
Io ( #[ from] std:: io:: Error ) ,
@@ -377,7 +382,7 @@ impl MBR {
377
382
S : Seek ,
378
383
{
379
384
let disk_size = u32:: try_from ( seeker. seek ( SeekFrom :: End ( 0 ) ) ? / u64:: from ( sector_size) )
380
- . unwrap_or ( u32:: max_value ( ) ) ;
385
+ . unwrap_or ( u32:: MAX ) ;
381
386
let header = MBRHeader :: new ( disk_signature) ;
382
387
383
388
Ok ( MBR {
@@ -402,12 +407,12 @@ impl MBR {
402
407
/// let mbr = mbrman::MBR::read_from(&mut f, 512)
403
408
/// .expect("could not read the partition table");
404
409
/// ```
405
- pub fn read_from < R : ? Sized > ( mut reader : & mut R , sector_size : u32 ) -> Result < MBR >
410
+ pub fn read_from < R > ( mut reader : & mut R , sector_size : u32 ) -> Result < MBR >
406
411
where
407
- R : Read + Seek ,
412
+ R : Read + Seek + ? Sized ,
408
413
{
409
414
let disk_size = u32:: try_from ( reader. seek ( SeekFrom :: End ( 0 ) ) ? / u64:: from ( sector_size) )
410
- . unwrap_or ( u32:: max_value ( ) ) ;
415
+ . unwrap_or ( u32:: MAX ) ;
411
416
let header = MBRHeader :: read_from ( & mut reader) ?;
412
417
413
418
let mut logical_partitions = Vec :: new ( ) ;
@@ -481,13 +486,13 @@ impl MBR {
481
486
482
487
/// Updates the header to match the specifications of the seeker given in argument.
483
488
/// `disk_size` will be updated after this operation.
484
- pub fn update_from < S : ? Sized > ( & mut self , seeker : & mut S ) -> Result < ( ) >
489
+ pub fn update_from < S > ( & mut self , seeker : & mut S ) -> Result < ( ) >
485
490
where
486
- S : Seek ,
491
+ S : Seek + ? Sized ,
487
492
{
488
493
self . disk_size =
489
494
u32:: try_from ( seeker. seek ( SeekFrom :: End ( 0 ) ) ? / u64:: from ( self . sector_size ) )
490
- . unwrap_or ( u32:: max_value ( ) ) ;
495
+ . unwrap_or ( u32:: MAX ) ;
491
496
Ok ( ( ) )
492
497
}
493
498
@@ -549,9 +554,9 @@ impl MBR {
549
554
/// mbr.write_into(&mut cur)
550
555
/// .expect("could not write MBR to disk")
551
556
/// ```
552
- pub fn write_into < W : ? Sized > ( & mut self , mut writer : & mut W ) -> Result < ( ) >
557
+ pub fn write_into < W > ( & mut self , mut writer : & mut W ) -> Result < ( ) >
553
558
where
554
- W : Write + Seek ,
559
+ W : Write + Seek + ? Sized ,
555
560
{
556
561
self . header . write_into ( & mut writer) ?;
557
562
@@ -588,11 +593,10 @@ impl MBR {
588
593
writer. seek ( SeekFrom :: Start ( u64:: from (
589
594
l. absolute_ebr_lba * self . sector_size ,
590
595
) ) ) ?;
591
- serialize_into ( & mut writer, & BootstrapCode446 ( l . bootstrap_code ) ) ?;
592
- serialize_into ( & mut writer, & partition ) ?;
596
+ encode_into_std_write ( BootstrapCode446 ( l . bootstrap_code ) , & mut writer, legacy ( ) ) ?;
597
+ encode_into_std_write ( & partition , & mut writer, legacy ( ) ) ?;
593
598
if let Some ( next) = next {
594
- serialize_into (
595
- & mut writer,
599
+ encode_into_std_write (
596
600
& MBRPartitionEntry {
597
601
boot : BOOT_INACTIVE ,
598
602
first_chs : next. ebr_first_chs ,
@@ -603,12 +607,14 @@ impl MBR {
603
607
. saturating_sub ( extended. starting_lba ) ,
604
608
sectors : next. ebr_sectors . unwrap ( ) ,
605
609
} ,
610
+ & mut writer,
611
+ legacy ( ) ,
606
612
) ?;
607
613
} else {
608
- serialize_into ( & mut writer, & MBRPartitionEntry :: empty ( ) ) ?;
614
+ encode_into_std_write ( MBRPartitionEntry :: empty ( ) , & mut writer, legacy ( ) ) ?;
609
615
}
610
616
writer. write_all ( & [ 0 ; 16 * 2 ] ) ?;
611
- serialize_into ( & mut writer, & BOOT_SIGNATURE ) ?;
617
+ encode_into_std_write ( BOOT_SIGNATURE , & mut writer, legacy ( ) ) ?;
612
618
}
613
619
}
614
620
@@ -1128,12 +1134,12 @@ impl MBRHeader {
1128
1134
1129
1135
/// Attempt to read a MBR header from a reader. This operation will seek at the
1130
1136
/// correct location before trying to write to disk.
1131
- pub fn read_from < R : ? Sized > ( mut reader : & mut R ) -> Result < MBRHeader >
1137
+ pub fn read_from < R > ( mut reader : & mut R ) -> Result < MBRHeader >
1132
1138
where
1133
- R : Read + Seek ,
1139
+ R : Read + Seek + ? Sized ,
1134
1140
{
1135
1141
reader. seek ( SeekFrom :: Start ( 0 ) ) ?;
1136
- let header: Self = deserialize_from ( & mut reader) ?;
1142
+ let header: Self = decode_from_std_read ( & mut reader, legacy ( ) ) ?;
1137
1143
header. check ( ) ?;
1138
1144
Ok ( header)
1139
1145
}
@@ -1154,13 +1160,13 @@ impl MBRHeader {
1154
1160
1155
1161
/// Write the MBR header into a writer. This operation will seek at the
1156
1162
/// correct location before trying to write to disk.
1157
- pub fn write_into < W : ? Sized > ( & self , mut writer : & mut W ) -> Result < ( ) >
1163
+ pub fn write_into < W > ( & self , mut writer : & mut W ) -> Result < ( ) >
1158
1164
where
1159
- W : Write + Seek ,
1165
+ W : Write + Seek + ? Sized ,
1160
1166
{
1161
1167
self . check ( ) ?;
1162
1168
writer. seek ( SeekFrom :: Start ( 0 ) ) ?;
1163
- serialize_into ( & mut writer, & self ) ?;
1169
+ encode_into_std_write ( self , & mut writer, legacy ( ) ) ?;
1164
1170
1165
1171
Ok ( ( ) )
1166
1172
}
@@ -1178,14 +1184,14 @@ impl Index<usize> for MBRHeader {
1178
1184
type Output = MBRPartitionEntry ;
1179
1185
1180
1186
fn index ( & self , i : usize ) -> & Self :: Output {
1181
- assert ! ( i != 0 , "invalid partition index: 0" ) ;
1187
+ assert_ne ! ( i, 0 , "invalid partition index: 0" ) ;
1182
1188
self . get ( i) . expect ( "invalid partition" )
1183
1189
}
1184
1190
}
1185
1191
1186
1192
impl IndexMut < usize > for MBRHeader {
1187
1193
fn index_mut ( & mut self , i : usize ) -> & mut Self :: Output {
1188
- assert ! ( i != 0 , "invalid partition index: 0" ) ;
1194
+ assert_ne ! ( i, 0 , "invalid partition index: 0" ) ;
1189
1195
self . get_mut ( i) . expect ( "invalid partition" )
1190
1196
}
1191
1197
}
@@ -1202,11 +1208,11 @@ struct EBRHeader {
1202
1208
}
1203
1209
1204
1210
impl EBRHeader {
1205
- fn read_from < R : ? Sized > ( reader : & mut R ) -> Result < EBRHeader >
1211
+ fn read_from < R > ( reader : & mut R ) -> Result < EBRHeader >
1206
1212
where
1207
1213
R : Read ,
1208
1214
{
1209
- let header: Self = deserialize_from ( reader) ?;
1215
+ let header: Self = decode_from_std_read ( reader, legacy ( ) ) ?;
1210
1216
header. check ( ) ?;
1211
1217
Ok ( header)
1212
1218
}
@@ -1562,6 +1568,7 @@ impl Serialize for CHS {
1562
1568
#[ allow( clippy:: cognitive_complexity) ]
1563
1569
mod tests {
1564
1570
use super :: * ;
1571
+ use bincode:: serde:: { decode_from_slice, encode_into_slice} ;
1565
1572
use std:: fs:: File ;
1566
1573
use std:: io:: Cursor ;
1567
1574
@@ -1570,7 +1577,7 @@ mod tests {
1570
1577
1571
1578
#[ test]
1572
1579
fn deserialize_maximum_chs_value ( ) {
1573
- let chs: CHS = bincode :: deserialize ( & [ 0xff , 0xff , 0xff ] ) . unwrap ( ) ;
1580
+ let chs: CHS = decode_from_slice ( & [ 0xff , 0xff , 0xff ] , legacy ( ) ) . unwrap ( ) . 0 ;
1574
1581
assert_eq ! (
1575
1582
chs,
1576
1583
CHS {
@@ -1588,13 +1595,16 @@ mod tests {
1588
1595
head : 255 ,
1589
1596
sector : 63 ,
1590
1597
} ;
1591
- let out = bincode:: serialize ( & chs) . unwrap ( ) ;
1592
- assert_eq ! ( out, & [ 0xff , 0xff , 0xff ] ) ;
1598
+ let mut slice = [ 0 ; 3 ] ;
1599
+ encode_into_slice ( chs, & mut slice, legacy ( ) ) . unwrap ( ) ;
1600
+ for element in slice {
1601
+ assert_eq ! ( element, 0xff ) ;
1602
+ }
1593
1603
}
1594
1604
1595
1605
#[ test]
1596
1606
fn serialize_and_deserialize_some_chs_value ( ) {
1597
- let chs: CHS = bincode :: deserialize ( & [ 0xaa , 0xaa , 0xaa ] ) . unwrap ( ) ;
1607
+ let chs: CHS = decode_from_slice ( & [ 0xaa , 0xaa , 0xaa ] , legacy ( ) ) . unwrap ( ) . 0 ;
1598
1608
assert_eq ! (
1599
1609
chs,
1600
1610
CHS {
@@ -1603,8 +1613,11 @@ mod tests {
1603
1613
sector: 42 ,
1604
1614
}
1605
1615
) ;
1606
- let out = bincode:: serialize ( & chs) . unwrap ( ) ;
1607
- assert_eq ! ( out, & [ 0xaa , 0xaa , 0xaa ] ) ;
1616
+ let mut slice = [ 0 ; 3 ] ;
1617
+ encode_into_slice ( chs, & mut slice, legacy ( ) ) . unwrap ( ) ;
1618
+ for element in slice {
1619
+ assert_eq ! ( element, 0xaa ) ;
1620
+ }
1608
1621
}
1609
1622
1610
1623
#[ test]
@@ -1707,7 +1720,7 @@ mod tests {
1707
1720
assert_eq ! (
1708
1721
CHS :: from_lba_exact(
1709
1722
mbr. logical_partitions[ 2 ] . partition. starting_lba,
1710
- u16 :: max_value ( ) ,
1723
+ u16 :: MAX ,
1711
1724
2 ,
1712
1725
3
1713
1726
)
@@ -2042,7 +2055,8 @@ mod tests {
2042
2055
mbr. header . partition_1 . sys = 0x0f ;
2043
2056
mbr. header . partition_1 . starting_lba = 1 ;
2044
2057
mbr. header . partition_1 . sectors = 10 ;
2045
- mbr. push ( 0x0f , 1 , 9 ) . unwrap ( ) . partition . boot = BOOT_ACTIVE | 0x01 ;
2058
+ let partition = mbr. push ( 0x0f , 1 , 9 ) . unwrap ( ) ;
2059
+ partition. partition . boot = BOOT_ACTIVE | 0x01 ;
2046
2060
assert ! ( matches!(
2047
2061
mbr. write_into( & mut cur) . unwrap_err( ) ,
2048
2062
Error :: InvalidBootFlag
0 commit comments