From 3beeab54c665b64eeee1c88db49fb1bc2f025480 Mon Sep 17 00:00:00 2001 From: Avril Date: Fri, 20 Nov 2020 01:39:09 +0000 Subject: [PATCH] check_size --- Cargo.lock | 30 +++++++++++++++--------------- src/ext.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 60 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 31f80e3..00d3bcc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -139,9 +139,9 @@ checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" [[package]] name = "cc" -version = "1.0.62" +version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1770ced377336a88a67c473594ccc14eca6f4559217c34f64aac8f83d641b40" +checksum = "ad9c6140b5a2c7db40ea56eb1821245e5362b44385c05b76288b1a599934ac87" [[package]] name = "cfg-if" @@ -452,7 +452,7 @@ dependencies = [ "futures-sink", "futures-task", "memchr", - "pin-project 1.0.1", + "pin-project 1.0.2", "pin-utils", "proc-macro-hack", "proc-macro-nested", @@ -656,7 +656,7 @@ dependencies = [ "httparse", "httpdate", "itoa", - "pin-project 1.0.1", + "pin-project 1.0.2", "socket2", "tokio", "tower-service", @@ -1016,11 +1016,11 @@ dependencies = [ [[package]] name = "pin-project" -version = "1.0.1" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee41d838744f60d959d7074e3afb6b35c7456d0f61cad38a24e35e6553f73841" +checksum = "9ccc2237c2c489783abd8c4c80e5450fc0e98644555b1364da68cc29aa151ca7" dependencies = [ - "pin-project-internal 1.0.1", + "pin-project-internal 1.0.2", ] [[package]] @@ -1036,9 +1036,9 @@ dependencies = [ [[package]] name = "pin-project-internal" -version = "1.0.1" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81a4ffa594b66bff340084d4081df649a7dc049ac8d7fc458d8e628bfbbb2f86" +checksum = "f8e8d2bf0b23038a4424865103a4df472855692821aab4e4f5c3312d461d9e5f" dependencies = [ "proc-macro2", "quote", @@ -1519,9 +1519,9 @@ dependencies = [ [[package]] name = "termcolor" -version = "1.1.0" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f" +checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" dependencies = [ "winapi-util", ] @@ -1548,9 +1548,9 @@ dependencies = [ [[package]] name = "tinyvec" -version = "1.0.1" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b78a366903f506d2ad52ca8dc552102ffdd3e937ba8a227f024dc1d1eae28575" +checksum = "ccf8dbc19eb42fba10e8feaaec282fb50e2c14b2726d6301dbfeed0f73306a6f" dependencies = [ "tinyvec_macros", ] @@ -1720,9 +1720,9 @@ dependencies = [ [[package]] name = "unicode-normalization" -version = "0.1.14" +version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7f98e67a4d84f730d343392f9bfff7d21e3fca562b9cb7a43b768350beeddc6" +checksum = "a13e63ab62dbe32aeee58d1c5408d35c36c392bba5d9d3142287219721afe606" dependencies = [ "tinyvec", ] diff --git a/src/ext.rs b/src/ext.rs index 1e542b1..50d05d6 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -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 UnwrapErrInfallible for Result /// /// 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)?)); }