@@ -2,7 +2,7 @@ use crate::leb128::{self, max_leb128_len};
22use crate :: serialize:: { self , Decoder as _, Encoder as _} ;
33use std:: borrow:: Cow ;
44use std:: fs:: File ;
5- use std:: io:: { self , Write } ;
5+ use std:: io:: { self , BufRead , BufReader , Read , Write } ;
66use std:: mem:: MaybeUninit ;
77use std:: path:: Path ;
88use std:: ptr;
@@ -680,6 +680,151 @@ impl<'a> serialize::Decoder for Decoder<'a> {
680680 }
681681}
682682
683+ pub struct FileDecoder {
684+ pub file : BufReader < File > ,
685+ }
686+
687+ impl FileDecoder {
688+ #[ inline]
689+ pub fn new ( file : BufReader < File > ) -> Self {
690+ FileDecoder { file }
691+ }
692+
693+ #[ inline]
694+ pub fn advance ( & mut self , bytes : usize ) {
695+ self . file . consume ( bytes)
696+ }
697+ }
698+
699+ macro_rules! read_leb128 {
700+ ( $dec: expr, $fun: ident, $ty: ty) => { {
701+ let mut buf = $dec. file. buffer( ) ;
702+ if buf. len( ) < max_leb128_len!( $ty) {
703+ buf = $dec. file. fill_buf( ) ?;
704+ }
705+ let ( value, bytes_read) : ( $ty, usize ) = leb128:: $fun( & buf) ;
706+ $dec. file. consume( bytes_read) ;
707+ Ok ( value)
708+ } } ;
709+ }
710+
711+ impl serialize:: Decoder for FileDecoder {
712+ type Error = io:: Error ;
713+
714+ #[ inline]
715+ fn read_nil ( & mut self ) -> Result < ( ) , Self :: Error > {
716+ Ok ( ( ) )
717+ }
718+
719+ #[ inline]
720+ fn read_u128 ( & mut self ) -> Result < u128 , Self :: Error > {
721+ read_leb128 ! ( self , read_u128_leb128, u128 )
722+ }
723+
724+ #[ inline]
725+ fn read_u64 ( & mut self ) -> Result < u64 , Self :: Error > {
726+ read_leb128 ! ( self , read_u64_leb128, u64 )
727+ }
728+
729+ #[ inline]
730+ fn read_u32 ( & mut self ) -> Result < u32 , Self :: Error > {
731+ read_leb128 ! ( self , read_u32_leb128, u32 )
732+ }
733+
734+ #[ inline]
735+ fn read_u16 ( & mut self ) -> Result < u16 , Self :: Error > {
736+ read_leb128 ! ( self , read_u16_leb128, u16 )
737+ }
738+
739+ #[ inline]
740+ fn read_u8 ( & mut self ) -> Result < u8 , Self :: Error > {
741+ let mut value = [ 0 ; 1 ] ;
742+ self . file . read_exact ( & mut value) ?;
743+ let [ value] = value;
744+ Ok ( value)
745+ }
746+
747+ #[ inline]
748+ fn read_usize ( & mut self ) -> Result < usize , Self :: Error > {
749+ read_leb128 ! ( self , read_usize_leb128, usize )
750+ }
751+
752+ #[ inline]
753+ fn read_i128 ( & mut self ) -> Result < i128 , Self :: Error > {
754+ read_leb128 ! ( self , read_i128_leb128, i128 )
755+ }
756+
757+ #[ inline]
758+ fn read_i64 ( & mut self ) -> Result < i64 , Self :: Error > {
759+ read_leb128 ! ( self , read_i64_leb128, i64 )
760+ }
761+
762+ #[ inline]
763+ fn read_i32 ( & mut self ) -> Result < i32 , Self :: Error > {
764+ read_leb128 ! ( self , read_i32_leb128, i32 )
765+ }
766+
767+ #[ inline]
768+ fn read_i16 ( & mut self ) -> Result < i16 , Self :: Error > {
769+ read_leb128 ! ( self , read_i16_leb128, i16 )
770+ }
771+
772+ #[ inline]
773+ fn read_i8 ( & mut self ) -> Result < i8 , Self :: Error > {
774+ let as_u8 = self . read_u8 ( ) ?;
775+ unsafe { Ok ( :: std:: mem:: transmute ( as_u8) ) }
776+ }
777+
778+ #[ inline]
779+ fn read_isize ( & mut self ) -> Result < isize , Self :: Error > {
780+ read_leb128 ! ( self , read_isize_leb128, isize )
781+ }
782+
783+ #[ inline]
784+ fn read_bool ( & mut self ) -> Result < bool , Self :: Error > {
785+ let value = self . read_u8 ( ) ?;
786+ Ok ( value != 0 )
787+ }
788+
789+ #[ inline]
790+ fn read_f64 ( & mut self ) -> Result < f64 , Self :: Error > {
791+ let bits = self . read_u64 ( ) ?;
792+ Ok ( f64:: from_bits ( bits) )
793+ }
794+
795+ #[ inline]
796+ fn read_f32 ( & mut self ) -> Result < f32 , Self :: Error > {
797+ let bits = self . read_u32 ( ) ?;
798+ Ok ( f32:: from_bits ( bits) )
799+ }
800+
801+ #[ inline]
802+ fn read_char ( & mut self ) -> Result < char , Self :: Error > {
803+ let bits = self . read_u32 ( ) ?;
804+ Ok ( std:: char:: from_u32 ( bits) . unwrap ( ) )
805+ }
806+
807+ #[ inline]
808+ fn read_str ( & mut self ) -> Result < Cow < ' _ , str > , Self :: Error > {
809+ let len = self . read_usize ( ) ?;
810+ let mut buf = Vec :: new ( ) ;
811+ buf. resize ( len, 0u8 ) ;
812+ self . file . read_exact ( & mut buf) ?;
813+ let s = String :: from_utf8 ( buf) . unwrap ( ) ;
814+ Ok ( Cow :: Owned ( s) )
815+ }
816+
817+ #[ inline]
818+ fn error ( & mut self , err : & str ) -> Self :: Error {
819+ io:: Error :: new ( io:: ErrorKind :: Other , err)
820+ }
821+
822+ #[ inline]
823+ fn read_raw_bytes ( & mut self , s : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Self :: Error > {
824+ self . file . read_exact ( unsafe { MaybeUninit :: slice_assume_init_mut ( s) } )
825+ }
826+ }
827+
683828// Specializations for contiguous byte sequences follow. The default implementations for slices
684829// encode and decode each element individually. This isn't necessary for `u8` slices when using
685830// opaque encoders and decoders, because each `u8` is unchanged by encoding and decoding.
@@ -719,3 +864,19 @@ impl<'a> serialize::Decodable<Decoder<'a>> for Vec<u8> {
719864 Ok ( v)
720865 }
721866}
867+
868+ impl serialize:: Decodable < FileDecoder > for Vec < u8 > {
869+ fn decode ( d : & mut FileDecoder ) -> Result < Self , io:: Error > {
870+ let len = serialize:: Decoder :: read_usize ( d) ?;
871+
872+ let mut v = Vec :: with_capacity ( len) ;
873+ let buf = & mut v. spare_capacity_mut ( ) [ ..len] ;
874+ d. read_raw_bytes ( buf) ?;
875+
876+ unsafe {
877+ v. set_len ( len) ;
878+ }
879+
880+ Ok ( v)
881+ }
882+ }
0 commit comments