Trait VariantAccess
trait VariantAccess<'de>: Sized
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: TraitBound { trait_: Path { path: "Error", id: Id(217), args: None }, generic_params: [], modifier: None }The error type that can be returned if some error occurs during deserialization. Must match the error type of our
EnumAccess.
Required Methods
fn unit_variant(self: Self) -> Result<(), <Self as >::Error>Called when deserializing a variant with no values.
If the data contains a different type of variant, the following
invalid_typeerror should be constructed:# use serde::de::{self, value, DeserializeSeed, Visitor, VariantAccess, Unexpected}; # # struct X; # # impl<'de> VariantAccess<'de> for X { # type Error = value::Error; # 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, _: T) -> Result<T::Value, Self::Error> # where # T: DeserializeSeed<'de>, # { unimplemented!() } # # fn tuple_variant<V>(self, _: usize, _: V) -> Result<V::Value, Self::Error> # where # V: Visitor<'de>, # { unimplemented!() } # # fn struct_variant<V>(self, _: &[&str], _: V) -> Result<V::Value, Self::Error> # where # V: Visitor<'de>, # { unimplemented!() } # }fn newtype_variant_seed<T>(self: Self, seed: T) -> Result<<T as >::Value, <Self as >::Error> where T: DeserializeSeed<'de>Called when deserializing a variant with a single value.
Deserializeimplementations should typically useVariantAccess::newtype_variantinstead.If the data contains a different type of variant, the following
invalid_typeerror should be constructed:# use serde::de::{self, value, DeserializeSeed, Visitor, VariantAccess, Unexpected}; # # struct X; # # impl<'de> VariantAccess<'de> for X { # type Error = value::Error; # # fn unit_variant(self) -> Result<(), Self::Error> { # unimplemented!() # } # 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, _: usize, _: V) -> Result<V::Value, Self::Error> # where # V: Visitor<'de>, # { unimplemented!() } # # fn struct_variant<V>(self, _: &[&str], _: V) -> Result<V::Value, Self::Error> # where # V: Visitor<'de>, # { unimplemented!() } # }fn tuple_variant<V>(self: Self, len: usize, visitor: V) -> Result<<V as >::Value, <Self as >::Error> where V: Visitor<'de>Called when deserializing a tuple-like variant.
The
lenis the number of fields expected in the tuple variant.If the data contains a different type of variant, the following
invalid_typeerror should be constructed:# use serde::de::{self, value, DeserializeSeed, Visitor, VariantAccess, Unexpected}; # # struct X; # # impl<'de> VariantAccess<'de> for X { # type Error = value::Error; # # fn unit_variant(self) -> Result<(), Self::Error> { # unimplemented!() # } # # fn newtype_variant_seed<T>(self, _: T) -> Result<T::Value, Self::Error> # where # T: DeserializeSeed<'de>, # { unimplemented!() } # 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, _: &[&str], _: V) -> Result<V::Value, Self::Error> # where # V: Visitor<'de>, # { unimplemented!() } # }fn struct_variant<V>(self: Self, fields: &'static [&'static str], visitor: V) -> Result<<V as >::Value, <Self as >::Error> where V: Visitor<'de>Called when deserializing a struct-like variant.
The
fieldsare the names of the fields of the struct variant.If the data contains a different type of variant, the following
invalid_typeerror should be constructed:# use serde::de::{self, value, DeserializeSeed, Visitor, VariantAccess, Unexpected}; # # struct X; # # impl<'de> VariantAccess<'de> for X { # type Error = value::Error; # # fn unit_variant(self) -> Result<(), Self::Error> { # unimplemented!() # } # # fn newtype_variant_seed<T>(self, _: T) -> Result<T::Value, Self::Error> # where # T: DeserializeSeed<'de>, # { unimplemented!() } # # fn tuple_variant<V>(self, _: usize, _: V) -> Result<V::Value, Self::Error> # where # V: Visitor<'de>, # { unimplemented!() } # 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")) } # }
Provided Methods
fn newtype_variant<T>(self: Self) -> Result<T, <Self as >::Error> where T: Deserialize<'de>Called when deserializing a variant with a single value.
This method exists as a convenience for
Deserializeimplementations.VariantAccessimplementations should not override the default behavior.