Compare commits

...

10 commits

Author SHA1 Message Date
5dba8395d3 chore: bump version to v0.5.1
All checks were successful
cargo devel CI / cargo CI (push) Successful in 1m47s
2024-05-13 15:25:15 +02:00
0e1cbf7daa Merge pull request 'update to v0.5.1' (#81) from devel into master
Reviewed-on: #81
2024-05-13 15:25:15 +02:00
738946c2ae Merge branch 'master' into devel 2024-05-13 15:25:15 +02:00
409ba93bb9 Merge pull request 'feat/bintols/join-array' (#80) from feat/bintols/join-array into devel
Reviewed-on: #80
2024-05-13 15:25:15 +02:00
7928423d6e refactor(bintols-join): return err if the result cannot be converted to T #80 #79 2024-05-13 15:25:15 +02:00
3d25040743 Merge branch 'devel' into feat/bintols/join-array 2024-05-13 15:25:15 +02:00
PlexSheep
53e545832c automatic cargo CI changes 2024-05-13 15:25:15 +02:00
3ef1ffc337 test: add tests for the join module #79 2024-05-13 15:25:14 +02:00
ed0b363a99 Merge pull request 'libpt 0.5' (#78) from devel into master
All checks were successful
cargo devel CI / cargo CI (push) Successful in 1m47s
Reviewed-on: #78
2024-05-12 18:28:12 +02:00
PlexSheep
86e1cc917d automatic cargo CI changes 2024-05-12 16:21:23 +00:00
6 changed files with 67 additions and 7 deletions

View file

@ -10,7 +10,7 @@ default-members = [".", "members/libpt-core"]
[workspace.package] [workspace.package]
publish = true publish = true
version = "0.5.0" version = "0.5.1"
edition = "2021" edition = "2021"
authors = ["Christoph J. Scherr <software@cscherr.de>"] authors = ["Christoph J. Scherr <software@cscherr.de>"]
license = "MIT" license = "MIT"
@ -29,7 +29,7 @@ categories = [
anyhow = "1.0.79" anyhow = "1.0.79"
thiserror = "1.0.56" thiserror = "1.0.56"
libpt-core = { version = "0.4.0", path = "members/libpt-core" } libpt-core = { version = "0.4.0", path = "members/libpt-core" }
libpt-bintols = { version = "0.5.0", path = "members/libpt-bintols" } libpt-bintols = { version = "0.5.1", path = "members/libpt-bintols" }
libpt-log = { version = "0.4.2", path = "members/libpt-log" } libpt-log = { version = "0.4.2", path = "members/libpt-log" }
[package] [package]

View file

@ -1,7 +1,7 @@
[package] [package]
name = "libpt-bintols" name = "libpt-bintols"
publish.workspace = true publish.workspace = true
version = "0.5.0" version = "0.5.1"
edition.workspace = true edition.workspace = true
authors.workspace = true authors.workspace = true
license.workspace = true license.workspace = true

View file

@ -27,7 +27,13 @@ pub fn array_to_unsigned<T>(parts: &[u8]) -> anyhow::Result<T>
where where
u128: std::convert::From<T>, u128: std::convert::From<T>,
T: std::str::FromStr, T: std::str::FromStr,
T: std::convert::TryFrom<u128>,
<T as std::str::FromStr>::Err: std::fmt::Debug, <T as std::str::FromStr>::Err: std::fmt::Debug,
<T as std::str::FromStr>::Err: std::error::Error,
<T as std::convert::TryFrom<u128>>::Error: std::error::Error,
<T as std::convert::TryFrom<u128>>::Error: std::marker::Send,
<T as std::convert::TryFrom<u128>>::Error: std::marker::Sync,
<T as std::convert::TryFrom<u128>>::Error: 'static,
{ {
trace!("amount of parts: {}", parts.len()); trace!("amount of parts: {}", parts.len());
if parts.len() > (u128::BITS / 8) as usize { if parts.len() > (u128::BITS / 8) as usize {
@ -40,5 +46,5 @@ where
for (i, e) in parts.iter().rev().enumerate() { for (i, e) in parts.iter().rev().enumerate() {
ri += (*e as u128) * 256u128.pow(i as u32); ri += (*e as u128) * 256u128.pow(i as u32);
} }
Ok(ri.to_string().parse().unwrap()) T::try_from(ri).map_err(anyhow::Error::from)
} }

View file

@ -25,5 +25,5 @@ pub const YOBI: u128 = 2u128.pow(80);
// use libpt_core; // use libpt_core;
pub mod datalayout; pub mod datalayout;
pub mod display; pub mod display;
pub mod split;
pub mod join; pub mod join;
pub mod split;

View file

@ -3,7 +3,6 @@
//! Sometimes, you need a large integer in the form of many bytes, so split into [u8]. //! Sometimes, you need a large integer in the form of many bytes, so split into [u8].
//! Rust provides //! Rust provides
/// Split unsigned integers into a [Vec] of [u8]s /// Split unsigned integers into a [Vec] of [u8]s
/// ///
/// Say you have the [u32] 1717 (binary: `00000000 00000000 00000110 10110101 `). This number would /// Say you have the [u32] 1717 (binary: `00000000 00000000 00000110 10110101 `). This number would
@ -23,7 +22,7 @@
/// ``` /// ```
pub fn unsigned_to_vec<T>(num: T) -> Vec<u8> pub fn unsigned_to_vec<T>(num: T) -> Vec<u8>
where where
u128: std::convert::From<T> u128: std::convert::From<T>,
{ {
let mut num: u128 = num.into(); let mut num: u128 = num.into();
if num == 0 { if num == 0 {

View file

@ -0,0 +1,55 @@
use libpt_bintols::join::*;
#[test]
fn join_u128() {
let correct = [
16,
255,
256,
0,
u128::MAX,
u64::MAX as u128,
u64::MAX as u128 + 1,
];
let source = [
vec![16],
vec![255],
vec![1, 0],
vec![0],
vec![
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
],
vec![255, 255, 255, 255, 255, 255, 255, 255],
vec![1, 0, 0, 0, 0, 0, 0, 0, 0],
];
for (i, n) in source.iter().enumerate() {
assert_eq!(array_to_unsigned::<u128>(n).unwrap(), correct[i]);
}
}
#[test]
fn join_u64() {
let correct = [
16,
255,
256,
0,
u64::MAX,
u32::MAX as u64,
0b1_00000001,
0b10011011_10110101_11110000_00110011,
];
let source = [
vec![16],
vec![255],
vec![1, 0],
vec![0],
vec![255, 255, 255, 255, 255, 255, 255, 255],
vec![255, 255, 255, 255],
vec![1, 1],
vec![0b10011011, 0b10110101, 0b11110000, 0b00110011],
];
for (i, n) in source.iter().enumerate() {
assert_eq!(array_to_unsigned::<u64>(n).unwrap(), correct[i]);
}
}