pub trait ReadAs<'de, T> {
// Required method
fn read<O: Options>(
value: &mut T,
p: &mut Parser<'de, O>,
) -> Result<(), ErrorCode>;
}Expand description
How a field of type T is read when its declaration names this adapter.
Implemented by the adapter rather than by T, which is what lets it
describe a type from another crate: the adapter is local to whoever writes
the impl, so the orphan rule is satisfied wherever it lives. The field keeps
its own type; only the reading of it moves.
struct Millis;
impl<'de> json::ReadAs<'de, Duration> for Millis {
fn read<O: Options>(
value: &mut Duration,
p: &mut json::Parser<'de, O>,
) -> Result<(), ErrorCode> {
let mut ms = 0u64;
json::Read::read(&mut ms, p)?;
*value = Duration::from_millis(ms);
Ok(())
}
}Adapters compose, because an adapter is a type: Option<Millis> reads an
Option<Duration> and Vec<Millis> reads a Vec<Duration>, each mirroring
the container’s own Read impl. Same is the identity,
for a position that wants the type’s own impl inside one that does not.
Required Methods§
Sourcefn read<O: Options>(
value: &mut T,
p: &mut Parser<'de, O>,
) -> Result<(), ErrorCode>
fn read<O: Options>( value: &mut T, p: &mut Parser<'de, O>, ) -> Result<(), ErrorCode>
Read into value, from the cursor’s current position.
The same contract as Read::read, including that the destination is
reused rather than replaced: an adapter over a String-shaped type
should refill it.
Unlike ReadObject::read_field, the neighbour an adapter author is
most likely to copy, this has no way to decline. It is reached only
after the key and its colon have both been consumed, so the member is
known to be this one and a failure here is terminal. The error is
reported against the object rather than wherever it was noticed,
exactly as it is for a hand-written impl.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl<'de, A, T, const N: usize> ReadAs<'de, [T; N]> for [A; N]where
A: ReadAs<'de, T>,
A fixed-length array has every element already, so the adapter never needs
T: Default here. Length is checked rather than filled, as it is for
[T; N] itself.
impl<'de, A, T, const N: usize> ReadAs<'de, [T; N]> for [A; N]where
A: ReadAs<'de, T>,
A fixed-length array has every element already, so the adapter never needs
T: Default here. Length is checked rather than filled, as it is for
[T; N] itself.
Source§impl<'de, A, T: Default + Eq + Hash, S: BuildHasher + Default> ReadAs<'de, HashSet<T, S>> for HashSet<A>where
A: ReadAs<'de, T>,
impl<'de, A, T: Default + Eq + Hash, S: BuildHasher + Default> ReadAs<'de, HashSet<T, S>> for HashSet<A>where
A: ReadAs<'de, T>,
Source§impl<'de, A, T: Default + Ord> ReadAs<'de, BTreeSet<T>> for BTreeSet<A>where
A: ReadAs<'de, T>,
impl<'de, A, T: Default + Ord> ReadAs<'de, BTreeSet<T>> for BTreeSet<A>where
A: ReadAs<'de, T>,
Source§impl<'de, KA, VA, K: Eq + Hash, V: Default, S: BuildHasher + Default> ReadAs<'de, HashMap<K, V, S>> for HashMap<KA, VA>
impl<'de, KA, VA, K: Eq + Hash, V: Default, S: BuildHasher + Default> ReadAs<'de, HashMap<K, V, S>> for HashMap<KA, VA>
Implementors§
impl<'de, T: Read<'de>> ReadAs<'de, T> for Same
impl<'de, VA, V: Default> ReadAs<'de, OrderedMap<V>> for OrderedMap<VA>where
VA: ReadAs<'de, V>,
OrderedMap’s adapter form takes one adapter rather than two, since its
key type is fixed: OrderedMap<Millis> adapts the values and leaves the
keys to String’s own FromJsonKey/ToJsonKey.