7070
7171#![ deny( missing_docs) ]
7272
73- use bincode:: { deserialize_from, serialize, serialize_into} ;
73+ use bincode:: config:: legacy;
74+ use bincode:: error:: { DecodeError , EncodeError } ;
75+ use bincode:: serde:: { decode_from_std_read, encode_into_std_write, encode_to_vec} ;
7476use crc:: { Crc , CRC_32_ISO_HDLC } ;
7577use serde:: de:: { SeqAccess , Visitor } ;
7678use serde:: ser:: SerializeTuple ;
@@ -96,7 +98,10 @@ const MAX_ALIGN: u64 = 16384;
9698pub enum Error {
9799 /// Derialization errors.
98100 #[ error( "deserialization failed" ) ]
99- Deserialize ( #[ from] bincode:: Error ) ,
101+ Deserialize ( #[ from] DecodeError ) ,
102+ /// Serialization errors.
103+ #[ error( "seserialization failed" ) ]
104+ Seserialize ( #[ from] EncodeError ) ,
100105 /// I/O errors.
101106 #[ error( "generic I/O error" ) ]
102107 Io ( #[ from] io:: Error ) ,
@@ -230,11 +235,11 @@ impl GPTHeader {
230235 ///
231236 /// The field `last_usable_lba` is not updated to reflect the actual size of the disk. You must
232237 /// do this yourself by calling `update_from`.
233- pub fn read_from < R : ? Sized > ( reader : & mut R ) -> Result < GPTHeader >
238+ pub fn read_from < R > ( mut reader : & mut R ) -> Result < GPTHeader >
234239 where
235- R : Read + Seek ,
240+ R : Read + Seek + ? Sized ,
236241 {
237- let gpt: GPTHeader = deserialize_from ( reader) ?;
242+ let gpt: GPTHeader = decode_from_std_read ( & mut reader, legacy ( ) ) ?;
238243
239244 if & gpt. signature != b"EFI PART" {
240245 return Err ( Error :: InvalidSignature ) ;
@@ -258,27 +263,27 @@ impl GPTHeader {
258263
259264 /// Write the GPT header into a writer. This operation will update the CRC32 checksums of the
260265 /// current struct and seek at the location `primary_lba` before trying to write to disk.
261- pub fn write_into < W : ? Sized > (
266+ pub fn write_into < W > (
262267 & mut self ,
263268 mut writer : & mut W ,
264269 sector_size : u64 ,
265270 partitions : & [ GPTPartitionEntry ] ,
266271 ) -> Result < ( ) >
267272 where
268- W : Write + Seek ,
273+ W : Write + Seek + ? Sized ,
269274 {
270275 self . update_partition_entry_array_crc32 ( partitions) ;
271276 self . update_crc32_checksum ( ) ;
272277
273278 writer. seek ( SeekFrom :: Start ( self . primary_lba * sector_size) ) ?;
274- serialize_into ( & mut writer, & self ) ?;
279+ encode_into_std_write ( & self , & mut writer, legacy ( ) ) ?;
275280
276281 for i in 0 ..self . number_of_partition_entries {
277282 writer. seek ( SeekFrom :: Start (
278283 self . partition_entry_lba * sector_size
279284 + u64:: from ( i) * u64:: from ( self . size_of_partition_entry ) ,
280285 ) ) ?;
281- serialize_into ( & mut writer , & partitions[ i as usize ] ) ?;
286+ encode_into_std_write ( & partitions[ i as usize ] , & mut writer , legacy ( ) ) ?;
282287 }
283288
284289 Ok ( ( ) )
@@ -288,7 +293,7 @@ impl GPTHeader {
288293 pub fn generate_crc32_checksum ( & self ) -> u32 {
289294 let mut clone = self . clone ( ) ;
290295 clone. crc32_checksum = 0 ;
291- let data = serialize ( & clone) . expect ( "could not serialize" ) ;
296+ let data = encode_to_vec ( & clone, legacy ( ) ) . expect ( "could not serialize" ) ;
292297 assert_eq ! ( data. len( ) as u32 , clone. header_size) ;
293298
294299 Crc :: < u32 > :: new ( & CRC_32_ISO_HDLC ) . checksum ( & data)
@@ -307,7 +312,7 @@ impl GPTHeader {
307312 let mut digest = crc. digest ( ) ;
308313 let mut wrote = 0 ;
309314 for x in partitions {
310- let data = serialize ( & x ) . expect ( "could not serialize" ) ;
315+ let data = encode_to_vec ( x , legacy ( ) ) . expect ( "could not serialize" ) ;
311316 digest. update ( & data) ;
312317 wrote += data. len ( ) ;
313318 }
@@ -327,9 +332,9 @@ impl GPTHeader {
327332 /// Updates the header to match the specifications of the seeker given in argument.
328333 /// `first_usable_lba`, `last_usable_lba`, `primary_lba`, `backup_lba`, `partition_entry_lba`
329334 /// will be updated after this operation.
330- pub fn update_from < S : ? Sized > ( & mut self , seeker : & mut S , sector_size : u64 ) -> Result < ( ) >
335+ pub fn update_from < S > ( & mut self , seeker : & mut S , sector_size : u64 ) -> Result < ( ) >
331336 where
332- S : Seek ,
337+ S : Seek + ? Sized ,
333338 {
334339 let partition_array_size = ( u64:: from ( self . number_of_partition_entries )
335340 * u64:: from ( self . size_of_partition_entry )
@@ -527,11 +532,11 @@ impl GPTPartitionEntry {
527532 }
528533
529534 /// Read a partition entry from the reader at the current position.
530- pub fn read_from < R : ? Sized > ( reader : & mut R ) -> bincode :: Result < GPTPartitionEntry >
535+ pub fn read_from < R > ( mut reader : & mut R ) -> std :: result :: Result < GPTPartitionEntry , DecodeError >
531536 where
532- R : Read ,
537+ R : Read + ? Sized ,
533538 {
534- deserialize_from ( reader)
539+ decode_from_std_read ( & mut reader, legacy ( ) )
535540 }
536541
537542 /// Returns `true` if the partition entry is not used (type GUID == `[0; 16]`)
@@ -715,9 +720,9 @@ impl GPT {
715720 /// let gpt = gptman::GPT::read_from(&mut f, 512)
716721 /// .expect("could not read the partition table");
717722 /// ```
718- pub fn read_from < R : ? Sized > ( mut reader : & mut R , sector_size : u64 ) -> Result < GPT >
723+ pub fn read_from < R > ( mut reader : & mut R , sector_size : u64 ) -> Result < GPT >
719724 where
720- R : Read + Seek ,
725+ R : Read + Seek + ? Sized ,
721726 {
722727 use self :: Error :: * ;
723728
@@ -779,9 +784,9 @@ impl GPT {
779784 /// let gpt_4096 = gptman::GPT::find_from(&mut f_4096)
780785 /// .expect("could not read the partition table");
781786 /// ```
782- pub fn find_from < R : ? Sized > ( mut reader : & mut R ) -> Result < GPT >
787+ pub fn find_from < R > ( mut reader : & mut R ) -> Result < GPT >
783788 where
784- R : Read + Seek ,
789+ R : Read + Seek + ? Sized ,
785790 {
786791 use self :: Error :: * ;
787792
@@ -886,9 +891,9 @@ impl GPT {
886891 /// gpt.write_into(&mut cur)
887892 /// .expect("could not write GPT to disk");
888893 /// ```
889- pub fn write_into < W : ? Sized > ( & mut self , mut writer : & mut W ) -> Result < GPTHeader >
894+ pub fn write_into < W > ( & mut self , mut writer : & mut W ) -> Result < GPTHeader >
890895 where
891- W : Write + Seek ,
896+ W : Write + Seek + ? Sized ,
892897 {
893898 self . check_partition_guids ( ) ?;
894899 self . check_partition_boundaries ( ) ?;
@@ -1233,9 +1238,9 @@ impl GPT {
12331238 /// starting at byte 446 and ending at byte 511. Any existing data will be overwritten.
12341239 ///
12351240 /// See also: [`Self::write_bootable_protective_mbr_into`].
1236- pub fn write_protective_mbr_into < W : ? Sized > ( mut writer : & mut W , sector_size : u64 ) -> Result < ( ) >
1241+ pub fn write_protective_mbr_into < W > ( mut writer : & mut W , sector_size : u64 ) -> Result < ( ) >
12371242 where
1238- W : Write + Seek ,
1243+ W : Write + Seek + ? Sized ,
12391244 {
12401245 Self :: write_protective_mbr_into_impl ( & mut writer, sector_size, false )
12411246 }
@@ -1250,23 +1255,20 @@ impl GPT {
12501255 /// <div class="warning">Some systems will not consider a disk to be bootable in UEFI mode
12511256 /// if the pMBR is marked as bootable, so this should only be used if booting on legacy BIOS
12521257 /// systems is a requirement.</div>
1253- pub fn write_bootable_protective_mbr_into < W : ?Sized > (
1254- mut writer : & mut W ,
1255- sector_size : u64 ,
1256- ) -> Result < ( ) >
1258+ pub fn write_bootable_protective_mbr_into < W > ( mut writer : & mut W , sector_size : u64 ) -> Result < ( ) >
12571259 where
1258- W : Write + Seek ,
1260+ W : Write + Seek + ? Sized ,
12591261 {
12601262 Self :: write_protective_mbr_into_impl ( & mut writer, sector_size, true )
12611263 }
12621264
1263- fn write_protective_mbr_into_impl < W : ? Sized > (
1265+ fn write_protective_mbr_into_impl < W > (
12641266 mut writer : & mut W ,
12651267 sector_size : u64 ,
12661268 bootable : bool ,
12671269 ) -> Result < ( ) >
12681270 where
1269- W : Write + Seek ,
1271+ W : Write + Seek + ? Sized ,
12701272 {
12711273 let size = writer. seek ( SeekFrom :: End ( 0 ) ) ? / sector_size - 1 ;
12721274 writer. seek ( SeekFrom :: Start ( 446 ) ) ?;
@@ -1283,13 +1285,14 @@ impl GPT {
12831285 0x01 , 0x00 , 0x00 , 0x00 , // LBA of first absolute sector
12841286 ] ) ?;
12851287 // number of sectors in partition 1
1286- serialize_into (
1287- & mut writer,
1288- & ( if size > u64:: from ( u32:: max_value ( ) ) {
1289- u32:: max_value ( )
1288+ encode_into_std_write (
1289+ if size > u64:: from ( u32:: MAX ) {
1290+ u32:: MAX
12901291 } else {
12911292 size as u32
1292- } ) ,
1293+ } ,
1294+ & mut writer,
1295+ legacy ( ) ,
12931296 ) ?;
12941297 writer. write_all ( & [ 0 ; 16 ] ) ?; // partition 2
12951298 writer. write_all ( & [ 0 ; 16 ] ) ?; // partition 3
@@ -1382,7 +1385,7 @@ mod test {
13821385 }
13831386
13841387 // NOTE: testing that serializing the PartitionName (and the whole struct) works
1385- let data1 = serialize ( & partition) . unwrap ( ) ;
1388+ let data1 = encode_to_vec ( & partition, legacy ( ) ) . unwrap ( ) ;
13861389 f. seek ( SeekFrom :: Start (
13871390 gpt. partition_entry_lba * ss
13881391 + u64:: from ( i) * u64:: from ( gpt. size_of_partition_entry ) ,
@@ -1432,7 +1435,7 @@ mod test {
14321435 assert_eq ! ( gpt. header. partition_entry_lba, 2 ) ;
14331436 gpt. header . crc32_checksum = 1 ;
14341437 cur. seek ( SeekFrom :: Start ( gpt. sector_size ) ) . unwrap ( ) ;
1435- serialize_into ( & mut cur, & gpt . header ) . unwrap ( ) ;
1438+ encode_into_std_write ( & gpt . header , & mut cur, legacy ( ) ) . unwrap ( ) ;
14361439 let maybe_gpt = GPT :: read_from ( & mut cur, gpt. sector_size ) ;
14371440 assert ! ( maybe_gpt. is_ok( ) ) ;
14381441 let gpt = maybe_gpt. unwrap ( ) ;
@@ -1587,7 +1590,7 @@ mod test {
15871590
15881591 gpt. header . crc32_checksum = 1 ;
15891592 cur. seek ( SeekFrom :: Start ( ss) ) . unwrap ( ) ;
1590- serialize_into ( & mut cur, & gpt . header ) . unwrap ( ) ;
1593+ encode_into_std_write ( & gpt . header , & mut cur, legacy ( ) ) . unwrap ( ) ;
15911594 let maybe_gpt = GPT :: read_from ( & mut cur, ss) ;
15921595 assert ! ( maybe_gpt. is_ok( ) ) ;
15931596 let gpt = maybe_gpt. unwrap ( ) ;
@@ -1608,7 +1611,7 @@ mod test {
16081611 gpt. header . crc32_checksum = 1 ;
16091612 let backup_lba = gpt. header . backup_lba ;
16101613 cur. seek ( SeekFrom :: Start ( ss) ) . unwrap ( ) ;
1611- serialize_into ( & mut cur, & gpt . header ) . unwrap ( ) ;
1614+ encode_into_std_write ( & gpt . header , & mut cur, legacy ( ) ) . unwrap ( ) ;
16121615 let mut gpt = GPT :: read_from ( & mut cur, ss) . unwrap ( ) ;
16131616 assert ! ( !gpt. is_primary( ) ) ;
16141617 assert ! ( gpt. is_backup( ) ) ;
@@ -1628,7 +1631,7 @@ mod test {
16281631
16291632 gpt. header . crc32_checksum = 1 ;
16301633 cur. seek ( SeekFrom :: Start ( ss) ) . unwrap ( ) ;
1631- serialize_into ( & mut cur, & gpt . header ) . unwrap ( ) ;
1634+ encode_into_std_write ( & gpt . header , & mut cur, legacy ( ) ) . unwrap ( ) ;
16321635 let maybe_gpt = GPT :: read_from ( & mut cur, ss) ;
16331636 assert ! ( maybe_gpt. is_ok( ) ) ;
16341637 let gpt = maybe_gpt. unwrap ( ) ;
@@ -1658,7 +1661,7 @@ mod test {
16581661
16591662 gpt. header . crc32_checksum = 1 ;
16601663 cur. seek ( SeekFrom :: Start ( ss) ) . unwrap ( ) ;
1661- serialize_into ( & mut cur, & gpt . header ) . unwrap ( ) ;
1664+ encode_into_std_write ( & gpt . header , & mut cur, legacy ( ) ) . unwrap ( ) ;
16621665 let maybe_gpt = GPT :: read_from ( & mut cur, ss) ;
16631666 assert ! ( maybe_gpt. is_ok( ) ) ;
16641667 let gpt = maybe_gpt. unwrap ( ) ;
@@ -1862,8 +1865,8 @@ mod test {
18621865 }
18631866
18641867 cur. seek ( SeekFrom :: Start ( 446 + 8 ) ) . unwrap ( ) ;
1865- let first_lba: u32 = deserialize_from ( & mut cur) . unwrap ( ) ;
1866- let sectors: u32 = deserialize_from ( & mut cur) . unwrap ( ) ;
1868+ let first_lba: u32 = decode_from_std_read ( & mut cur, legacy ( ) ) . unwrap ( ) ;
1869+ let sectors: u32 = decode_from_std_read ( & mut cur, legacy ( ) ) . unwrap ( ) ;
18671870 assert_eq ! ( first_lba, 1 ) ;
18681871 assert_eq ! ( sectors, 99 ) ;
18691872 }
0 commit comments