Trait serde::de::Visitor

source ·
pub trait Visitor<'de>: Sized {
    type Value;

Show 28 methods // Required method fn expecting(&self, formatter: &mut Formatter<'_>) -> Result; // Provided methods fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E> where E: Error { ... } fn visit_i8<E>(self, v: i8) -> Result<Self::Value, E> where E: Error { ... } fn visit_i16<E>(self, v: i16) -> Result<Self::Value, E> where E: Error { ... } fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E> where E: Error { ... } fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E> where E: Error { ... } fn visit_i128<E>(self, v: i128) -> Result<Self::Value, E> where E: Error { ... } fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E> where E: Error { ... } fn visit_u16<E>(self, v: u16) -> Result<Self::Value, E> where E: Error { ... } fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E> where E: Error { ... } fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E> where E: Error { ... } fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E> where E: Error { ... } fn visit_f32<E>(self, v: f32) -> Result<Self::Value, E> where E: Error { ... } fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E> where E: Error { ... } fn visit_char<E>(self, v: char) -> Result<Self::Value, E> where E: Error { ... } fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where E: Error { ... } fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E> where E: Error { ... } fn visit_string<E>(self, v: String) -> Result<Self::Value, E> where E: Error { ... } fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> where E: Error { ... } fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E> where E: Error { ... } fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> where E: Error { ... } fn visit_none<E>(self) -> Result<Self::Value, E> where E: Error { ... } fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error> where D: Deserializer<'de> { ... } fn visit_unit<E>(self) -> Result<Self::Value, E> where E: Error { ... } fn visit_newtype_struct<D>( self, deserializer: D, ) -> Result<Self::Value, D::Error> where D: Deserializer<'de> { ... } fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error> where A: SeqAccess<'de> { ... } fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error> where A: MapAccess<'de> { ... } fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error> where A: EnumAccess<'de> { ... }
}
Expand description

This trait represents a visitor that walks through a deserializer.

§Lifetime

The 'de lifetime of this trait is the requirement for lifetime of data that may be borrowed by Self::Value. See the page Understanding deserializer lifetimes for a more detailed explanation of these lifetimes.

§Example

/// A visitor that deserializes a long string - a string containing at least
/// some minimum number of bytes.
struct LongString {
    min: usize,
}

impl<'de> Visitor<'de> for LongString {
    type Value = String;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(formatter, "a string containing at least {} bytes", self.min)
    }

    fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        if s.len() >= self.min {
            Ok(s.to_owned())
        } else {
            Err(de::Error::invalid_value(Unexpected::Str(s), &self))
        }
    }
}

Required Associated Types§

source

type Value

The value produced by this visitor.

Required Methods§

source

fn expecting(&self, formatter: &mut Formatter<'_>) -> Result

Format a message stating what data this Visitor expects to receive.

This is used in error messages. The message should complete the sentence “This Visitor expects to receive …”, for example the message could be “an integer between 0 and 64”. The message should not be capitalized and should not end with a period.

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
    write!(formatter, "an integer between 0 and {}", self.max)
}

Provided Methods§

source

fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
where E: Error,

The input contains a boolean.

The default implementation fails with a type error.

source

fn visit_i8<E>(self, v: i8) -> Result<Self::Value, E>
where E: Error,

The input contains an i8.

The default implementation forwards to visit_i64.

source

fn visit_i16<E>(self, v: i16) -> Result<Self::Value, E>
where E: Error,

The input contains an i16.

The default implementation forwards to visit_i64.

source

fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E>
where E: Error,

The input contains an i32.

The default implementation forwards to visit_i64.

source

fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
where E: Error,

The input contains an i64.

The default implementation fails with a type error.

source

fn visit_i128<E>(self, v: i128) -> Result<Self::Value, E>
where E: Error,

The input contains a i128.

The default implementation fails with a type error.

source

fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E>
where E: Error,

The input contains a u8.

The default implementation forwards to visit_u64.

source

fn visit_u16<E>(self, v: u16) -> Result<Self::Value, E>
where E: Error,

The input contains a u16.

The default implementation forwards to visit_u64.

source

fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
where E: Error,

The input contains a u32.

The default implementation forwards to visit_u64.

source

fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
where E: Error,

The input contains a u64.

The default implementation fails with a type error.

source

fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E>
where E: Error,

The input contains a u128.

The default implementation fails with a type error.

source

fn visit_f32<E>(self, v: f32) -> Result<Self::Value, E>
where E: Error,

The input contains an f32.

The default implementation forwards to visit_f64.

source

fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
where E: Error,

The input contains an f64.

The default implementation fails with a type error.

source

fn visit_char<E>(self, v: char) -> Result<Self::Value, E>
where E: Error,

The input contains a char.

The default implementation forwards to visit_str as a one-character string.

source

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where E: Error,

The input contains a string. The lifetime of the string is ephemeral and it may be destroyed after this method returns.

This method allows the Deserializer to avoid a copy by retaining ownership of any buffered data. Deserialize implementations that do not benefit from taking ownership of String data should indicate that to the deserializer by using Deserializer::deserialize_str rather than Deserializer::deserialize_string.

It is never correct to implement visit_string without implementing visit_str. Implement neither, both, or just visit_str.

source

fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
where E: Error,

The input contains a string that lives at least as long as the Deserializer.

This enables zero-copy deserialization of strings in some formats. For example JSON input containing the JSON string "borrowed" can be deserialized with zero copying into a &'a str as long as the input data outlives 'a.

The default implementation forwards to visit_str.

source

fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where E: Error,

The input contains a string and ownership of the string is being given to the Visitor.

This method allows the Visitor to avoid a copy by taking ownership of a string created by the Deserializer. Deserialize implementations that benefit from taking ownership of String data should indicate that to the deserializer by using Deserializer::deserialize_string rather than Deserializer::deserialize_str, although not every deserializer will honor such a request.

It is never correct to implement visit_string without implementing visit_str. Implement neither, both, or just visit_str.

The default implementation forwards to visit_str and then drops the String.

source

fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where E: Error,

The input contains a byte array. The lifetime of the byte array is ephemeral and it may be destroyed after this method returns.

This method allows the Deserializer to avoid a copy by retaining ownership of any buffered data. Deserialize implementations that do not benefit from taking ownership of Vec<u8> data should indicate that to the deserializer by using Deserializer::deserialize_bytes rather than Deserializer::deserialize_byte_buf.

It is never correct to implement visit_byte_buf without implementing visit_bytes. Implement neither, both, or just visit_bytes.

source

fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E>
where E: Error,

The input contains a byte array that lives at least as long as the Deserializer.

This enables zero-copy deserialization of bytes in some formats. For example Postcard data containing bytes can be deserialized with zero copying into a &'a [u8] as long as the input data outlives 'a.

The default implementation forwards to visit_bytes.

source

fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
where E: Error,

The input contains a byte array and ownership of the byte array is being given to the Visitor.

This method allows the Visitor to avoid a copy by taking ownership of a byte buffer created by the Deserializer. Deserialize implementations that benefit from taking ownership of Vec<u8> data should indicate that to the deserializer by using Deserializer::deserialize_byte_buf rather than Deserializer::deserialize_bytes, although not every deserializer will honor such a request.

It is never correct to implement visit_byte_buf without implementing visit_bytes. Implement neither, both, or just visit_bytes.

The default implementation forwards to visit_bytes and then drops the Vec<u8>.

source

fn visit_none<E>(self) -> Result<Self::Value, E>
where E: Error,

The input contains an optional that is absent.

The default implementation fails with a type error.

source

fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
where D: Deserializer<'de>,

The input contains an optional that is present.

The default implementation fails with a type error.

source

fn visit_unit<E>(self) -> Result<Self::Value, E>
where E: Error,

The input contains a unit ().

The default implementation fails with a type error.

source

fn visit_newtype_struct<D>( self, deserializer: D, ) -> Result<Self::Value, D::Error>
where D: Deserializer<'de>,

The input contains a newtype struct.

The content of the newtype struct may be read from the given Deserializer.

The default implementation fails with a type error.

source

fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
where A: SeqAccess<'de>,

The input contains a sequence of elements.

The default implementation fails with a type error.

source

fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>
where A: MapAccess<'de>,

The input contains a key-value map.

The default implementation fails with a type error.

source

fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
where A: EnumAccess<'de>,

The input contains an enum.

The default implementation fails with a type error.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<'a> Visitor<'a> for BytesVisitor

source§

type Value = &'a [u8]

source§

impl<'a> Visitor<'a> for PathVisitor

source§

type Value = &'a Path

source§

impl<'a> Visitor<'a> for StrVisitor

source§

type Value = &'a str

source§

impl<'a, 'de> Visitor<'de> for StringInPlaceVisitor<'a>

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 1]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 2]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 3]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 4]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 5]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 6]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 7]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 8]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 9]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 10]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 11]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 12]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 13]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 14]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 15]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 16]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 17]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 18]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 19]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 20]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 21]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 22]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 23]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 24]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 25]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 26]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 27]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 28]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 29]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 30]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 31]>
where T: Deserialize<'de>,

source§

impl<'a, 'de, T> Visitor<'de> for ArrayInPlaceVisitor<'a, [T; 32]>
where T: Deserialize<'de>,

source§

impl<'de> Visitor<'de> for IgnoredAny

source§

impl<'de> Visitor<'de> for BoolVisitor

source§

impl<'de> Visitor<'de> for CStringVisitor

source§

impl<'de> Visitor<'de> for CharVisitor

source§

impl<'de> Visitor<'de> for OsStringVisitor

source§

impl<'de> Visitor<'de> for PathBufVisitor

source§

impl<'de> Visitor<'de> for StringVisitor

source§

impl<'de> Visitor<'de> for UnitVisitor

source§

impl<'de, Idx> Visitor<'de> for RangeVisitor<Idx>
where Idx: Deserialize<'de>,

source§

impl<'de, Idx> Visitor<'de> for RangeFromVisitor<Idx>
where Idx: Deserialize<'de>,

source§

type Value = Idx

source§

impl<'de, Idx> Visitor<'de> for RangeToVisitor<Idx>
where Idx: Deserialize<'de>,

source§

type Value = Idx

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 0]>

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 1]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 2]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 3]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 4]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 5]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 6]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 7]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 8]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 9]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 10]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 11]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 12]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 13]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 14]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 15]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 16]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 17]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 18]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 19]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 20]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 21]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 22]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 23]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 24]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 25]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 26]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 27]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 28]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 29]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 30]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 31]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for ArrayVisitor<[T; 32]>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for FromStrVisitor<T>
where T: FromStr, T::Err: Display,

source§

type Value = T

source§

impl<'de, T> Visitor<'de> for OptionVisitor<T>
where T: Deserialize<'de>,

source§

impl<'de, T> Visitor<'de> for PhantomDataVisitor<T>
where T: ?Sized,