commonware_utils/array/
mod.rs

1use commonware_codec::{Decode, EncodeFixed};
2use std::{
3    cmp::{Ord, PartialOrd},
4    error::Error as StdError,
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;
16
17/// Errors returned by the `Array` trait's functions.
18#[derive(Error, Debug, PartialEq)]
19pub enum Error<E: StdError + Send + Sync + 'static> {
20    #[error("invalid bytes")]
21    InsufficientBytes,
22    #[error("other: {0}")]
23    Other(E),
24}
25
26/// Types that can be fallibly read from a fixed-size byte sequence.
27///
28/// `Array` is typically used to parse things like `PublicKeys` and `Signatures`
29/// from an untrusted network connection. Once parsed, these types are assumed
30/// to be well-formed (which prevents duplicate validation).
31///
32/// If a byte sequencer is not properly formatted, `TryFrom` must return an error.
33pub trait Array:
34    Clone
35    + Send
36    + Sync
37    + 'static
38    + Eq
39    + PartialEq
40    + Ord
41    + PartialOrd
42    + Debug
43    + Hash
44    + Display
45    + AsRef<[u8]>
46    + Deref<Target = [u8]>
47    + Decode<Cfg = ()>
48    + EncodeFixed
49{
50}