[−][src]Trait serde::de::VariantAccess
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
.
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:
# 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, seed: T) -> Result<T::Value, Self::Error> where
T: DeserializeSeed<'de>,
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:
# 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,
len: usize,
visitor: V
) -> Result<V::Value, Self::Error> where
V: Visitor<'de>,
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:
# 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,
fields: &'static [&'static str],
visitor: V
) -> Result<V::Value, Self::Error> where
V: Visitor<'de>,
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:
# 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) -> Result<T, Self::Error> where
T: Deserialize<'de>,
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.