mirror of
https://codeberg.org/icewind/bitbuffer.git
synced 2026-06-03 16:44:06 +02:00
write tuple
This commit is contained in:
parent
1fadf092cf
commit
89f9e4f0af
2 changed files with 22 additions and 0 deletions
16
src/write.rs
16
src/write.rs
|
|
@ -81,6 +81,22 @@ impl<E: Endianness, T: BitWrite<E>, const N: usize> BitWrite<E> for [T; N] {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! impl_write_tuple {
|
||||||
|
($($i:tt: $type:ident),*) => {
|
||||||
|
impl<'a, E: Endianness, $($type: BitWrite<E>),*> BitWrite<E> for ($($type),*) {
|
||||||
|
#[inline]
|
||||||
|
fn write(&self, stream: &mut BitWriteStream<E>) -> Result<()> {
|
||||||
|
$(self.$i.write(stream)?;)*
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
impl_write_tuple!(0: T1, 1: T2);
|
||||||
|
impl_write_tuple!(0: T1, 1: T2, 2: T3);
|
||||||
|
impl_write_tuple!(0: T1, 1: T2, 2: T3, 3: T4);
|
||||||
|
|
||||||
/// Trait for types that can be written to a stream, requiring the size to be configured
|
/// Trait for types that can be written to a stream, requiring the size to be configured
|
||||||
pub trait BitWriteSized<E: Endianness> {
|
pub trait BitWriteSized<E: Endianness> {
|
||||||
/// Write the type to stream
|
/// Write the type to stream
|
||||||
|
|
|
||||||
|
|
@ -96,3 +96,9 @@ fn test_array() {
|
||||||
roundtrip([1, 2, 3, 4, 5]);
|
roundtrip([1, 2, 3, 4, 5]);
|
||||||
roundtrip([String::from("asd"), String::from("foobar")]);
|
roundtrip([String::from("asd"), String::from("foobar")]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tuple() {
|
||||||
|
roundtrip((1, false));
|
||||||
|
roundtrip((1, 10.12, String::from("asd")));
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue