optimize bytes conversion utils
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
f43c09b05d
commit
0613140130
3 changed files with 26 additions and 18 deletions
|
@ -28,6 +28,12 @@ pub enum Error {
|
||||||
Fmt(#[from] fmt::Error),
|
Fmt(#[from] fmt::Error),
|
||||||
#[error("I/O error: {0}")]
|
#[error("I/O error: {0}")]
|
||||||
Io(#[from] std::io::Error),
|
Io(#[from] std::io::Error),
|
||||||
|
#[error("{0}")]
|
||||||
|
Utf8Error(#[from] std::str::Utf8Error),
|
||||||
|
#[error("{0}")]
|
||||||
|
FromUtf8Error(#[from] std::string::FromUtf8Error),
|
||||||
|
#[error("{0}")]
|
||||||
|
TryFromSliceError(#[from] std::array::TryFromSliceError),
|
||||||
|
|
||||||
// third-party
|
// third-party
|
||||||
#[error("Regex error: {0}")]
|
#[error("Regex error: {0}")]
|
||||||
|
|
|
@ -19,10 +19,10 @@ pub use json::{deserialize_from_str, to_canonical_object};
|
||||||
pub use mutex_map::MutexMap;
|
pub use mutex_map::MutexMap;
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
use ring::digest;
|
use ring::digest;
|
||||||
use ruma::OwnedUserId;
|
use ruma::UserId;
|
||||||
pub use sys::available_parallelism;
|
pub use sys::available_parallelism;
|
||||||
|
|
||||||
use crate::{Error, Result};
|
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) }
|
||||||
|
|
||||||
|
@ -38,9 +38,8 @@ pub fn millis_since_unix_epoch() -> u64 {
|
||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn increment(old: Option<&[u8]>) -> [u8; 8] {
|
pub fn increment(old: Option<&[u8]>) -> [u8; 8] {
|
||||||
const ZERO: u64 = 0;
|
|
||||||
old.map(TryInto::try_into)
|
old.map(TryInto::try_into)
|
||||||
.map_or(ZERO, |val| val.map_or(ZERO, u64::from_be_bytes))
|
.map_or(0_u64, |val| val.map_or(0_u64, u64::from_be_bytes))
|
||||||
.wrapping_add(1)
|
.wrapping_add(1)
|
||||||
.to_be_bytes()
|
.to_be_bytes()
|
||||||
}
|
}
|
||||||
|
@ -55,23 +54,26 @@ pub fn generate_keypair() -> Vec<u8> {
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses the bytes into an u64.
|
/// Parses a `UserId` from bytes.
|
||||||
pub fn u64_from_bytes(bytes: &[u8]) -> Result<u64, std::array::TryFromSliceError> {
|
pub fn user_id_from_bytes(bytes: &[u8]) -> Result<&UserId> {
|
||||||
let array: [u8; 8] = bytes.try_into()?;
|
let str: &str = str_from_bytes(bytes)?;
|
||||||
Ok(u64::from_be_bytes(array))
|
let user_id: &UserId = str.try_into()?;
|
||||||
|
Ok(user_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses the bytes into a string.
|
/// Parses the bytes into a string.
|
||||||
pub fn string_from_bytes(bytes: &[u8]) -> Result<String, std::string::FromUtf8Error> {
|
pub fn string_from_bytes(bytes: &[u8]) -> Result<String> {
|
||||||
String::from_utf8(bytes.to_vec())
|
let str: &str = str_from_bytes(bytes)?;
|
||||||
|
Ok(str.to_owned())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses a `OwnedUserId` from bytes.
|
/// Parses the bytes into a string.
|
||||||
pub fn user_id_from_bytes(bytes: &[u8]) -> Result<OwnedUserId> {
|
pub fn str_from_bytes(bytes: &[u8]) -> Result<&str> { Ok(std::str::from_utf8(bytes)?) }
|
||||||
OwnedUserId::try_from(
|
|
||||||
string_from_bytes(bytes).map_err(|_| Error::bad_database("Failed to parse string from bytes"))?,
|
/// Parses the bytes into an u64.
|
||||||
)
|
pub fn u64_from_bytes(bytes: &[u8]) -> Result<u64> {
|
||||||
.map_err(|_| Error::bad_database("Failed to parse user id from bytes"))
|
let array: [u8; 8] = bytes.try_into()?;
|
||||||
|
Ok(u64::from_be_bytes(array))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn random_string(length: usize) -> String {
|
pub fn random_string(length: usize) -> String {
|
||||||
|
|
|
@ -115,7 +115,7 @@ impl Data {
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|(key, presence_bytes)| -> Result<(OwnedUserId, u64, Vec<u8>)> {
|
.flat_map(|(key, presence_bytes)| -> Result<(OwnedUserId, u64, Vec<u8>)> {
|
||||||
let (count, user_id) = presenceid_parse(&key)?;
|
let (count, user_id) = presenceid_parse(&key)?;
|
||||||
Ok((user_id, count, presence_bytes))
|
Ok((user_id.to_owned(), count, presence_bytes))
|
||||||
})
|
})
|
||||||
.filter(move |(_, count, _)| *count > since),
|
.filter(move |(_, count, _)| *count > since),
|
||||||
)
|
)
|
||||||
|
@ -128,7 +128,7 @@ fn presenceid_key(count: u64, user_id: &UserId) -> Vec<u8> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn presenceid_parse(key: &[u8]) -> Result<(u64, OwnedUserId)> {
|
fn presenceid_parse(key: &[u8]) -> Result<(u64, &UserId)> {
|
||||||
let (count, user_id) = key.split_at(8);
|
let (count, user_id) = key.split_at(8);
|
||||||
let user_id = utils::user_id_from_bytes(user_id)?;
|
let user_id = utils::user_id_from_bytes(user_id)?;
|
||||||
let count = utils::u64_from_bytes(count).unwrap();
|
let count = utils::u64_from_bytes(count).unwrap();
|
||||||
|
|
Loading…
Add table
Reference in a new issue