@@ -293,7 +293,7 @@ where
293293 /// Gets a reference to the underlying reader in this decoder.
294294 #[ inline( always) ]
295295 pub fn get_ref ( & self ) -> & R {
296- self . rd . rd
296+ self . rd . whole_slice
297297 }
298298}
299299
@@ -399,7 +399,7 @@ enum ExtDeserializerState {
399399#[ derive( Debug ) ]
400400struct ExtDeserializer < ' a , R , C > {
401401 rd : & ' a mut R ,
402- config : C ,
402+ _config : C ,
403403 len : u32 ,
404404 state : ExtDeserializerState ,
405405}
@@ -408,7 +408,7 @@ impl<'de, 'a, R: ReadSlice<'de> + 'a, C: SerializerConfig> ExtDeserializer<'a, R
408408 fn new ( d : & ' a mut Deserializer < R , C > , len : u32 ) -> Self {
409409 ExtDeserializer {
410410 rd : & mut d. rd ,
411- config : d. config ,
411+ _config : d. config ,
412412 len,
413413 state : ExtDeserializerState :: New ,
414414 }
@@ -925,8 +925,11 @@ impl<R: Read> ReadReader<R> {
925925impl < ' de , R : Read > ReadSlice < ' de > for ReadReader < R > {
926926 #[ inline]
927927 fn read_slice < ' a > ( & ' a mut self , len : usize ) -> Result < Reference < ' de , ' a , [ u8 ] > , io:: Error > {
928- self . buf . resize ( len, 0u8 ) ;
929- self . rd . read_exact ( & mut self . buf [ ..] ) ?;
928+ self . buf . clear ( ) ;
929+ let read = self . rd . by_ref ( ) . take ( len as u64 ) . read_to_end ( & mut self . buf ) ?;
930+ if read != len {
931+ return Err ( io:: ErrorKind :: UnexpectedEof . into ( ) ) ;
932+ }
930933
931934 Ok ( Reference :: Copied ( & self . buf [ ..] ) )
932935 }
@@ -947,15 +950,22 @@ impl<R: Read> Read for ReadReader<R> {
947950/// Borrowed reader wrapper.
948951#[ derive( Debug ) ]
949952pub struct ReadRefReader < ' a , R : ?Sized > {
950- rd : & ' a R ,
953+ whole_slice : & ' a R ,
951954 buf : & ' a [ u8 ] ,
952955}
953956
957+ impl < ' a , T > ReadRefReader < ' a , T > {
958+ /// Returns the part that hasn't been consumed yet
959+ pub fn remaining_slice ( & self ) -> & ' a [ u8 ] {
960+ self . buf
961+ }
962+ }
963+
954964impl < ' a , T : AsRef < [ u8 ] > + ?Sized > ReadRefReader < ' a , T > {
955965 #[ inline]
956966 fn new ( rd : & ' a T ) -> Self {
957967 Self {
958- rd,
968+ whole_slice : rd,
959969 buf : rd. as_ref ( ) ,
960970 }
961971 }
@@ -1011,21 +1021,10 @@ where R: Read,
10111021 Deserialize :: deserialize ( & mut Deserializer :: new ( rd) )
10121022}
10131023
1014- /// Deserializes a byte slice into the desired type.
1015- ///
1016- /// It just calls the more generic `from_read_ref`.
1017- #[ inline( always) ]
1018- pub fn from_slice < ' a , T > ( input : & ' a [ u8 ] ) -> Result < T , Error >
1019- where
1020- T : Deserialize < ' a >
1021- {
1022- from_read_ref ( input)
1023- }
1024-
1025- /// Deserialize an instance of type `T` from a reference I/O reader of MessagePack.
1024+ /// Deserialize a temporary scope-bound instance of type `T` from a slice, with zero-copy if possible.
10261025///
10271026/// Deserialization will be performed in zero-copy manner whenever it is possible, borrowing the
1028- /// data from the reader itself. For example, strings and byte-arrays won't be not copied.
1027+ /// data from the slice itself. For example, strings and byte-arrays won't copied.
10291028///
10301029/// # Errors
10311030///
@@ -1047,9 +1046,20 @@ where
10471046/// age: u8,
10481047/// }
10491048///
1050- /// assert_eq!(Dog { name: "Bobby", age: 8 }, rmp_serde::from_read_ref (&buf).unwrap());
1049+ /// assert_eq!(Dog { name: "Bobby", age: 8 }, rmp_serde::from_slice (&buf).unwrap());
10511050/// ```
1051+ #[ inline( always) ]
1052+ #[ allow( deprecated) ]
1053+ pub fn from_slice < ' a , T > ( input : & ' a [ u8 ] ) -> Result < T , Error >
1054+ where
1055+ T : Deserialize < ' a >
1056+ {
1057+ from_read_ref ( input)
1058+ }
1059+
10521060#[ inline]
1061+ #[ doc( hidden) ]
1062+ #[ deprecated( note = "use from_slice" ) ]
10531063pub fn from_read_ref < ' a , R , T > ( rd : & ' a R ) -> Result < T , Error >
10541064where
10551065 R : AsRef < [ u8 ] > + ?Sized ,
0 commit comments