raad crate for eating and pushing bytes
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs28
1 files changed, 28 insertions, 0 deletions
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") {