improve hashed_storage_read
This commit is contained in:
parent
4a90440f64
commit
276d1f3f20
@ -145,7 +145,7 @@ impl MathApp {
|
||||
|
||||
fn get_storage_decompressed() -> Option<Vec<u8>> {
|
||||
let data = get_localstorage().get_item(DATA_NAME).ok()??;
|
||||
let (commit, cached_data) = crate::misc::hashed_storage_read(data)?;
|
||||
let (commit, cached_data) = crate::misc::hashed_storage_read(&data)?;
|
||||
|
||||
debug_assert!(!commit.is_empty());
|
||||
debug_assert!(!cached_data.is_empty());
|
||||
@ -183,7 +183,7 @@ impl MathApp {
|
||||
return None;
|
||||
}
|
||||
|
||||
let (commit, func_data) = crate::misc::hashed_storage_read(data)?;
|
||||
let (commit, func_data) = crate::misc::hashed_storage_read(&data)?;
|
||||
|
||||
debug_assert!(!commit.is_empty());
|
||||
debug_assert!(!func_data.is_empty());
|
||||
@ -193,7 +193,7 @@ impl MathApp {
|
||||
assume(!func_data.is_empty());
|
||||
}
|
||||
|
||||
if commit == build::SHORT_COMMIT.chars().map(|c| c as u8).collect::<Vec<u8>>().as_slice() {
|
||||
if commit == unsafe { std::mem::transmute::<&str, &[u8]>(build::SHORT_COMMIT) } {
|
||||
tracing::info!("Reading previous function data");
|
||||
let function_manager: FunctionManager = bincode::deserialize(&func_data).ok()?;
|
||||
return Some(function_manager);
|
||||
|
||||
@ -337,7 +337,7 @@ pub fn hashed_storage_create(hash: HashBytes, data: &[u8]) -> String {
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn hashed_storage_read(data: String) -> Option<(HashBytes, Vec<u8>)> {
|
||||
pub const fn hashed_storage_read(data: &str) -> Option<(HashBytes, &[u8])> {
|
||||
if HASH_LENGTH >= data.len() {
|
||||
return None;
|
||||
}
|
||||
@ -347,12 +347,9 @@ pub fn hashed_storage_read(data: String) -> Option<(HashBytes, Vec<u8>)> {
|
||||
assume(data.len() > HASH_LENGTH);
|
||||
}
|
||||
|
||||
let decoded_1: Vec<u8> = unsafe { std::mem::transmute::<String, Vec<u8>>(data) };
|
||||
let decoded_1: &[u8] = unsafe { std::mem::transmute::<&str, &[u8]>(data) };
|
||||
|
||||
let hash: HashBytes = unsafe { *(decoded_1[..HASH_LENGTH].as_ptr() as *const HashBytes) };
|
||||
let cached_data = decoded_1[HASH_LENGTH..].to_vec();
|
||||
|
||||
debug_assert!(!cached_data.is_empty());
|
||||
|
||||
Some((hash, cached_data))
|
||||
Some((hash, &decoded_1[HASH_LENGTH..]))
|
||||
}
|
||||
|
||||
@ -105,14 +105,17 @@ fn hashed_storage() {
|
||||
data.as_slice(),
|
||||
);
|
||||
|
||||
let read = hashed_storage_read(storage);
|
||||
assert_eq!(read.map(|(a, b)| (a.to_vec(), b)), Some((commit, data)));
|
||||
let read = hashed_storage_read(&storage);
|
||||
assert_eq!(
|
||||
read.map(|(a, b)| (a.to_vec(), b.to_vec())),
|
||||
Some((commit.to_vec(), data.to_vec()))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_hashed_storage() {
|
||||
use ytbn_graphing_software::hashed_storage_read;
|
||||
assert_eq!(hashed_storage_read("aaaa".to_owned()), None);
|
||||
assert_eq!(hashed_storage_read("aaaa"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user