split bytes utils into unit
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
af81baae44
commit
3b9fba233c
4 changed files with 45 additions and 31 deletions
29
src/core/utils/bytes.rs
Normal file
29
src/core/utils/bytes.rs
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
use crate::Result;
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
|
pub fn increment(old: Option<&[u8]>) -> [u8; 8] {
|
||||||
|
old.map(TryInto::try_into)
|
||||||
|
.map_or(0_u64, |val| val.map_or(0_u64, u64::from_be_bytes))
|
||||||
|
.wrapping_add(1)
|
||||||
|
.to_be_bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parses the big-endian bytes into an u64.
|
||||||
|
#[inline]
|
||||||
|
pub fn u64_from_bytes(bytes: &[u8]) -> Result<u64> {
|
||||||
|
let array: [u8; 8] = bytes.try_into()?;
|
||||||
|
Ok(u64_from_u8x8(array))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parses the 8 big-endian bytes into an u64.
|
||||||
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
|
pub fn u64_from_u8(bytes: &[u8]) -> u64 {
|
||||||
|
let bytes: &[u8; 8] = bytes.try_into().expect("must slice at least 8 bytes");
|
||||||
|
u64_from_u8x8(*bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
|
pub fn u64_from_u8x8(bytes: [u8; 8]) -> u64 { u64::from_be_bytes(bytes) }
|
|
@ -12,4 +12,5 @@ pub fn verify_password(password: &str, password_hash: &str) -> Result<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[must_use]
|
||||||
pub fn calculate_hash(keys: &[&[u8]]) -> Vec<u8> { sha256::hash(keys) }
|
pub fn calculate_hash(keys: &[&[u8]]) -> Vec<u8> { sha256::hash(keys) }
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
pub mod bytes;
|
||||||
pub mod content_disposition;
|
pub mod content_disposition;
|
||||||
pub mod debug;
|
pub mod debug;
|
||||||
pub mod defer;
|
pub mod defer;
|
||||||
|
@ -14,6 +15,7 @@ use std::{
|
||||||
time::{SystemTime, UNIX_EPOCH},
|
time::{SystemTime, UNIX_EPOCH},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub use bytes::{increment, u64_from_bytes, u64_from_u8, u64_from_u8x8};
|
||||||
pub use debug::slice_truncated as debug_slice_truncated;
|
pub use debug::slice_truncated as debug_slice_truncated;
|
||||||
pub use hash::calculate_hash;
|
pub use hash::calculate_hash;
|
||||||
pub use html::Escape as HtmlEscape;
|
pub use html::Escape as HtmlEscape;
|
||||||
|
@ -26,6 +28,18 @@ use crate::Result;
|
||||||
|
|
||||||
pub fn clamp<T: Ord>(val: T, min: T, max: T) -> T { cmp::min(cmp::max(val, min), max) }
|
pub fn clamp<T: Ord>(val: T, min: T, max: T) -> T { cmp::min(cmp::max(val, min), max) }
|
||||||
|
|
||||||
|
/// Boilerplate for wraps which are typed to never error.
|
||||||
|
///
|
||||||
|
/// * <https://doc.rust-lang.org/std/convert/enum.Infallible.html>
|
||||||
|
#[must_use]
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn unwrap_infallible<T>(result: Result<T, std::convert::Infallible>) -> T {
|
||||||
|
match result {
|
||||||
|
Ok(val) => val,
|
||||||
|
Err(err) => match err {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[allow(clippy::as_conversions)]
|
#[allow(clippy::as_conversions)]
|
||||||
pub fn millis_since_unix_epoch() -> u64 {
|
pub fn millis_since_unix_epoch() -> u64 {
|
||||||
|
@ -35,15 +49,6 @@ pub fn millis_since_unix_epoch() -> u64 {
|
||||||
.as_millis() as u64
|
.as_millis() as u64
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
#[must_use]
|
|
||||||
pub fn increment(old: Option<&[u8]>) -> [u8; 8] {
|
|
||||||
old.map(TryInto::try_into)
|
|
||||||
.map_or(0_u64, |val| val.map_or(0_u64, u64::from_be_bytes))
|
|
||||||
.wrapping_add(1)
|
|
||||||
.to_be_bytes()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn generate_keypair() -> Vec<u8> {
|
pub fn generate_keypair() -> Vec<u8> {
|
||||||
let mut value = random_string(8).as_bytes().to_vec();
|
let mut value = random_string(8).as_bytes().to_vec();
|
||||||
|
@ -54,15 +59,6 @@ pub fn generate_keypair() -> Vec<u8> {
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses the bytes into an u64.
|
|
||||||
pub fn u64_from_bytes(bytes: &[u8]) -> Result<u64> {
|
|
||||||
let array: [u8; 8] = bytes.try_into()?;
|
|
||||||
Ok(u64::from_be_bytes(array))
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#[allow(clippy::impl_trait_in_params)]
|
#[allow(clippy::impl_trait_in_params)]
|
||||||
pub fn common_elements(
|
pub fn common_elements(
|
||||||
mut iterators: impl Iterator<Item = impl Iterator<Item = Vec<u8>>>, check_order: impl Fn(&[u8], &[u8]) -> Ordering,
|
mut iterators: impl Iterator<Item = impl Iterator<Item = Vec<u8>>>, check_order: impl Fn(&[u8], &[u8]) -> Ordering,
|
||||||
|
@ -86,15 +82,3 @@ pub fn common_elements(
|
||||||
})
|
})
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Boilerplate for wraps which are typed to never error.
|
|
||||||
///
|
|
||||||
/// * <https://doc.rust-lang.org/std/convert/enum.Infallible.html>
|
|
||||||
#[must_use]
|
|
||||||
#[inline(always)]
|
|
||||||
pub fn unwrap_infallible<T>(result: Result<T, std::convert::Infallible>) -> T {
|
|
||||||
match result {
|
|
||||||
Ok(val) => val,
|
|
||||||
Err(err) => match err {},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -38,7 +38,7 @@ impl Data {
|
||||||
.map(|chain| {
|
.map(|chain| {
|
||||||
chain
|
chain
|
||||||
.chunks_exact(size_of::<u64>())
|
.chunks_exact(size_of::<u64>())
|
||||||
.map(|chunk| utils::u64_from_bytes(chunk).expect("byte length is correct"))
|
.map(utils::u64_from_u8)
|
||||||
.collect::<Arc<[u64]>>()
|
.collect::<Arc<[u64]>>()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue