|
|
|
@ -197,20 +197,59 @@ pub type GenericID = uuid::Uuid;
|
|
|
|
|
/// ```
|
|
|
|
|
/// check_size!((u8, u8)); // Expected ... found one with *2* elements
|
|
|
|
|
/// ```
|
|
|
|
|
/// # Size assertions
|
|
|
|
|
/// Can also be used to statically assert the size of a type
|
|
|
|
|
/// ```
|
|
|
|
|
/// # use datse::ext::check_size;
|
|
|
|
|
/// check_size!(u16 as 2; "u16 should be 2 bytes");
|
|
|
|
|
/// check_size!(u16 where == 2; "u16 should be 2 bytes");
|
|
|
|
|
/// check_size!(u16 where < 3; "u16 should be lower than 3 bytes");
|
|
|
|
|
/// check_size!(u16 where > 1; "u16 should be larger that 1 byte");
|
|
|
|
|
/// check_size!(u16 where != 0; "u16 should be larger that 0 bytes");
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// You can also combine multiple
|
|
|
|
|
/// ```
|
|
|
|
|
/// # use datse::ext::check_size;
|
|
|
|
|
/// check_size!([u8; 512] where {
|
|
|
|
|
/// != 10;
|
|
|
|
|
/// > 511;
|
|
|
|
|
/// < 513;
|
|
|
|
|
/// == 512
|
|
|
|
|
/// });
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
/// This can be used to give you prompts as to when you might want to consider boxing a type.
|
|
|
|
|
#[macro_export] macro_rules! check_size {
|
|
|
|
|
($t:ty) => {
|
|
|
|
|
const _: [(); 0] = [(); ::std::mem::size_of::<$t>()];
|
|
|
|
|
};
|
|
|
|
|
($t:ty as $n:literal $(; $msg:literal)?) => {
|
|
|
|
|
|
|
|
|
|
($t:ty where == $n:literal $(; $msg:literal)?) => {
|
|
|
|
|
const _: [(); $n] = [(); ::std::mem::size_of::<$t>()];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
($t:ty where {$($op:tt $n:literal);+} $(; $msg:literal)?) => {
|
|
|
|
|
$(
|
|
|
|
|
$crate::static_assert!(::std::mem::size_of::<$t>() $op $n);
|
|
|
|
|
)+
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
($t:ty where $op:tt $n:literal $(; $msg:literal)?) => {
|
|
|
|
|
$crate::static_assert!(::std::mem::size_of::<$t>() $op $n $(; $msg)?);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
check_size!(u8 where == 1);
|
|
|
|
|
check_size!(u16 where > 1);
|
|
|
|
|
check_size!([u8; 512] where <= 512);
|
|
|
|
|
check_size!([u8; 512] where {
|
|
|
|
|
!= 10;
|
|
|
|
|
> 511;
|
|
|
|
|
< 513;
|
|
|
|
|
== 512
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Assert the output of a constant boolean expression is `true` at compile time.
|
|
|
|
|
#[macro_export] macro_rules! static_assert {
|
|
|
|
|
($val:expr $(; $msg:literal)?) => {
|
|
|
|
@ -299,15 +338,15 @@ impl<T> UnwrapErrInfallible<T> for Result<!, T>
|
|
|
|
|
///
|
|
|
|
|
/// Load the private module, and then re-export all its internals.
|
|
|
|
|
#[macro_export] macro_rules! submod {
|
|
|
|
|
(priv $name:path $(; $doc:literal)?) => {
|
|
|
|
|
(priv $name:ident $(; $doc:literal)?) => {
|
|
|
|
|
$(#[doc=$doc])?
|
|
|
|
|
mod $name;
|
|
|
|
|
use $name::*;
|
|
|
|
|
};
|
|
|
|
|
($vis:vis $name:path $(; $doc:literal)?) => {
|
|
|
|
|
($vis:vis $name:ident $(; $doc:literal)?) => {
|
|
|
|
|
$(#[doc=$doc])?
|
|
|
|
|
mod $name;
|
|
|
|
|
$vis use $name::*;
|
|
|
|
|
};
|
|
|
|
|
($name:path $(; $doc:literal)) => ($crate::submod!(pub $name $(; $doc)?));
|
|
|
|
|
($name:ident $(; $doc:literal)?) => ($crate::submod!(pub $name $(; $doc)?));
|
|
|
|
|
}
|
|
|
|
|