In my limited understanding of Rust, I believed that traits were like interfaces - when you implemented one, you needed to implement all of the methods.
I'm now writing a custom serde::Deserializer.
Looking at https://docs.serde.rs/serde/de/trait.Visitor.html#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))
}
}
}
and https://serde.rs/impl-deserialize.html
use std::fmt;
use serde::de::{self, Visitor};
struct I32Visitor;
impl<'de> Visitor<'de> for I32Visitor {
type Value = i32;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("an integer between -2^31 and 2^31")
}
fn visit_i8<E>(self, value: i8) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(i32::from(value))
}
fn visit_i32<E>(self, value: i32) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(value)
}
fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
where
E: de::Error,
{
use std::i32;
if value >= i64::from(i32::MIN) && value <= i64::from(i32::MAX) {
Ok(value as i32)
} else {
Err(E::custom(format!("i32 out of range: {}", value)))
}
}
// Similar for other methods:
// - visit_i16
// - visit_u8
// - visit_u16
// - visit_u32
// - visit_u64
}
It seems like the Visitor trait is magic - you just need to implement the methods you feel like implementing and the others default to some form of error.
How does this magic work?
Trait methods can have default implementations. Methods with default implementations are called "provided methods" in the documentation, and they can optionally be overridden by an implementation.
You can see the default implementations for the Visitor
trait in the source code. Not all of them return errors, but some also have a reasonable default implementation, e.g. smaller number types defer to visit_i64()
or visit_u64()
, so you get reasonable implementations for all integer types by simply defining these two methods.