bounded_collections/
lib.rs1#![cfg_attr(not(feature = "std"), no_std)]
13
14pub extern crate alloc;
15
16pub mod bounded_btree_map;
17pub mod bounded_btree_set;
18pub mod bounded_vec;
19pub mod weak_bounded_vec;
20
21mod test;
22
23pub use bounded_btree_map::BoundedBTreeMap;
24pub use bounded_btree_set::BoundedBTreeSet;
25pub use bounded_vec::{BoundedSlice, BoundedVec};
26pub use weak_bounded_vec::WeakBoundedVec;
27
28pub trait TypedGet {
32 type Type;
34 fn get() -> Self::Type;
36}
37
38pub trait Get<T> {
42 fn get() -> T;
44}
45
46impl<T: Default> Get<T> for () {
47 fn get() -> T {
48 T::default()
49 }
50}
51
52pub struct GetDefault;
54impl<T: Default> Get<T> for GetDefault {
55 fn get() -> T {
56 T::default()
57 }
58}
59
60macro_rules! impl_const_get {
61 ($name:ident, $t:ty) => {
62 #[derive(Default, Clone)]
64 pub struct $name<const T: $t>;
65
66 #[cfg(feature = "std")]
67 impl<const T: $t> core::fmt::Debug for $name<T> {
68 fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
69 fmt.write_str(&format!("{}<{}>", stringify!($name), T))
70 }
71 }
72 #[cfg(not(feature = "std"))]
73 impl<const T: $t> core::fmt::Debug for $name<T> {
74 fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
75 fmt.write_str("<wasm:stripped>")
76 }
77 }
78 impl<const T: $t> Get<$t> for $name<T> {
79 fn get() -> $t {
80 T
81 }
82 }
83 impl<const T: $t> Get<Option<$t>> for $name<T> {
84 fn get() -> Option<$t> {
85 Some(T)
86 }
87 }
88 impl<const T: $t> TypedGet for $name<T> {
89 type Type = $t;
90 fn get() -> $t {
91 T
92 }
93 }
94 };
95}
96
97impl_const_get!(ConstBool, bool);
98impl_const_get!(ConstU8, u8);
99impl_const_get!(ConstU16, u16);
100impl_const_get!(ConstU32, u32);
101impl_const_get!(ConstU64, u64);
102impl_const_get!(ConstU128, u128);
103impl_const_get!(ConstI8, i8);
104impl_const_get!(ConstI16, i16);
105impl_const_get!(ConstI32, i32);
106impl_const_get!(ConstI64, i64);
107impl_const_get!(ConstI128, i128);
108
109pub trait TryCollect<C> {
111 type Error;
113 fn try_collect(self) -> Result<C, Self::Error>;
118}
119
120#[macro_export]
172macro_rules! parameter_types {
173 (
174 $( #[ $attr:meta ] )*
175 $vis:vis const $name:ident: $type:ty = $value:expr;
176 $( $rest:tt )*
177 ) => (
178 $( #[ $attr ] )*
179 $vis struct $name;
180 $crate::parameter_types!(@IMPL_CONST $name , $type , $value);
181 $crate::parameter_types!( $( $rest )* );
182 );
183 (
184 $( #[ $attr:meta ] )*
185 $vis:vis $name:ident: $type:ty = $value:expr;
186 $( $rest:tt )*
187 ) => (
188 $( #[ $attr ] )*
189 $vis struct $name;
190 $crate::parameter_types!(@IMPL $name, $type, $value);
191 $crate::parameter_types!( $( $rest )* );
192 );
193 () => ();
194 (@IMPL_CONST $name:ident, $type:ty, $value:expr) => {
195 impl $name {
196 pub const fn get() -> $type {
198 $value
199 }
200 }
201
202 impl<I: From<$type>> $crate::Get<I> for $name {
203 fn get() -> I {
204 I::from(Self::get())
205 }
206 }
207
208 impl $crate::TypedGet for $name {
209 type Type = $type;
210 fn get() -> $type {
211 Self::get()
212 }
213 }
214 };
215 (@IMPL $name:ident, $type:ty, $value:expr) => {
216 impl $name {
217 pub fn get() -> $type {
219 $value
220 }
221 }
222
223 impl<I: From<$type>> $crate::Get<I> for $name {
224 fn get() -> I {
225 I::from(Self::get())
226 }
227 }
228
229 impl $crate::TypedGet for $name {
230 type Type = $type;
231 fn get() -> $type {
232 Self::get()
233 }
234 }
235 };
236}
237
238#[macro_export]
245#[cfg(feature = "std")]
246macro_rules! bounded_vec {
247 ($ ($values:expr),* $(,)?) => {
248 {
249 $crate::alloc::vec![$($values),*].try_into().unwrap()
250 }
251 };
252 ( $value:expr ; $repetition:expr ) => {
253 {
254 $crate::alloc::vec![$value ; $repetition].try_into().unwrap()
255 }
256 }
257}
258
259#[macro_export]
266#[cfg(feature = "std")]
267macro_rules! bounded_btree_map {
268 ($ ( $key:expr => $value:expr ),* $(,)?) => {
269 {
270 $crate::TryCollect::<$crate::BoundedBTreeMap<_, _, _>>::try_collect(
271 $crate::alloc::vec![$(($key, $value)),*].into_iter()
272 ).unwrap()
273 }
274 };
275}