#![cfg_attr(not(feature = "std"), no_std)]
pub extern crate alloc;
pub mod bounded_btree_map;
pub mod bounded_btree_set;
pub mod bounded_vec;
pub(crate) mod codec_utils;
pub mod const_int;
pub mod weak_bounded_vec;
mod test;
pub use bounded_btree_map::BoundedBTreeMap;
pub use bounded_btree_set::BoundedBTreeSet;
pub use bounded_vec::{BoundedSlice, BoundedVec};
pub use const_int::{ConstInt, ConstUint};
pub use weak_bounded_vec::WeakBoundedVec;
pub trait TypedGet {
type Type;
fn get() -> Self::Type;
}
pub trait Get<T> {
fn get() -> T;
}
impl<T: Default> Get<T> for () {
fn get() -> T {
T::default()
}
}
pub struct GetInto<Inner, I>(core::marker::PhantomData<(Inner, I)>);
impl<Inner, I, R> Get<R> for GetInto<Inner, I>
where
Inner: Get<I>,
I: Into<R>,
{
fn get() -> R {
Inner::get().into()
}
}
pub struct GetDefault;
impl<T: Default> Get<T> for GetDefault {
fn get() -> T {
T::default()
}
}
macro_rules! impl_const_get {
($name:ident, $t:ty) => {
#[derive(Default, Clone)]
pub struct $name<const T: $t>;
#[cfg(feature = "std")]
impl<const T: $t> core::fmt::Debug for $name<T> {
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
fmt.write_str(&format!("{}<{}>", stringify!($name), T))
}
}
#[cfg(not(feature = "std"))]
impl<const T: $t> core::fmt::Debug for $name<T> {
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
fmt.write_str("<wasm:stripped>")
}
}
impl<const T: $t> Get<$t> for $name<T> {
fn get() -> $t {
T
}
}
impl<const T: $t> Get<Option<$t>> for $name<T> {
fn get() -> Option<$t> {
Some(T)
}
}
impl<const T: $t> TypedGet for $name<T> {
type Type = $t;
fn get() -> $t {
T
}
}
};
}
impl_const_get!(ConstBool, bool);
impl_const_get!(ConstU8, u8);
impl_const_get!(ConstU16, u16);
impl_const_get!(ConstU32, u32);
impl_const_get!(ConstU64, u64);
impl_const_get!(ConstU128, u128);
impl_const_get!(ConstI8, i8);
impl_const_get!(ConstI16, i16);
impl_const_get!(ConstI32, i32);
impl_const_get!(ConstI64, i64);
impl_const_get!(ConstI128, i128);
pub trait TryCollect<C> {
type Error;
fn try_collect(self) -> Result<C, Self::Error>;
}
#[macro_export]
macro_rules! parameter_types {
(
$( #[ $attr:meta ] )*
$vis:vis const $name:ident: $type:ty = $value:expr;
$( $rest:tt )*
) => (
$( #[ $attr ] )*
$vis struct $name;
$crate::parameter_types!(@IMPL_CONST $name , $type , $value);
$crate::parameter_types!( $( $rest )* );
);
(
$( #[ $attr:meta ] )*
$vis:vis $name:ident: $type:ty = $value:expr;
$( $rest:tt )*
) => (
$( #[ $attr ] )*
$vis struct $name;
$crate::parameter_types!(@IMPL $name, $type, $value);
$crate::parameter_types!( $( $rest )* );
);
() => ();
(@IMPL_CONST $name:ident, $type:ty, $value:expr) => {
impl $name {
pub const fn get() -> $type {
$value
}
}
impl<I: From<$type>> $crate::Get<I> for $name {
fn get() -> I {
I::from(Self::get())
}
}
impl $crate::TypedGet for $name {
type Type = $type;
fn get() -> $type {
Self::get()
}
}
};
(@IMPL $name:ident, $type:ty, $value:expr) => {
impl $name {
pub fn get() -> $type {
$value
}
}
impl<I: From<$type>> $crate::Get<I> for $name {
fn get() -> I {
I::from(Self::get())
}
}
impl $crate::TypedGet for $name {
type Type = $type;
fn get() -> $type {
Self::get()
}
}
};
}
#[macro_export]
#[cfg(feature = "std")]
macro_rules! bounded_vec {
($ ($values:expr),* $(,)?) => {
{
$crate::alloc::vec![$($values),*].try_into().unwrap()
}
};
( $value:expr ; $repetition:expr ) => {
{
$crate::alloc::vec![$value ; $repetition].try_into().unwrap()
}
}
}
#[macro_export]
#[cfg(feature = "std")]
macro_rules! bounded_btree_map {
($ ( $key:expr => $value:expr ),* $(,)?) => {
{
$crate::TryCollect::<$crate::BoundedBTreeMap<_, _, _>>::try_collect(
$crate::alloc::vec![$(($key, $value)),*].into_iter()
).unwrap()
}
};
}