Skip to main content

commonware_utils/sequence/
mod.rs

1use commonware_codec::{Codec, EncodeFixed};
2use core::{
3    cmp::Ord,
4    fmt::{Debug, Display},
5    hash::Hash,
6    ops::Deref,
7};
8
9pub mod fixed_bytes;
10pub use fixed_bytes::FixedBytes;
11pub mod u64;
12pub use u64::U64;
13pub mod prefixed_u64;
14pub mod vec_u64;
15pub use vec_u64::VecU64;
16pub mod u32;
17pub use u32::U32;
18pub mod unit;
19pub use unit::Unit;
20
21/// Types that can be read from a variable-size byte sequence.
22///
23/// `Span` is typically used to parse things like requests from an untrusted
24/// network connection (with variable-length fields). Once parsed, these types
25/// are assumed to be well-formed (which prevents duplicate validation).
26pub trait Span:
27    Clone + Send + Sync + 'static + Ord + Debug + Hash + Display + Codec<Cfg = ()>
28{
29}
30
31impl Span for u8 {}
32impl Span for u16 {}
33impl Span for u32 {}
34impl Span for u64 {}
35impl Span for u128 {}
36impl Span for i8 {}
37impl Span for i16 {}
38impl Span for i32 {}
39impl Span for i64 {}
40impl Span for i128 {}
41
42/// Types that can be fallibly read from a fixed-size byte sequence.
43///
44/// `Array` is typically used to parse things like `PublicKeys` and `Signatures`
45/// from an untrusted network connection. Once parsed, these types are assumed
46/// to be well-formed (which prevents duplicate validation).
47pub trait Array: Span + EncodeFixed + AsRef<[u8]> + Deref<Target = [u8]> {}