raad crate for eating and pushing bytes
try the diagnostics
bendn 2024-05-04
parent 4aa4318 · commit 6c77f66
-rw-r--r--Cargo.toml2
-rw-r--r--src/lib.rs28
2 files changed, 29 insertions, 1 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 4081473..6963393 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "raad"
-version = "0.1.0"
+version = "0.1.1"
authors = ["bend-n <[email protected]>"]
license = "MIT"
edition = "2021"
diff --git a/src/lib.rs b/src/lib.rs
index 1a8375b..9c661bd 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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") {