Skip to content

Commit

Permalink
feat: Impl DekuRead and DekuWrite for ()
Browse files Browse the repository at this point in the history
  • Loading branch information
sharksforarms committed Jan 7, 2021
1 parent c2a8b3a commit 1b336c6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod nonzero;
mod option;
mod primitive;
mod slice;
mod unit;
mod vec;

#[cfg(feature = "std")]
Expand Down
44 changes: 44 additions & 0 deletions src/impls/unit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::{DekuError, DekuRead, DekuWrite};
use bitvec::prelude::*;

impl<Ctx: Copy> DekuRead<'_, Ctx> for () {
/// NOP on read
fn read(
input: &BitSlice<Msb0, u8>,
_inner_ctx: Ctx,
) -> Result<(&BitSlice<Msb0, u8>, Self), DekuError>
where
Self: Sized,
{
Ok((input, ()))
}
}

impl<Ctx: Copy> DekuWrite<Ctx> for () {
/// NOP on write
fn write(&self, _output: &mut BitVec<Msb0, u8>, _inner_ctx: Ctx) -> Result<(), DekuError> {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use hexlit::hex;

#[test]
#[allow(clippy::unit_arg)]
#[allow(clippy::unit_cmp)]
fn test_unit() {
let input = &hex!("FF");

let bit_slice = input.view_bits::<Msb0>();
let (rest, res_read) = <()>::read(bit_slice, ()).unwrap();
assert_eq!((), res_read);
assert_eq!(bit_slice, rest);

let mut res_write = bitvec![Msb0, u8;];
res_read.write(&mut res_write, ()).unwrap();
assert_eq!(0, res_write.len());
}
}

0 comments on commit 1b336c6

Please sign in to comment.