[][src]Trait serde::de::VariantAccess

pub trait VariantAccess<'de>: Sized {
type Error: Error;
    fn unit_variant(self) -> Result<(), Self::Error>;
fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
    where
        T: DeserializeSeed<'de>
;
fn tuple_variant<V>(
        self,
        len: usize,
        visitor: V
    ) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>
;
fn struct_variant<V>(
        self,
        fields: &'static [&'static str],
        visitor: V
    ) -> Result<V::Value, Self::Error>
    where
        V: Visitor<'de>
; fn newtype_variant<T>(self) -> Result<T, Self::Error>
    where
        T: Deserialize<'de>
, { ... } }

VariantAccess is a visitor that is created by the Deserializer and passed to the Deserialize to deserialize the content of a particular enum variant.

Lifetime

The 'de lifetime of this trait is the lifetime of data that may be borrowed by the deserialized enum variant. See the page Understanding deserializer lifetimes for a more detailed explanation of these lifetimes.

Example implementation

The example data format presented on the website demonstrates an implementation of VariantAccess for a basic JSON data format.

Associated Types

type Error: Error

The error type that can be returned if some error occurs during deserialization. Must match the error type of our EnumAccess.

Loading content...

Required methods

fn unit_variant(self) -> Result<(), Self::Error>

Called when deserializing a variant with no values.

If the data contains a different type of variant, the following invalid_type error should be constructed:

This code runs with edition 2018
fn unit_variant(self) -> Result<(), Self::Error> {
    // What the data actually contained; suppose it is a tuple variant.
    let unexp = Unexpected::TupleVariant;
    Err(de::Error::invalid_type(unexp, &"unit variant"))
}

fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error> where
    T: DeserializeSeed<'de>, 

Called when deserializing a variant with a single value.

Deserialize implementations should typically use VariantAccess::newtype_variant instead.

If the data contains a different type of variant, the following invalid_type error should be constructed:

This code runs with edition 2018
fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value, Self::Error>
where
    T: DeserializeSeed<'de>,
{
    // What the data actually contained; suppose it is a unit variant.
    let unexp = Unexpected::UnitVariant;
    Err(de::Error::invalid_type(unexp, &"newtype variant"))
}

fn tuple_variant<V>(
    self,
    len: usize,
    visitor: V
) -> Result<V::Value, Self::Error> where
    V: Visitor<'de>, 

Called when deserializing a tuple-like variant.

The len is the number of fields expected in the tuple variant.

If the data contains a different type of variant, the following invalid_type error should be constructed:

This code runs with edition 2018
fn tuple_variant<V>(
    self,
    _len: usize,
    _visitor: V,
) -> Result<V::Value, Self::Error>
where
    V: Visitor<'de>,
{
    // What the data actually contained; suppose it is a unit variant.
    let unexp = Unexpected::UnitVariant;
    Err(de::Error::invalid_type(unexp, &"tuple variant"))
}

fn struct_variant<V>(
    self,
    fields: &'static [&'static str],
    visitor: V
) -> Result<V::Value, Self::Error> where
    V: Visitor<'de>, 

Called when deserializing a struct-like variant.

The fields are the names of the fields of the struct variant.

If the data contains a different type of variant, the following invalid_type error should be constructed:

This code runs with edition 2018
fn struct_variant<V>(
    self,
    _fields: &'static [&'static str],
    _visitor: V,
) -> Result<V::Value, Self::Error>
where
    V: Visitor<'de>,
{
    // What the data actually contained; suppose it is a unit variant.
    let unexp = Unexpected::UnitVariant;
    Err(de::Error::invalid_type(unexp, &"struct variant"))
}
Loading content...

Provided methods

fn newtype_variant<T>(self) -> Result<T, Self::Error> where
    T: Deserialize<'de>, 

Called when deserializing a variant with a single value.

This method exists as a convenience for Deserialize implementations. VariantAccess implementations should not override the default behavior.

Loading content...

Implementors

Loading content...