diff --git a/src/de/mod.rs b/src/de.rs similarity index 100% rename from src/de/mod.rs rename to src/de.rs diff --git a/src/de/from_bytes.rs b/src/de/from_bytes.rs deleted file mode 100644 index 133ef07..0000000 --- a/src/de/from_bytes.rs +++ /dev/null @@ -1,518 +0,0 @@ -use crate::error::{Error, Result}; -use serde::de::{self, Deserialize, DeserializeSeed, IntoDeserializer, Visitor}; - -use super::BcsDeserializer; - -/// Deserializes a `&[u8]` into a type. -/// -/// This function will attempt to interpret `bytes` as the BCS serialized form of `T` and -/// deserialize `T` from `bytes`. -/// -/// # Examples -/// -/// ``` -/// use bcs::from_bytes; -/// use serde::Deserialize; -/// -/// #[derive(Deserialize)] -/// struct Ip([u8; 4]); -/// -/// #[derive(Deserialize)] -/// struct Port(u16); -/// -/// #[derive(Deserialize)] -/// struct SocketAddr { -/// ip: Ip, -/// port: Port, -/// } -/// -/// let bytes = vec![0x7f, 0x00, 0x00, 0x01, 0x41, 0x1f]; -/// let socket_addr: SocketAddr = from_bytes(&bytes).unwrap(); -/// -/// assert_eq!(socket_addr.ip.0, [127, 0, 0, 1]); -/// assert_eq!(socket_addr.port.0, 8001); -/// ``` -pub fn from_bytes<'a, T>(bytes: &'a [u8]) -> Result -where - T: Deserialize<'a>, -{ - let mut deserializer = Deserializer::new(bytes, crate::MAX_CONTAINER_DEPTH); - let t = T::deserialize(&mut deserializer)?; - deserializer.end().map(move |_| t) -} - -/// Perform a stateful deserialization from a `&[u8]` using the provided `seed`. -pub fn from_bytes_seed<'a, T>(seed: T, bytes: &'a [u8]) -> Result -where - T: DeserializeSeed<'a>, -{ - let mut deserializer = Deserializer::new(bytes, crate::MAX_CONTAINER_DEPTH); - let t = seed.deserialize(&mut deserializer)?; - deserializer.end().map(move |_| t) -} - -impl<'de> BcsDeserializer for Deserializer<'de> { - fn next(&mut self) -> Result { - let byte = self.peek()?; - self.input = &self.input[1..]; - Ok(byte) - } - - fn max_remaining_depth(&mut self) -> usize { - self.max_remaining_depth - } - - fn max_remaining_depth_mut(&mut self) -> &mut usize { - &mut self.max_remaining_depth - } - - fn fill_slice(&mut self, slice: &mut [u8]) -> Result<()> { - for byte in slice { - *byte = self.next()?; - } - Ok(()) - } -} - -/// Deserialization implementation for BCS -struct Deserializer<'de> { - input: &'de [u8], - max_remaining_depth: usize, -} - -impl<'de> Deserializer<'de> { - /// Creates a new `Deserializer` which will be deserializing the provided - /// input. - fn new(input: &'de [u8], max_remaining_depth: usize) -> Self { - Deserializer { - input, - max_remaining_depth, - } - } - - /// The `Deserializer::end` method should be called after a type has been - /// fully deserialized. This allows the `Deserializer` to validate that - /// the there are no more bytes remaining in the input stream. - fn end(&mut self) -> Result<()> { - if self.input.is_empty() { - Ok(()) - } else { - Err(Error::RemainingInput) - } - } -} - -impl<'de> Deserializer<'de> { - fn peek(&mut self) -> Result { - self.input.first().copied().ok_or(Error::Eof) - } - - fn parse_string_borrowed(&mut self) -> Result<&'de str> { - let slice = self.parse_bytes_borrowed()?; - std::str::from_utf8(slice).map_err(|_| Error::Utf8) - } - - fn parse_bytes_borrowed(&mut self) -> Result<&'de [u8]> { - let len = self.parse_length()?; - let slice = self.input.get(..len).ok_or(Error::Eof)?; - self.input = &self.input[len..]; - Ok(slice) - } -} - -impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> { - type Error = Error; - - // BCS is not a self-describing format so we can't implement `deserialize_any` - fn deserialize_any(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - Err(Error::NotSupported("deserialize_any")) - } - - fn deserialize_bool(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_bool(self.parse_bool()?) - } - - fn deserialize_i8(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_i8(self.parse_u8()? as i8) - } - - fn deserialize_i16(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_i16(self.parse_u16()? as i16) - } - - fn deserialize_i32(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_i32(self.parse_u32()? as i32) - } - - fn deserialize_i64(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_i64(self.parse_u64()? as i64) - } - - fn deserialize_i128(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_i128(self.parse_u128()? as i128) - } - - fn deserialize_u8(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_u8(self.parse_u8()?) - } - - fn deserialize_u16(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_u16(self.parse_u16()?) - } - - fn deserialize_u32(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_u32(self.parse_u32()?) - } - - fn deserialize_u64(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_u64(self.parse_u64()?) - } - - fn deserialize_u128(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_u128(self.parse_u128()?) - } - - fn deserialize_f32(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - Err(Error::NotSupported("deserialize_f32")) - } - - fn deserialize_f64(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - Err(Error::NotSupported("deserialize_f64")) - } - - fn deserialize_char(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - Err(Error::NotSupported("deserialize_char")) - } - - fn deserialize_str(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_borrowed_str(self.parse_string_borrowed()?) - } - - fn deserialize_string(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_str(visitor) - } - - fn deserialize_bytes(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_borrowed_bytes(self.parse_bytes_borrowed()?) - } - - fn deserialize_byte_buf(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_bytes(visitor) - } - - fn deserialize_option(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - let byte = self.next()?; - - match byte { - 0 => visitor.visit_none(), - 1 => visitor.visit_some(self), - _ => Err(Error::ExpectedOption), - } - } - - fn deserialize_unit(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_unit() - } - - fn deserialize_unit_struct(self, name: &'static str, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.enter_named_container(name)?; - let r = self.deserialize_unit(visitor); - self.leave_named_container(); - r - } - - fn deserialize_newtype_struct(self, name: &'static str, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.enter_named_container(name)?; - let r = visitor.visit_newtype_struct(&mut *self); - self.leave_named_container(); - r - } - #[allow(clippy::needless_borrow)] - fn deserialize_seq(mut self, visitor: V) -> Result - where - V: Visitor<'de>, - { - let len = self.parse_length()?; - visitor.visit_seq(SeqDeserializer::new(&mut self, len)) - } - #[allow(clippy::needless_borrow)] - fn deserialize_tuple(mut self, len: usize, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_seq(SeqDeserializer::new(&mut self, len)) - } - #[allow(clippy::needless_borrow)] - fn deserialize_tuple_struct( - mut self, - name: &'static str, - len: usize, - visitor: V, - ) -> Result - where - V: Visitor<'de>, - { - self.enter_named_container(name)?; - let r = visitor.visit_seq(SeqDeserializer::new(&mut self, len)); - self.leave_named_container(); - r - } - #[allow(clippy::needless_borrow)] - fn deserialize_map(mut self, visitor: V) -> Result - where - V: Visitor<'de>, - { - let len = self.parse_length()?; - visitor.visit_map(MapDeserializer::new(&mut self, len)) - } - #[allow(clippy::needless_borrow)] - fn deserialize_struct( - mut self, - name: &'static str, - fields: &'static [&'static str], - visitor: V, - ) -> Result - where - V: Visitor<'de>, - { - self.enter_named_container(name)?; - let r = visitor.visit_seq(SeqDeserializer::new(&mut self, fields.len())); - self.leave_named_container(); - r - } - - fn deserialize_enum( - self, - name: &'static str, - _variants: &'static [&'static str], - visitor: V, - ) -> Result - where - V: Visitor<'de>, - { - self.enter_named_container(name)?; - let r = visitor.visit_enum(&mut *self); - self.leave_named_container(); - r - } - - // BCS does not utilize identifiers, so throw them away - fn deserialize_identifier(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_bytes(_visitor) - } - - // BCS is not a self-describing format so we can't implement `deserialize_ignored_any` - fn deserialize_ignored_any(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - Err(Error::NotSupported("deserialize_ignored_any")) - } - - // BCS is not a human readable format - fn is_human_readable(&self) -> bool { - false - } -} - -struct SeqDeserializer<'a, 'de: 'a> { - de: &'a mut Deserializer<'de>, - remaining: usize, -} -#[allow(clippy::needless_borrow)] -impl<'a, 'de> SeqDeserializer<'a, 'de> { - fn new(de: &'a mut Deserializer<'de>, remaining: usize) -> Self { - Self { de, remaining } - } -} - -impl<'de, 'a> de::SeqAccess<'de> for SeqDeserializer<'a, 'de> { - type Error = Error; - - fn next_element_seed(&mut self, seed: T) -> Result> - where - T: DeserializeSeed<'de>, - { - if self.remaining == 0 { - Ok(None) - } else { - self.remaining -= 1; - seed.deserialize(&mut *self.de).map(Some) - } - } - - fn size_hint(&self) -> Option { - Some(self.remaining) - } -} - -struct MapDeserializer<'a, 'de: 'a> { - de: &'a mut Deserializer<'de>, - remaining: usize, - previous_key_bytes: Option<&'a [u8]>, -} - -impl<'a, 'de> MapDeserializer<'a, 'de> { - fn new(de: &'a mut Deserializer<'de>, remaining: usize) -> Self { - Self { - de, - remaining, - previous_key_bytes: None, - } - } -} - -impl<'de, 'a> de::MapAccess<'de> for MapDeserializer<'a, 'de> { - type Error = Error; - - fn next_key_seed(&mut self, seed: K) -> Result> - where - K: DeserializeSeed<'de>, - { - match self.remaining.checked_sub(1) { - None => Ok(None), - Some(remaining) => { - let previous_input_slice = self.de.input; - let key_value = seed.deserialize(&mut *self.de)?; - let key_len = previous_input_slice - .len() - .saturating_sub(self.de.input.len()); - let key_bytes = &previous_input_slice[..key_len]; - if let Some(previous_key_bytes) = self.previous_key_bytes { - if previous_key_bytes >= key_bytes { - return Err(Error::NonCanonicalMap); - } - } - self.remaining = remaining; - self.previous_key_bytes = Some(key_bytes); - Ok(Some(key_value)) - } - } - } - - fn next_value_seed(&mut self, seed: V) -> Result - where - V: DeserializeSeed<'de>, - { - seed.deserialize(&mut *self.de) - } - - fn size_hint(&self) -> Option { - Some(self.remaining) - } -} - -impl<'de, 'a> de::EnumAccess<'de> for &'a mut Deserializer<'de> { - type Error = Error; - type Variant = Self; - - fn variant_seed(self, seed: V) -> Result<(V::Value, Self::Variant)> - where - V: DeserializeSeed<'de>, - { - let variant_index = self.parse_u32_from_uleb128()?; - let result: Result = seed.deserialize(variant_index.into_deserializer()); - Ok((result?, self)) - } -} - -impl<'de, 'a> de::VariantAccess<'de> for &'a mut Deserializer<'de> { - type Error = Error; - - fn unit_variant(self) -> Result<()> { - Ok(()) - } - - fn newtype_variant_seed(self, seed: T) -> Result - where - T: DeserializeSeed<'de>, - { - seed.deserialize(self) - } - - fn tuple_variant(self, len: usize, visitor: V) -> Result - where - V: Visitor<'de>, - { - de::Deserializer::deserialize_tuple(self, len, visitor) - } - - fn struct_variant(self, fields: &'static [&'static str], visitor: V) -> Result - where - V: Visitor<'de>, - { - de::Deserializer::deserialize_tuple(self, fields.len(), visitor) - } -} diff --git a/src/de/from_reader.rs b/src/de/from_reader.rs deleted file mode 100644 index e550cbc..0000000 --- a/src/de/from_reader.rs +++ /dev/null @@ -1,475 +0,0 @@ -use crate::error::{Error, Result}; -use serde::de::{self, DeserializeSeed, IntoDeserializer, Visitor}; -use std::io::Read; - -use super::BcsDeserializer; - -/// Deserializes BCS from a [`std::io::Read`]er -pub struct DeserializeReader<'de, R> { - reader: TeeReader<'de, R>, - max_remaining_depth: usize, -} - -impl<'de, R: Read> DeserializeReader<'de, R> { - /// Wraps the provided reader in a new [`DeserializeReader`] - fn new(reader: &'de mut R, max_remaining_depth: usize) -> Self { - DeserializeReader { - reader: TeeReader::new(reader), - max_remaining_depth, - } - } -} - -impl<'de, R: Read> BcsDeserializer for DeserializeReader<'de, R> { - fn fill_slice(&mut self, slice: &mut [u8]) -> Result<()> { - Ok(self.reader.read_exact(&mut slice[..])?) - } - - fn max_remaining_depth(&mut self) -> usize { - self.max_remaining_depth - } - - fn max_remaining_depth_mut(&mut self) -> &mut usize { - &mut self.max_remaining_depth - } -} - -impl<'de, R: Read> DeserializeReader<'de, R> { - /// Parse a vector of bytes from the reader - fn parse_vec(&mut self) -> Result> { - let len = self.parse_length()?; - let mut output = vec![0; len]; - self.fill_slice(&mut output)?; - Ok(output) - } - - /// Parse a String from the reader - fn parse_string(&mut self) -> Result { - let bytes = self.parse_vec()?; - String::from_utf8(bytes).map_err(|_| Error::Utf8) - } -} - -impl<'de, 'a, R: Read> de::Deserializer<'de> for &'a mut DeserializeReader<'de, R> { - type Error = Error; - - // BCS is not a self-describing format so we can't implement `deserialize_any` - fn deserialize_any(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - Err(Error::NotSupported("deserialize_any")) - } - - fn deserialize_bool(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_bool(self.parse_bool()?) - } - - fn deserialize_i8(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_i8(self.parse_u8()? as i8) - } - - fn deserialize_i16(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_i16(self.parse_u16()? as i16) - } - - fn deserialize_i32(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_i32(self.parse_u32()? as i32) - } - - fn deserialize_i64(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_i64(self.parse_u64()? as i64) - } - - fn deserialize_i128(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_i128(self.parse_u128()? as i128) - } - - fn deserialize_u8(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_u8(self.parse_u8()?) - } - - fn deserialize_u16(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_u16(self.parse_u16()?) - } - - fn deserialize_u32(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_u32(self.parse_u32()?) - } - - fn deserialize_u64(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_u64(self.parse_u64()?) - } - - fn deserialize_u128(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_u128(self.parse_u128()?) - } - - fn deserialize_f32(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - Err(Error::NotSupported("deserialize_f32")) - } - - fn deserialize_f64(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - Err(Error::NotSupported("deserialize_f64")) - } - - fn deserialize_char(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - Err(Error::NotSupported("deserialize_char")) - } - - fn deserialize_str(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_string(self.parse_string()?) - } - - fn deserialize_string(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_str(visitor) - } - - fn deserialize_bytes(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_byte_buf(self.parse_vec()?) - } - - fn deserialize_byte_buf(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_bytes(visitor) - } - - fn deserialize_option(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - let byte = self.next()?; - - match byte { - 0 => visitor.visit_none(), - 1 => visitor.visit_some(self), - _ => Err(Error::ExpectedOption), - } - } - - fn deserialize_unit(self, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_unit() - } - - fn deserialize_unit_struct(self, name: &'static str, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.enter_named_container(name)?; - let r = self.deserialize_unit(visitor); - self.leave_named_container(); - r - } - - fn deserialize_newtype_struct(self, name: &'static str, visitor: V) -> Result - where - V: Visitor<'de>, - { - self.enter_named_container(name)?; - let r = visitor.visit_newtype_struct(&mut *self); - self.leave_named_container(); - r - } - #[allow(clippy::needless_borrow)] - fn deserialize_seq(mut self, visitor: V) -> Result - where - V: Visitor<'de>, - { - let len = self.parse_length()?; - visitor.visit_seq(SeqDeserializer::new(&mut self, len)) - } - #[allow(clippy::needless_borrow)] - fn deserialize_tuple(mut self, len: usize, visitor: V) -> Result - where - V: Visitor<'de>, - { - visitor.visit_seq(SeqDeserializer::new(&mut self, len)) - } - #[allow(clippy::needless_borrow)] - fn deserialize_tuple_struct( - mut self, - name: &'static str, - len: usize, - visitor: V, - ) -> Result - where - V: Visitor<'de>, - { - self.enter_named_container(name)?; - let r = visitor.visit_seq(SeqDeserializer::new(&mut self, len)); - self.leave_named_container(); - r - } - #[allow(clippy::needless_borrow)] - fn deserialize_map(mut self, visitor: V) -> Result - where - V: Visitor<'de>, - { - let len = self.parse_length()?; - visitor.visit_map(MapDeserializer::new(&mut self, len)) - } - #[allow(clippy::needless_borrow)] - fn deserialize_struct( - mut self, - name: &'static str, - fields: &'static [&'static str], - visitor: V, - ) -> Result - where - V: Visitor<'de>, - { - self.enter_named_container(name)?; - let r = visitor.visit_seq(SeqDeserializer::new(&mut self, fields.len())); - self.leave_named_container(); - r - } - - fn deserialize_enum( - self, - name: &'static str, - _variants: &'static [&'static str], - visitor: V, - ) -> Result - where - V: Visitor<'de>, - { - self.enter_named_container(name)?; - let r = visitor.visit_enum(&mut *self); - self.leave_named_container(); - r - } - - // BCS does not utilize identifiers, so throw them away - fn deserialize_identifier(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - self.deserialize_bytes(_visitor) - } - - // BCS is not a self-describing format so we can't implement `deserialize_ignored_any` - fn deserialize_ignored_any(self, _visitor: V) -> Result - where - V: Visitor<'de>, - { - Err(Error::NotSupported("deserialize_ignored_any")) - } - - // BCS is not a human readable format - fn is_human_readable(&self) -> bool { - false - } -} - -struct SeqDeserializer<'a, 'de: 'a, R> { - de: &'a mut DeserializeReader<'de, R>, - remaining: usize, -} -#[allow(clippy::needless_borrow)] -impl<'a, 'de: 'a, R> SeqDeserializer<'a, 'de, R> { - fn new(de: &'a mut DeserializeReader<'de, R>, remaining: usize) -> Self { - Self { de, remaining } - } -} - -impl<'de, 'a, R: Read> de::SeqAccess<'de> for SeqDeserializer<'a, 'de, R> { - type Error = Error; - - fn next_element_seed(&mut self, seed: T) -> Result> - where - T: DeserializeSeed<'de>, - { - if self.remaining == 0 { - Ok(None) - } else { - self.remaining -= 1; - seed.deserialize(&mut *self.de).map(Some) - } - } - - fn size_hint(&self) -> Option { - Some(self.remaining) - } -} - -/// A reader that can optionally capture all bytes from an underlying [`Read`]er -pub struct TeeReader<'a, R> { - reader: &'a mut R, - capture_buffer: Option>, -} - -impl<'a, R> TeeReader<'a, R> { - /// Wrapse the provided reader in a new [`TeeReader`]. - pub fn new(reader: &'a mut R) -> Self { - Self { - reader, - capture_buffer: Default::default(), - } - } -} - -impl<'a, R: Read> Read for TeeReader<'a, R> { - fn read(&mut self, buf: &mut [u8]) -> std::io::Result { - let bytes_read = self.reader.read(buf)?; - if let Some(ref mut buffer) = self.capture_buffer { - buffer.extend_from_slice(&buf[..bytes_read]); - } - Ok(bytes_read) - } -} - -struct MapDeserializer<'a, 'de: 'a, R> { - de: &'a mut DeserializeReader<'de, R>, - remaining: usize, - previous_key_bytes: Option>, -} - -impl<'a, 'de, R: Read> MapDeserializer<'a, 'de, R> { - fn new(de: &'a mut DeserializeReader<'de, R>, remaining: usize) -> Self { - Self { - de, - remaining, - previous_key_bytes: None, - } - } -} - -impl<'de, 'a, R: Read> de::MapAccess<'de> for MapDeserializer<'a, 'de, R> -where - 'de: 'a, -{ - type Error = Error; - - fn next_key_seed(&mut self, seed: K) -> Result> - where - K: DeserializeSeed<'de>, - { - match self.remaining.checked_sub(1) { - None => Ok(None), - Some(remaining) => { - self.de.reader.capture_buffer = Some(Vec::new()); - let key_value = seed.deserialize(&mut *self.de)?; - let key_bytes = self.de.reader.capture_buffer.take().unwrap(); - - if let Some(ref previous_key_bytes) = self.previous_key_bytes { - if previous_key_bytes.as_slice() >= key_bytes.as_slice() { - return Err(Error::NonCanonicalMap); - } - } - self.remaining = remaining; - self.previous_key_bytes = Some(key_bytes); - Ok(Some(key_value)) - } - } - } - - fn next_value_seed(&mut self, seed: V) -> Result - where - V: DeserializeSeed<'de>, - { - seed.deserialize(&mut *self.de) - } - - fn size_hint(&self) -> Option { - Some(self.remaining) - } -} - -impl<'a, 'de: 'a, R: Read> de::EnumAccess<'de> for &'a mut DeserializeReader<'de, R> { - type Error = Error; - type Variant = Self; - - fn variant_seed(self, seed: V) -> Result<(V::Value, Self::Variant)> - where - V: DeserializeSeed<'de>, - { - let variant_index = self.parse_u32_from_uleb128()?; - let result: Result = seed.deserialize(variant_index.into_deserializer()); - Ok((result?, self)) - } -} - -impl<'a, 'de: 'a, R: Read> de::VariantAccess<'de> for &'a mut DeserializeReader<'de, R> { - type Error = Error; - - fn unit_variant(self) -> Result<()> { - Ok(()) - } - - fn newtype_variant_seed(self, seed: T) -> Result - where - T: DeserializeSeed<'de>, - { - seed.deserialize(self) - } - - fn tuple_variant(self, len: usize, visitor: V) -> Result - where - V: Visitor<'de>, - { - de::Deserializer::deserialize_tuple(self, len, visitor) - } - - fn struct_variant(self, fields: &'static [&'static str], visitor: V) -> Result - where - V: Visitor<'de>, - { - de::Deserializer::deserialize_tuple(self, fields.len(), visitor) - } -}