commonware_utils/sequence/
mod.rs

1use commonware_codec::{Codec, EncodeFixed};
2use core::{
3    cmp::{Ord, PartialOrd},
4    error::Error as CoreError,
5    fmt::{Debug, Display},
6    hash::Hash,
7    ops::Deref,
8};
9use thiserror::Error;
10
11pub mod fixed_bytes;
12pub use fixed_bytes::FixedBytes;
13pub mod u64;
14pub use u64::U64;
15pub mod prefixed_u64;
16pub mod u32;
17pub use u32::U32;
18
19/// Errors returned by the `Array` trait's functions.
20#[derive(Error, Debug, PartialEq)]
21pub enum Error<E: CoreError + Send + Sync + 'static> {
22    #[error("invalid bytes")]
23    InsufficientBytes,
24    #[error("other: {0}")]
25    Other(E),
26}
27
28/// Types that can be read from a variable-size byte sequence.
29///
30/// `Span` is typically used to parse things like requests from an untrusted
31/// network connection (with variable-length fields). Once parsed, these types
32/// are assumed to be well-formed (which prevents duplicate validation).
33pub trait Span:
34    Clone
35    + Send
36    + Sync
37    + 'static
38    + Eq
39    + PartialEq
40    + Ord
41    + PartialOrd
42    + Debug
43    + Hash
44    + Display
45    + Codec<Cfg = ()>
46{
47}
48
49/// Types that can be fallibly read from a fixed-size byte sequence.
50///
51/// `Array` is typically used to parse things like `PublicKeys` and `Signatures`
52/// from an untrusted network connection. Once parsed, these types are assumed
53/// to be well-formed (which prevents duplicate validation).
54pub trait Array: Span + EncodeFixed + AsRef<[u8]> + Deref<Target = [u8]> {}