This commit is contained in:
Simon Gardling 2022-05-17 10:15:45 -04:00
parent 149adfd614
commit 4227437475
4 changed files with 19 additions and 18 deletions

View File

@ -1,4 +1,4 @@
use std::intrinsics::assume;
use std::{hint::unreachable_unchecked, intrinsics::assume};
use crate::{generate_hint, Hint, HINT_EMPTY};
@ -61,13 +61,16 @@ impl<'a> AutoComplete<'a> {
#[allow(dead_code)]
pub fn register_movement(&mut self, movement: &Movement) {
if movement.is_none() {
if movement.is_none() | self.hint.is_none() {
return;
}
match self.hint {
Hint::Many(hints) => {
// Impossible for plural hints to be singular or non-existant
debug_assert!(hints.len() > 1); // check on debug
// Hint to the compiler
unsafe {
assume(hints.len() > 1);
assume(!hints.is_empty());
@ -75,14 +78,15 @@ impl<'a> AutoComplete<'a> {
match movement {
Movement::Up => {
// if self.i is below 1, it's at
match self.i {
0 => self.i = hints.len() - 1,
_ => self.i -= 1,
// Wrap self.i to maximum `i` value if needed
if self.i == 0 {
self.i = hints.len() - 1;
} else {
self.i -= 1;
}
}
Movement::Down => {
// add one, if resulting value is above maximum i value, set i to 0
// Add one, if resulting value is above maximum `i` value, set `i` to 0
self.i += 1;
if self.i > (hints.len() - 1) {
self.i = 0;
@ -91,7 +95,7 @@ impl<'a> AutoComplete<'a> {
Movement::Complete => {
self.apply_hint(unsafe { hints.get_unchecked(self.i) });
}
_ => unreachable!(),
_ => unsafe { unreachable_unchecked() },
}
}
Hint::Single(hint) => {
@ -99,7 +103,7 @@ impl<'a> AutoComplete<'a> {
self.apply_hint(hint);
}
}
Hint::None => {}
Hint::None => unsafe { unreachable_unchecked() },
}
}

View File

@ -146,11 +146,7 @@ impl MathApp {
fn get_storage_decompressed() -> Option<Vec<u8>> {
let data = get_localstorage().get_item(DATA_NAME).ok()??;
if crate::misc::HASH_LENGTH >= data.len() {
return None;
}
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());
@ -188,7 +184,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());

View File

@ -324,7 +324,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 fn hashed_storage_read(data: &str) -> Option<(HashBytes, Vec<u8>)> {
if HASH_LENGTH >= data.len() {
return None;
}
@ -335,6 +335,7 @@ pub fn hashed_storage_read(data: String) -> Option<(HashBytes, Vec<u8>)> {
}
// can't use data.as_bytes() here for some reason, seems to break on wasm?
// Other memory trickery seems to not worm on wasm. so I was unable to implement a more effecient manner of doing this
let decoded_1 = data.chars().map(|c| c as u8).collect::<Vec<u8>>();
let (hash, cached_data) = {

View File

@ -105,12 +105,12 @@ fn hashed_storage() {
data.as_slice(),
);
let read = hashed_storage_read(storage);
let read = hashed_storage_read(&storage);
assert_eq!(read.map(|(a, b)| (a.to_vec(), b)), Some((commit, data)));
}
#[test]
fn invalid_hashed_storage() {
use ytbn_graphing_software::hashed_storage_read;
assert_eq!(hashed_storage_read(String::from("aaaa")), None);
assert_eq!(hashed_storage_read("aaaa"), None);
}