pub trait UnsizedVisitor<'de, C, T>: Sized{
type Ok;
type Error;
type Allocator: Allocator;
// Required method
fn expecting(&self, f: &mut Formatter<'_>) -> Result;
// Provided methods
fn visit_owned(
self,
cx: C,
value: T::Owned<Self::Allocator>,
) -> Result<Self::Ok, Self::Error> { ... }
fn visit_borrowed(
self,
cx: C,
value: &'de T,
) -> Result<Self::Ok, Self::Error> { ... }
fn visit_ref(self, cx: C, value: &T) -> Result<Self::Ok, Self::Error> { ... }
}Expand description
A visitor for data where we might need to borrow without copying from the
underlying Decoder.
When implementing this trait you must use the #[musli::trait_defaults]
attribute macro.
A visitor is needed with Decoder::decode_bytes and
Decoder::decode_string because the caller doesn’t know if the encoding
format is capable of producing references to the underlying data directly or
if it needs to be processed first.
If all you want is to decode a value by reference, use the
Decoder::decode_unsized method.
By requiring a visitor we ensure that the caller has to handle both
scenarios, even if one involves erroring. A type like Cow is an example
of a type which can comfortably handle both.
§Examples
use std::fmt;
use musli::Context;
use musli::de::UnsizedVisitor;
struct Visitor;
#[musli::trait_defaults]
impl<'de, C> UnsizedVisitor<'de, C, [u8]> for Visitor
where
C: Context,
{
type Ok = ();
#[inline]
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"a reference of bytes"
)
}
}Required Associated Types§
Required Methods§
Provided Methods§
Sourcefn visit_owned(
self,
cx: C,
value: T::Owned<Self::Allocator>,
) -> Result<Self::Ok, Self::Error>
fn visit_owned( self, cx: C, value: T::Owned<Self::Allocator>, ) -> Result<Self::Ok, Self::Error>
Visit an owned value.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.