raad crate for eating and pushing bytes
try the diagnostics
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/lib.rs | 28 |
2 files changed, 29 insertions, 1 deletions
@@ -1,6 +1,6 @@ [package] name = "raad" -version = "0.1.0" +version = "0.1.1" authors = ["bend-n <[email protected]>"] license = "MIT" edition = "2021" @@ -61,6 +61,11 @@ macro_rules! trt { T::r(self) } } + #[diagnostic::on_unimplemented( + message = "this type is not suitable for reading into", + note = "read to [u8; N] first and then parse it", + label = "unreadable type" + )] trait Readable where Self: Sized, @@ -75,6 +80,12 @@ macro_rules! trt { } } + impl<const N: usize> Writable for &[u8; N] { + fn _w(self, to: &mut impl Write) -> Result<()> { + to.w(&self[..]) + } + } + $(#[doc = $y])+ pub trait W: Write { /// Writes a type to a [`Writer`](Write) @@ -87,6 +98,11 @@ macro_rules! trt { data._w(self) } } + #[diagnostic::on_unimplemented( + message = "this type is not suitable for writing", + note = "turn it into a &[u8] first and then write that", + label = "unwritable type", + )] trait Writable { fn _w(self, to: &mut impl Write) -> Result<()>; } @@ -102,6 +118,12 @@ macro_rules! trt { macro_rules! n { (writes $bytes:ident $($n:ident)+) => { $( + impl<const N: usize> Writable for &[$n; N] { + fn _w(self, to: &mut impl Write) -> Result<()> { + to.w(&self[..]) + } + } + impl Writable for &[$n] { fn _w(self, to: &mut impl Write) -> Result<()> { if (cfg!(target_endian = "little") && stringify!($bytes) == "le") || (cfg!(target_endian = "big") && stringify!($bytes) == "be") { @@ -168,6 +190,12 @@ macro_rules! n { } } + impl<const N: usize> Writable for &[$n; N] { + fn _w(self, to: &mut impl Write) -> Result<()> { + to.w(&self[..]) + } + } + impl Writable for &[$n] { fn _w(self, to: &mut impl Write) -> Result<()> { if (cfg!(target_endian = "little") && stringify!($bytes) == "le") || (cfg!(target_endian = "big") && stringify!($bytes) == "be") { |