Skip to main content

ReadAs

Trait ReadAs 

Source
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§

Source

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.

Source§

fn read<O: Options>( value: &mut [T; N], p: &mut Parser<'de, O>, ) -> Result<(), ErrorCode>

Source§

impl<'de, A, T: Default + Eq + Hash, S: BuildHasher + Default> ReadAs<'de, HashSet<T, S>> for HashSet<A>
where A: ReadAs<'de, T>,

Source§

fn read<O: Options>( value: &mut HashSet<T, S>, p: &mut Parser<'de, O>, ) -> Result<(), ErrorCode>

Source§

impl<'de, A, T: Default + Ord> ReadAs<'de, BTreeSet<T>> for BTreeSet<A>
where A: ReadAs<'de, T>,

Source§

fn read<O: Options>( value: &mut BTreeSet<T>, p: &mut Parser<'de, O>, ) -> Result<(), ErrorCode>

Source§

impl<'de, A, T> ReadAs<'de, Arc<T>> for Arc<A>
where A: ReadAs<'de, T>, T: Default,

Source§

fn read<O: Options>( value: &mut Arc<T>, p: &mut Parser<'de, O>, ) -> Result<(), ErrorCode>

Source§

impl<'de, A, T> ReadAs<'de, Box<T>> for Box<A>
where A: ReadAs<'de, T>,

Source§

fn read<O: Options>( value: &mut Box<T>, p: &mut Parser<'de, O>, ) -> Result<(), ErrorCode>

Source§

impl<'de, A, T> ReadAs<'de, Option<T>> for Option<A>
where A: ReadAs<'de, T>, T: Default,

Source§

fn read<O: Options>( value: &mut Option<T>, p: &mut Parser<'de, O>, ) -> Result<(), ErrorCode>

Source§

impl<'de, A, T> ReadAs<'de, Rc<T>> for Rc<A>
where A: ReadAs<'de, T>, T: Default,

Source§

fn read<O: Options>( value: &mut Rc<T>, p: &mut Parser<'de, O>, ) -> Result<(), ErrorCode>

Source§

impl<'de, A, T> ReadAs<'de, Vec<T>> for Vec<A>
where A: ReadAs<'de, T>, T: Default,

Source§

fn read<O: Options>( value: &mut Vec<T>, p: &mut Parser<'de, O>, ) -> Result<(), ErrorCode>

Source§

impl<'de, A, T> ReadAs<'de, VecDeque<T>> for VecDeque<A>
where A: ReadAs<'de, T>, T: Default,

Source§

fn read<O: Options>( value: &mut VecDeque<T>, p: &mut Parser<'de, O>, ) -> Result<(), ErrorCode>

Source§

impl<'de, KA, VA, K: Eq + Hash, V: Default, S: BuildHasher + Default> ReadAs<'de, HashMap<K, V, S>> for HashMap<KA, VA>
where KA: ReadKeyAs<K>, VA: ReadAs<'de, V>,

Source§

fn read<O: Options>( value: &mut HashMap<K, V, S>, p: &mut Parser<'de, O>, ) -> Result<(), ErrorCode>

Source§

impl<'de, KA, VA, K: Ord, V: Default> ReadAs<'de, BTreeMap<K, V>> for BTreeMap<KA, VA>
where KA: ReadKeyAs<K>, VA: ReadAs<'de, V>,

Source§

fn read<O: Options>( value: &mut BTreeMap<K, V>, p: &mut Parser<'de, O>, ) -> Result<(), ErrorCode>

Implementors§

Source§

impl<'de, T: Read<'de>> ReadAs<'de, T> for Same

Source§

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.