typeface/
walue.rs

1//! Types that can be read and written given a parameter.
2
3use crate::Result;
4
5/// A type that can be read given a parameter.
6pub trait Read<'l>: Sized {
7    /// The parameter type.
8    type Parameter;
9
10    /// Read a value.
11    fn read<T: crate::tape::Read>(_: &mut T, _: Self::Parameter) -> Result<Self>;
12}
13
14/// A type that can be written given a parameter.
15pub trait Write<'l> {
16    /// The parameter type.
17    type Parameter;
18
19    /// Write the value.
20    fn write<T: crate::tape::Write>(&self, _: &mut T, _: Self::Parameter) -> Result<()>;
21}
22
23impl<V> Read<'static> for Vec<V>
24where
25    V: crate::value::Read,
26{
27    type Parameter = usize;
28
29    fn read<T: crate::tape::Read>(tape: &mut T, count: usize) -> Result<Self> {
30        let mut values = Vec::with_capacity(count);
31        for _ in 0..count {
32            values.push(crate::value::Read::read(tape)?);
33        }
34        Ok(values)
35    }
36}