Skip to content

Commit

Permalink
migrate uX -> nsw-types
Browse files Browse the repository at this point in the history
  • Loading branch information
bbaldino committed Jul 9, 2024
1 parent 1c84322 commit 19c1ca8
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ edition = "2021"

[dependencies]
paste = "1"
ux = { version = "0.1.6", git = "https://github.com/bbaldino/uX.git", branch = "master", features = ["num-traits"] }
nsw-types = { version = "0.1.7", features = ["num-traits", "std"] }
num-traits = "0.2"
bitvec = "1.0.1"
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# BitCursor

BitCursor is similar to std::io::Cursor, but allows reading various amounts of bits from a given buffer in addition
to byte-sized chunks. It's built on top of the [ux](https://crates.io/crates/ux) crate for types and leverages
to byte-sized chunks. It's built on top of the [nsw_types](https://crates.io/crates/nsw-types) crate for types and leverages
[bitvec](https://docs.rs/bitvec/latest/bitvec/) to provide a more complete implementation.

# Examples
Expand All @@ -12,12 +12,12 @@ let mut cursor = BitCursor::from_vec(data);

// Read any non-standard-width type from the cursor
let u3_val = cursor.read_u3().unwrap();
assert_eq!(u3_val, ux::u3::new(0b111));
assert_eq!(u3_val, nsw_types::u3::new(0b111));
// Sizes larger than 8 bits require a byte order argument
let u13_val = cursor
.read_u13::<crate::byte_order::NetworkOrder>()
.unwrap();
assert_eq!(u13_val, ux::u13::new(0b0000011101111));
assert_eq!(u13_val, nsw_types::u13::new(0b0000011101111));
```

# Design
Expand All @@ -35,4 +35,4 @@ assert_eq!(u13_val, ux::u13::new(0b0000011101111));
## Types

### `BitCursor`
`BitCursor` is analgous to the [`std::io::Cursor`](https://doc.rust-lang.org/std/io/struct.Cursor.html) type, but its API is defined in terms of bits instead of bytes.
`BitCursor` is analogous to the [`std::io::Cursor`](https://doc.rust-lang.org/std/io/struct.Cursor.html) type, but its API is defined in terms of bits instead of bytes.
12 changes: 6 additions & 6 deletions src/bit_cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,23 +241,23 @@ impl Read for BitCursor<&BitSlice<u8, Msb0>> {
}

impl BitRead for BitCursor<BitVec<u8, Msb0>> {
fn read_bits(&mut self, buf: &mut [ux::u1]) -> std::io::Result<usize> {
fn read_bits(&mut self, buf: &mut [nsw_types::u1]) -> std::io::Result<usize> {
let n = BitRead::read_bits(&mut self.remaining_slice(), buf)?;
self.pos += n as u64;
Ok(n)
}
}

impl BitRead for BitCursor<&BitSlice<u8, Msb0>> {
fn read_bits(&mut self, buf: &mut [ux::u1]) -> std::io::Result<usize> {
fn read_bits(&mut self, buf: &mut [nsw_types::u1]) -> std::io::Result<usize> {
let n = BitRead::read_bits(&mut self.remaining_slice(), buf)?;
self.pos += n as u64;
Ok(n)
}
}

impl BitRead for BitCursor<&[u8]> {
fn read_bits(&mut self, buf: &mut [ux::u1]) -> std::io::Result<usize> {
fn read_bits(&mut self, buf: &mut [nsw_types::u1]) -> std::io::Result<usize> {
let n = BitRead::read_bits(&mut self.remaining_slice(), buf)?;
self.pos += n as u64;
Ok(n)
Expand Down Expand Up @@ -309,15 +309,15 @@ impl Write for BitCursor<&mut BitSlice<u8, Msb0>> {
}

impl BitWrite for BitCursor<BitVec<u8, Msb0>> {
fn write_bits(&mut self, buf: &[ux::u1]) -> std::io::Result<usize> {
fn write_bits(&mut self, buf: &[nsw_types::u1]) -> std::io::Result<usize> {
let n = BitWrite::write_bits(&mut self.remaining_slice_mut(), buf)?;
self.pos += n as u64;
Ok(n)
}
}

impl BitWrite for BitCursor<&mut BitSlice<u8, Msb0>> {
fn write_bits(&mut self, buf: &[ux::u1]) -> std::io::Result<usize> {
fn write_bits(&mut self, buf: &[nsw_types::u1]) -> std::io::Result<usize> {
let n = BitWrite::write_bits(&mut self.inner, buf)?;
self.pos += n as u64;
Ok(n)
Expand All @@ -338,7 +338,7 @@ mod test {
use std::io::{Seek, SeekFrom};

use bitvec::{bits, order::Msb0, vec::BitVec};
use ux::u1;
use nsw_types::u1;

use crate::{bit_read::BitRead, bit_read_exts::BitReadExts};

Expand Down
2 changes: 1 addition & 1 deletion src/bit_read.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ux::u1;
use nsw_types::u1;

pub trait BitRead: std::io::Read {
/// Pull some bits from this source into the specified buffer, returning how many bytes were read.
Expand Down
4 changes: 2 additions & 2 deletions src/bit_read_exts.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ops::{BitOrAssign, ShlAssign};

use nsw_types::*;
use num_traits::ConstZero;
use ux::*;

use crate::{bit_read::BitRead, byte_order::ByteOrder};

Expand All @@ -13,7 +13,7 @@ fn bit_read_exts_helper<
buf: &mut U,
) -> std::io::Result<T> {
// TODO: it'd be nice not to do this bit-by-bit. I think once we get the from_xxx_bytes methods
// in ux those could work here.
// in nsw_types those could work here.
let mut read_buf = [u1::ZERO; N];
buf.read_bits_exact(&mut read_buf)?;
let mut val = T::ZERO;
Expand Down
2 changes: 1 addition & 1 deletion src/bit_slice_traits.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bitvec::{order::Msb0, slice::BitSlice};
use ux::u1;
use nsw_types::u1;

use crate::{bit_read::BitRead, bit_write::BitWrite};

Expand Down
2 changes: 1 addition & 1 deletion src/bit_write.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ux::u1;
use nsw_types::u1;

pub trait BitWrite: std::io::Write {
/// Write a buffer into this writer, returning how many bytes were written.
Expand Down
2 changes: 1 addition & 1 deletion src/bit_write_exts.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::ops::{BitAnd, ShrAssign};

use crate::{bit_write::BitWrite, byte_order::ByteOrder};
use nsw_types::*;
use num_traits::{ConstOne, ConstZero};
use ux::*;

fn bit_write_exts_helper<T, const N: usize, U>(buf: &mut U, mut value: T) -> std::io::Result<()>
where
Expand Down
18 changes: 9 additions & 9 deletions src/byte_order.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use nsw_types::*;
use paste::paste;
use ux::*;

const U1_ONE: u1 = u1::new(1);

// Helper macro to provide consistent syntax for initializing both uX and standard integer types
// Helper macro to provide consistent syntax for initializing both nsw_types and standard integer types
// TODO: better to just define a trait that implements ONE and ZERO for all types?
macro_rules! init_integer {
(u16, $value:expr) => {
Expand All @@ -17,36 +17,36 @@ macro_rules! init_integer {
};
}

/// Generate a LittleEndian read operation from a buffer for the uX types
/// Generate a LittleEndian read operation from a buffer for the nsw_types types
macro_rules! impl_read_le {
($type:ty, $size_bits:expr) => {
paste! {
fn [<read_ $type>](buf: &[u1; $size_bits]) -> $type {
let mut val = <$type>::default();
if $size_bits > 32 {
unimplemented!("Only uX types up to 32 bits supported");
unimplemented!("Only nsw_types types up to 32 bits supported");
}
if $size_bits > 24 {
for i in 24..std::cmp::min($size_bits, 32) {
val <<= 1;
val |= <ux::u1 as Into<$type>>::into(buf[i]);
val |= <nsw_types::u1 as Into<$type>>::into(buf[i]);
}
}
if $size_bits > 16 {
for i in 16..std::cmp::min($size_bits, 24) {
val <<= 1;
val |= <ux::u1 as Into<$type>>::into(buf[i]);
val |= <nsw_types::u1 as Into<$type>>::into(buf[i]);
}
}
if $size_bits > 8 {
for i in 8..std::cmp::min($size_bits, 16) {
val <<= 1;
val |= <ux::u1 as Into<$type>>::into(buf[i]);
val |= <nsw_types::u1 as Into<$type>>::into(buf[i]);
}
}
for i in 0..std::cmp::min($size_bits, 8) {
val <<= 1;
val |= <ux::u1 as Into<$type>>::into(buf[i]);
val |= <nsw_types::u1 as Into<$type>>::into(buf[i]);
}
val
}
Expand All @@ -61,7 +61,7 @@ macro_rules! impl_read_be {
let mut val = <$type>::default();
for bit in buf.iter() {
val <<= 1;
val |= <ux::u1 as Into<$type>>::into(*bit);
val |= <nsw_types::u1 as Into<$type>>::into(*bit);
}
val
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ pub mod bit_write;
pub mod bit_write_exts;
pub mod byte_order;

pub use ux;
pub use nsw_types;

0 comments on commit 19c1ca8

Please sign in to comment.