encdec_base/lib.rs
1//! `encdec` base traits
2//!
3
4// Support initialising fixed size arrays, requires nightly
5// TODO: remove when [rust-lang:89379](https://github.com/rust-lang/rust/issues/89379)
6#![cfg_attr(feature = "nightly", feature(array_try_from_fn))]
7#![cfg_attr(feature = "nightly", feature(associated_type_defaults))]
8#![no_std]
9
10#[cfg(feature = "std")]
11extern crate std;
12
13#[cfg(feature = "alloc")]
14extern crate alloc;
15
16pub mod encode;
17use encode::Encode;
18
19pub mod decode;
20use decode::{Decode, DecodeOwned};
21
22mod error;
23pub use error::Error;
24
25pub mod primitives;
26
27pub mod helpers;
28
29/// Composite trait requiring an object is reversibly encodable and decodable
30/// into borrowed types, useful for simplifying type bounds / generics.
31///
32/// (ie. `Self == <Self as Decode>::Output`, `<Self as Encode::Error> == <Self as Decode::Error>`)
33pub trait EncDec<'a, E = Error>: Encode<Error = E> + Decode<'a, Output = Self, Error = E> {}
34
35/// Automatic implementation for types implementing [`Encode`] and [`Decode`]
36impl<'a, T: Encode<Error = E> + Decode<'a, Output = Self, Error = E>, E> EncDec<'a, E> for T {}
37
38/// Composite trait requiring an object is reversibly encodable and decodable
39/// into owned types, useful for simplifying type bounds / generics.
40///
41/// (ie. `Self == <Self as Decode>::Output`, `<Self as Encode::Error> == <Self as DecodeOwned::Error>`)
42pub trait EncDecOwned<E = Error>:
43 Encode<Error = E> + DecodeOwned<Output = Self, Error = E>
44{
45}
46
47/// Automatic implementation for types implementing [`Encode`] and [`Decode`]
48impl<T: Encode<Error = E> + DecodeOwned<Output = Self, Error = E>, E> EncDecOwned<E> for T {}