generated from PlexSheep/baserepo
Compare commits
10 commits
5dba8395d3
...
47d5ef71c7
Author | SHA1 | Date | |
---|---|---|---|
47d5ef71c7 | |||
6d73efcdfc | |||
ab2877546c | |||
8f75925ec3 | |||
22d5b3ac07 | |||
b9f55d1613 | |||
|
e9c3402aff | ||
a1c5725c55 | |||
ed0b363a99 | |||
|
86e1cc917d |
6 changed files with 67 additions and 7 deletions
|
@ -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]
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
55
members/libpt-bintols/tests/join.rs
Normal file
55
members/libpt-bintols/tests/join.rs
Normal 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]);
|
||||||
|
}
|
||||||
|
}
|
Reference in a new issue