Skip to content

Commit

Permalink
use shifting and remove bitvec dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
gvaradarajan committed Jan 17, 2025
1 parent 189157e commit 84ad67e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 27 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ async-std-openssl = "0.6.3"
atomic-waker = "1.1.2"
base64 = "0.22.1"
bitfield = "0.17.0"
bitvec = "1.0.1"
bytecodec = "0.4.15"
bytes = "1.9.0"
cargo_metadata = "0.19.1"
Expand Down
1 change: 0 additions & 1 deletion micro-rdk-nmea/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ rust-version.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bitvec = { workspace = true }
micro-rdk = { workspace = true, features = ["native"] }
thiserror = { workspace = true }
36 changes: 12 additions & 24 deletions micro-rdk-nmea/src/parse_helpers/parsers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use bitvec::prelude::*;
use std::{
marker::PhantomData,
rc::Rc,
Expand Down Expand Up @@ -110,33 +109,21 @@ macro_rules! generate_number_field_readers {
($($t:ty),*) => {
$(
impl TryFrom<DataRead<'_>> for $t {
type Error = NumberFieldError;
type Error = NmeaParseError;
fn try_from(value: DataRead) -> Result<Self, Self::Error> {
let max_size = std::mem::size_of::<Self>();
if (value.bit_size / 8) > max_size {
return Err(NumberFieldError::ImproperBitSize(value.bit_size, max_size * 8));
Err(NumberFieldError::ImproperBitSize(value.bit_size, max_size * 8).into())
} else if value.bit_size == (max_size * 8) {
Ok(<$t>::from_le_bytes(value.data[..].try_into()?))
} else {
let byte_idx = value.bit_size / 8;
let data_cpy = &mut value.data[..].to_vec();
data_cpy.resize(max_size, 0);
let shift = 8 - (value.bit_size % 8);
data_cpy[byte_idx] = data_cpy[byte_idx] >> shift;
Ok(<$t>::from_le_bytes(data_cpy[..].try_into()?))
}

let bits = &value.data[..].view_bits::<Msb0>();

let start_idx = value.bit_position.load(Ordering::SeqCst) % 8;
let end_idx = start_idx + value.bit_size;
let mut bit_vec = bits[start_idx..end_idx].to_bitvec();
if bit_vec.len() != (max_size * 8) {
// the last byte is incomplete, we reverse the remaining bits of the
// incomplete byte so it can be padded with zeros, then we reverse the bits
// of the now complete last byte for the proper bit order
let last_bit_start = bit_vec.len() - (value.bit_size % 8);
let _ = &bit_vec[last_bit_start..].reverse();

for _ in (0..(max_size * 8 - value.bit_size)) {
bit_vec.push(false);
}

let _ = &bit_vec[last_bit_start..(last_bit_start + 8)].reverse();
}

Ok(bit_vec.load_le::<$t>())
}
}

Expand Down Expand Up @@ -305,6 +292,7 @@ mod tests {

// 154 is 10011010, reading the first 3 bits should yield 100 = 4
let res = reader.read_from_cursor(&cursor);
println!("res: {:?}", res);
assert!(res.is_ok());
assert_eq!(res.unwrap(), 4);
}
Expand Down

0 comments on commit 84ad67e

Please sign in to comment.