From 13265c8529cbee795eecb1ae08098388d8d5eb95 Mon Sep 17 00:00:00 2001 From: Avril Date: Sat, 6 Feb 2021 20:47:37 +0000 Subject: [PATCH] less unsafe --- Cargo.toml | 2 +- src/lib.rs | 2 +- src/reinterpret.rs | 3 --- src/sixteen.rs | 15 +++++++++++++-- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 963846d..4cea998 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "khash" description = "Kana hashes" -version = "2.0.0" +version = "2.0.1" authors = ["Avril "] edition = "2018" license = "GPL-3.0-or-later" diff --git a/src/lib.rs b/src/lib.rs index 9ec827e..851a69a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -206,7 +206,7 @@ fn compute(context: &ctx::Context, mut from: T) -> Result<(usize, Strin let mut output = String::with_capacity(128); for element in hash.into_iter() .into_16() - .map(|bytes| mnemonic::Digest::new(unsafe{reinterpret::bytes(&bytes)})) + .map(|bytes| mnemonic::Digest::new(&u16::to_le_bytes(bytes)[..]))//unsafe{reinterpret::bytes(&bytes)})) { write!(output, "{}", element)?; } diff --git a/src/reinterpret.rs b/src/reinterpret.rs index 98e39bd..0d79a75 100644 --- a/src/reinterpret.rs +++ b/src/reinterpret.rs @@ -1,5 +1,3 @@ - - pub unsafe fn bytes<'a, T>(src: &'a T) -> &'a [u8] where T: ?Sized { @@ -17,4 +15,3 @@ pub unsafe fn value<'a, T>(src: &[u8]) -> &'a T assert!(src.len() >= std::mem::size_of::()); &std::slice::from_raw_parts(&src[0] as *const u8 as *const T, 1)[0] } - diff --git a/src/sixteen.rs b/src/sixteen.rs index 32d2364..da6d1df 100644 --- a/src/sixteen.rs +++ b/src/sixteen.rs @@ -15,7 +15,7 @@ where I: Iterator, type Item = u16; fn next(&mut self) -> Option { - let mut c = 0u16; + /*let mut c = 0u16; unsafe { if let Some(a) = self.iter.next() { crate::reinterpret::bytes_mut(&mut c)[0] = *a.borrow(); @@ -25,8 +25,19 @@ where I: Iterator, if let Some(b) = self.iter.next() { crate::reinterpret::bytes_mut(&mut c)[1] = *b.borrow(); } + }*/ + let mut ar = [0u8; std::mem::size_of::()]; + if let Some(a) = self.iter.next() + { + ar[0] = *a.borrow(); + } else { + return None; } - Some(c) + if let Some(b) = self.iter.next() + { + ar[1] = *b.borrow(); + } + Some(u16::from_le_bytes(ar)) } }