Skip to main content

substrate_data/schema/plain/
dynamic.rs

1//! The schemas of a value of any shape, `bool` and `()`.
2
3use crate::binary::{Decoder, Writer};
4use crate::frame::{Packer, Unpacker};
5use crate::model::borrowed::ValueRef;
6use crate::model::value::Value;
7use crate::schema::{Expected, Reader, Schema, Shape};
8
9impl Schema for Value {
10    fn shape() -> Shape {
11        Shape::Any
12    }
13    fn read(v: &ValueRef<'_>, _: &mut Reader<'_>) -> Option<Self> {
14        Some(v.to_value())
15    }
16    fn write(&self) -> Value {
17        self.clone()
18    }
19}
20
21impl Schema for bool {
22    fn shape() -> Shape {
23        Shape::Bool
24    }
25    fn read(v: &ValueRef<'_>, cx: &mut Reader<'_>) -> Option<Self> {
26        v.as_bool().or_else(|| {
27            cx.expected(Expected::Bool, v);
28            None
29        })
30    }
31    fn write(&self) -> Value {
32        Value::Bool(*self)
33    }
34    fn encode(&self, w: &mut Writer) {
35        w.bool(*self);
36    }
37    fn decode(d: &mut Decoder<'_>) -> Option<Self> {
38        d.bool()
39    }
40    fn pack(&self, p: &mut Packer) {
41        p.put_bool(*self);
42    }
43    fn unpack(u: &mut Unpacker<'_>) -> Option<Self> {
44        u.bool()
45    }
46}
47
48/// A number as carried (an `i64`) back to `T`: out of `T`'s range, none.
49/// Nothing: what a call answers when it only says it's done.
50impl Schema for () {
51    fn shape() -> Shape {
52        Shape::Any
53    }
54    fn read(_v: &ValueRef<'_>, _cx: &mut Reader<'_>) -> Option<Self> {
55        Some(())
56    }
57    fn write(&self) -> Value {
58        Value::Null
59    }
60}