Trait serde::de::VariantAccess
source · [−]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>,
{ ... }
}
Expand description
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.
Required Associated Types
Required Methods
sourcefn unit_variant(self) -> Result<(), Self::Error>
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:
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"))
}
sourcefn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>where
T: DeserializeSeed<'de>,
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:
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"))
}
sourcefn tuple_variant<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
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:
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"))
}
sourcefn struct_variant<V>(
self,
fields: &'static [&'static str],
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>,
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:
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
sourcefn newtype_variant<T>(self) -> Result<T, Self::Error>where
T: Deserialize<'de>,
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.