Combinator

Trait Combinator 

Source
pub trait Combinator<'x, I, O>: View
where I: VestInput, O: VestOutput<I>, Self::V: SecureSpecCombinator<Type = <Self::Type as View>::V>,
{ type Type: View; type SType: View<V = <Self::Type as View>::V>; // Required methods exec fn length(&self, v: Self::SType) -> len : usize; exec fn parse(&self, s: I) -> res : PResult<Self::Type, ParseError>; exec fn serialize( &self, v: Self::SType, buf: &mut O, pos: usize, ) -> res : SResult<usize, SerializeError>; // Provided method open spec fn ex_requires(&self) -> bool { ... } }
Expand description

Implementation for parser and serializer combinators. A combinator’s view must be a SecureSpecCombinator.

Required Associated Types§

Source

type Type: View

The result type of parsing

Source

type SType: View<V = <Self::Type as View>::V>

The input type of serialization, often a reference to Self::Type. For “structural” formats though (e.g., crate::regular::sequence::Pair and crate::regular::variant::Choice), this is the tuple/sum of the corresponding Combinator::SType types.

Required Methods§

Source

exec fn length(&self, v: Self::SType) -> len : usize

requires
self@.requires(),
self.ex_requires(),
self@.wf(v@),
self@.spec_serialize(v@).len() <= usize::MAX,
ensures
len == self@.spec_serialize(v@).len(),

The length of the output buffer. This can be used to optimize serialization by pre-allocating the buffer.

Source

exec fn parse(&self, s: I) -> res : PResult<Self::Type, ParseError>

requires
self@.requires(),
self.ex_requires(),
s@.len() <= usize::MAX,
ensures
res matches Ok((n, v)) ==> self@.spec_parse(s@) == Some((n as int, v@)),
self@.spec_parse(s@) matches Some((n, v)) ==> res matches Ok((m, u)) && m == n && v == u@,
res is Err ==> self@.spec_parse(s@) is None,
self@.spec_parse(s@) is None ==> res is Err,

The parsing function. To enable “zero-copy” parsing, implementations of parse should not consume/deepcopy the input buffer I, but rather return a slice of the input buffer for Self::Type whenever possible. See crate::buf_traits::VestInput and crate::buf_traits::VestPublicInput for more details.

§Pre-conditions
§Post-conditions

Essentially, the implementation of parse is functionally correct with respect to the specification spec_parse on both success and failure cases.

Source

exec fn serialize( &self, v: Self::SType, buf: &mut O, pos: usize, ) -> res : SResult<usize, SerializeError>

requires
self@.requires(),
self.ex_requires(),
self@.wf(v@),
pos <= old(buf)@.len() <= usize::MAX,
ensures
res matches Ok(
    n,
) ==> {
    &&& buf@.len() == old(buf)@.len()
    &&& pos <= usize::MAX - n && pos + n <= buf@.len()
    &&& n == self@.spec_serialize(v@).len()
    &&& buf@ == seq_splice(old(buf)@, pos, self@.spec_serialize(v@))

},

The serialization function. The intended use of serialize is to serialize a value v into the buffer buf at the position pos “in-place” (i.e., without allocating a new buffer or extending the buffer).

§Pre-conditions
§Post-conditions

serialize ensures that it is functionally correct with respect to the specification spec_serialize when it returns Ok. This is because serialize being a partial function can fail (e.g., insufficient buffer space), while spec_serialize is a total function (with infinite buffer size) and will never fail.

Provided Methods§

Source

open spec fn ex_requires(&self) -> bool

{ true }

Additional pre-conditions for parsing and serialization

e.g., crate::regular::sequence::Pair combinator requires that the crate::regular::sequence::Continuation is well-formed w.r.t. the spec_fn closure.

Implementations on Foreign Types§

Source§

impl<'x, Fst, Snd, I, O> Combinator<'x, I, O> for (Fst, Snd)
where I: VestInput, O: VestOutput<I>, Fst: Combinator<'x, I, O>, Snd: Combinator<'x, I, O>, Fst::V: SecureSpecCombinator<Type = <Fst::Type as View>::V>, Snd::V: SecureSpecCombinator<Type = <Snd::Type as View>::V>,

Source§

exec fn length(&self, v: Self::SType) -> usize

Source§

open spec fn ex_requires(&self) -> bool

{ self.0.ex_requires() && self.1.ex_requires() }
Source§

exec fn parse(&self, s: I) -> res : Result<(usize, Self::Type), ParseError>

Source§

exec fn serialize( &self, v: Self::SType, data: &mut O, pos: usize, ) -> res : Result<usize, SerializeError>

Source§

type Type = (<Fst as Combinator<'x, I, O>>::Type, <Snd as Combinator<'x, I, O>>::Type)

Source§

type SType = (<Fst as Combinator<'x, I, O>>::SType, <Snd as Combinator<'x, I, O>>::SType)

Source§

impl<'x, I, O, C: Combinator<'x, I, O>> Combinator<'x, I, O> for &C
where I: VestInput, O: VestOutput<I>, C::V: SecureSpecCombinator<Type = <C::Type as View>::V>,

Source§

exec fn length(&self, v: Self::SType) -> usize

Source§

open spec fn ex_requires(&self) -> bool

{ (*self).ex_requires() }
Source§

exec fn parse(&self, s: I) -> res : Result<(usize, Self::Type), ParseError>

Source§

exec fn serialize( &self, v: Self::SType, data: &mut O, pos: usize, ) -> res : Result<usize, SerializeError>

Source§

type Type = <C as Combinator<'x, I, O>>::Type

Source§

type SType = <C as Combinator<'x, I, O>>::SType

Source§

impl<'x, I, O, C: Combinator<'x, I, O>> Combinator<'x, I, O> for Box<C>
where I: VestInput, O: VestOutput<I>, C::V: SecureSpecCombinator<Type = <C::Type as View>::V>,

Source§

exec fn length(&self, v: Self::SType) -> usize

Source§

open spec fn ex_requires(&self) -> bool

{ (**self).ex_requires() }
Source§

exec fn parse(&self, s: I) -> res : Result<(usize, Self::Type), ParseError>

Source§

exec fn serialize( &self, v: Self::SType, data: &mut O, pos: usize, ) -> res : Result<usize, SerializeError>

Source§

type Type = <C as Combinator<'x, I, O>>::Type

Source§

type SType = <C as Combinator<'x, I, O>>::SType

Implementors§

Source§

impl<'a> Combinator<'a, &'a [u8], Vec<u8>> for BtcVarint

Source§

impl<'x> Combinator<'x, &[u8], Vec<u8>> for U24Be

Source§

impl<'x> Combinator<'x, &[u8], Vec<u8>> for U24Le

Source§

impl<'x, I, O> Combinator<'x, I, O> for Variable
where I: VestInput + 'x, O: VestOutput<I>,

Source§

impl<'x, I, O> Combinator<'x, I, O> for UnsignedLEB128

Source§

impl<'x, I, O> Combinator<'x, I, O> for Tag<UnsignedLEB128, UInt>

Source§

impl<'x, I, O> Combinator<'x, I, O> for Tag<U8, u8>

Source§

impl<'x, I, O> Combinator<'x, I, O> for Tag<U16Be, u16>

Source§

impl<'x, I, O> Combinator<'x, I, O> for Tag<U16Le, u16>

Source§

impl<'x, I, O> Combinator<'x, I, O> for Tag<U32Be, u32>

Source§

impl<'x, I, O> Combinator<'x, I, O> for Tag<U32Le, u32>

Source§

impl<'x, I, O> Combinator<'x, I, O> for Tag<U64Be, u64>

Source§

impl<'x, I, O> Combinator<'x, I, O> for Tag<U64Le, u64>

Source§

impl<'x, I, O, C> Combinator<'x, I, O> for Repeat<C>
where I: VestInput, O: VestOutput<I>, C: Combinator<'x, I, O, SType = &'x <C as Combinator<'x, I, O>>::Type>, C::V: SecureSpecCombinator<Type = <C::Type as View>::V>, <C as Combinator<'x, I, O>>::Type: 'x,

Source§

type Type = RepeatResult<<C as Combinator<'x, I, O>>::Type>

Source§

type SType = &'x RepeatResult<<C as Combinator<'x, I, O>>::Type>

Source§

impl<'x, I, O, C> Combinator<'x, I, O> for RepeatN<C>
where I: VestInput, O: VestOutput<I>, C: Combinator<'x, I, O, SType = &'x <C as Combinator<'x, I, O>>::Type>, C::V: SecureSpecCombinator<Type = <C::Type as View>::V>, <C as Combinator<'x, I, O>>::Type: 'x,

Source§

type Type = RepeatResult<<C as Combinator<'x, I, O>>::Type>

Source§

type SType = &'x RepeatResult<<C as Combinator<'x, I, O>>::Type>

Source§

impl<'x, I, O, Fst, Snd> Combinator<'x, I, O> for Preceded<Fst, Snd>
where I: VestInput, O: VestOutput<I>, Fst: Combinator<'x, I, O, Type = (), SType = ()>, Snd: Combinator<'x, I, O>, Fst::V: SecureSpecCombinator<Type = ()>, Snd::V: SecureSpecCombinator<Type = <Snd::Type as View>::V>,

Source§

type Type = <Snd as Combinator<'x, I, O>>::Type

Source§

type SType = <Snd as Combinator<'x, I, O>>::SType

Source§

impl<'x, I, O, Fst, Snd> Combinator<'x, I, O> for Terminated<Fst, Snd>
where I: VestInput, O: VestOutput<I>, Fst: Combinator<'x, I, O>, Snd: Combinator<'x, I, O, Type = (), SType = ()>, Fst::V: SecureSpecCombinator<Type = <Fst::Type as View>::V>, Snd::V: SecureSpecCombinator<Type = ()>,

Source§

type Type = <Fst as Combinator<'x, I, O>>::Type

Source§

type SType = <Fst as Combinator<'x, I, O>>::SType

Source§

impl<'x, I, O, Fst, Snd> Combinator<'x, I, O> for Choice<Fst, Snd>
where I: VestInput, O: VestOutput<I>, Fst: Combinator<'x, I, O>, Snd: Combinator<'x, I, O>, Fst::V: SecureSpecCombinator<Type = <Fst::Type as View>::V>, Snd::V: SecureSpecCombinator<Type = <Snd::Type as View>::V> + DisjointFrom<Fst::V>,

Source§

type Type = Either<<Fst as Combinator<'x, I, O>>::Type, <Snd as Combinator<'x, I, O>>::Type>

Source§

type SType = Either<<Fst as Combinator<'x, I, O>>::SType, <Snd as Combinator<'x, I, O>>::SType>

Source§

impl<'x, I, O, Fst, Snd> Combinator<'x, I, O> for OptThen<Fst, Snd>
where I: VestInput, O: VestOutput<I>, Fst: Combinator<'x, I, O, SType = &'x <Fst as Combinator<'x, I, O>>::Type>, Snd: Combinator<'x, I, O>, Fst::V: SecureSpecCombinator<Type = <Fst::Type as View>::V>, Snd::V: SecureSpecCombinator<Type = <Snd::Type as View>::V> + DisjointFrom<Fst::V>, Fst::Type: 'x,

Source§

type Type = (<Opt<Fst> as Combinator<'x, I, O>>::Type, <Snd as Combinator<'x, I, O>>::Type)

Source§

type SType = (<Opt<Fst> as Combinator<'x, I, O>>::SType, <Snd as Combinator<'x, I, O>>::SType)

Source§

impl<'x, I, O, Fst, Snd, Cont> Combinator<'x, I, O> for Pair<Fst, Snd, Cont>
where I: VestInput, O: VestOutput<I>, Fst: Combinator<'x, I, O>, Snd: Combinator<'x, I, O>, Fst::V: SecureSpecCombinator<Type = <Fst::Type as View>::V>, Snd::V: SecureSpecCombinator<Type = <Snd::Type as View>::V>, Fst::SType: Copy, Cont: for<'a> Continuation<POrSType<&'a Fst::Type, Fst::SType>, Output = Snd> + View<V = GhostFn<<Fst::Type as View>::V, Snd::V>>, <Fst as Combinator<'x, I, O>>::Type: 'x,

Source§

type Type = (<Fst as Combinator<'x, I, O>>::Type, <Snd as Combinator<'x, I, O>>::Type)

Source§

type SType = (<Fst as Combinator<'x, I, O>>::SType, <Snd as Combinator<'x, I, O>>::SType)

Source§

impl<'x, I, O, Inner, M> Combinator<'x, I, O> for Mapped<Inner, M>
where I: VestInput, O: VestOutput<I>, Inner: Combinator<'x, I, O>, Inner::V: SecureSpecCombinator<Type = <Inner::Type as View>::V>, M: Iso<'x, Src = Inner::Type, RefSrc = Inner::SType>, M::Dst: From<Inner::Type> + View, Inner::SType: From<&'x M::Dst> + View, M::V: SpecIsoProof<Src = <Inner::Type as View>::V, Dst = <M::Dst as View>::V>, <Inner::Type as View>::V: SpecFrom<<M::Dst as View>::V>, <M::Dst as View>::V: SpecFrom<<Inner::Type as View>::V>,

Source§

type Type = <M as Iso<'x>>::Dst

Source§

type SType = &'x <M as Iso<'x>>::Dst

Source§

impl<'x, I, O, Inner, M> Combinator<'x, I, O> for TryMap<Inner, M>
where I: VestInput, O: VestOutput<I>, Inner: Combinator<'x, I, O>, Inner::V: SecureSpecCombinator<Type = <Inner::Type as View>::V>, M: PartialIso<'x, Src = Inner::Type, RefSrc = Inner::SType>, M::Dst: TryFrom<Inner::Type> + View, Inner::SType: TryFrom<&'x M::Dst> + View, M::V: SpecPartialIsoProof<Src = <Inner::Type as View>::V, Dst = <M::Dst as View>::V>, <Inner::Type as View>::V: SpecTryFrom<<M::Dst as View>::V>, <M::Dst as View>::V: SpecTryFrom<<Inner::Type as View>::V>, <Inner::SType as TryFrom<&'x M::Dst>>::Error: Debug,

Source§

type Type = <M as PartialIso<'x>>::Dst

Source§

type SType = &'x <M as PartialIso<'x>>::Dst

Source§

impl<'x, I, O, Inner, P> Combinator<'x, I, O> for Refined<Inner, P>
where I: VestInput, O: VestOutput<I>, Inner: Combinator<'x, I, O, SType = &'x <Inner as Combinator<'x, I, O>>::Type>, Inner::V: SecureSpecCombinator<Type = <Inner::Type as View>::V>, P: Pred<Inner::Type>, P::V: SpecPred<<Inner::Type as View>::V>, Inner::Type: 'x,

Source§

type Type = <Inner as Combinator<'x, I, O>>::Type

Source§

type SType = <Inner as Combinator<'x, I, O>>::SType

Source§

impl<'x, I, O, Next: Combinator<'x, I, O>> Combinator<'x, I, O> for AndThen<Variable, Next>
where I: VestInput, O: VestOutput<I>, Next::V: SecureSpecCombinator<Type = <Next::Type as View>::V>,

Source§

type Type = <Next as Combinator<'x, I, O>>::Type

Source§

type SType = <Next as Combinator<'x, I, O>>::SType

Source§

impl<'x, I, O, T> Combinator<'x, I, O> for Opt<T>
where I: VestInput, O: VestOutput<I>, T: Combinator<'x, I, O, SType = &'x <T as Combinator<'x, I, O>>::Type>, T::V: SecureSpecCombinator<Type = <T::Type as View>::V>, T::Type: 'x,

Source§

type Type = Optional<<T as Combinator<'x, I, O>>::Type>

Source§

type SType = &'x <Opt<T> as Combinator<'x, I, O>>::Type

Source§

impl<'x, I: VestInput + 'x, O: VestOutput<I>> Combinator<'x, I, O> for Tail

Source§

impl<'x, I: VestInput, O: VestOutput<I>> Combinator<'x, I, O> for End

Source§

impl<'x, I: VestInput, O: VestOutput<I>> Combinator<'x, I, O> for Fail

Source§

impl<'x, I: VestInput, O: VestOutput<I>> Combinator<'x, I, O> for Success

Source§

impl<'x, I: VestInput, O: VestOutput<I>, Inner: Combinator<'x, I, O>> Combinator<'x, I, O> for Cond<Inner>
where Inner::V: SecureSpecCombinator<Type = <Inner::Type as View>::V>,

Source§

type Type = <Inner as Combinator<'x, I, O>>::Type

Source§

type SType = <Inner as Combinator<'x, I, O>>::SType

Source§

impl<'x, I: VestPublicInput, O: VestPublicOutput<I>> Combinator<'x, I, O> for U8

Source§

type Type = u8

Source§

type SType = &'x u8

Source§

impl<'x, I: VestPublicInput, O: VestPublicOutput<I>> Combinator<'x, I, O> for U16Be

Source§

impl<'x, I: VestPublicInput, O: VestPublicOutput<I>> Combinator<'x, I, O> for U16Le

Source§

impl<'x, I: VestPublicInput, O: VestPublicOutput<I>> Combinator<'x, I, O> for U32Be

Source§

impl<'x, I: VestPublicInput, O: VestPublicOutput<I>> Combinator<'x, I, O> for U32Le

Source§

impl<'x, I: VestPublicInput, O: VestPublicOutput<I>> Combinator<'x, I, O> for U64Be

Source§

impl<'x, I: VestPublicInput, O: VestPublicOutput<I>> Combinator<'x, I, O> for U64Le

Source§

impl<'x, const N: usize> Combinator<'x, &'x [u8], Vec<u8>> for Tag<Fixed<N>, [u8; N]>

Source§

impl<'x, const N: usize, I, O> Combinator<'x, I, O> for Fixed<N>
where I: VestInput + 'x, O: VestOutput<I>,