less unsafe

master
Avril 4 years ago
parent a773324eb1
commit 13265c8529
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -1,7 +1,7 @@
[package]
name = "khash"
description = "Kana hashes"
version = "2.0.0"
version = "2.0.1"
authors = ["Avril <flanchan@cumallover.me>"]
edition = "2018"
license = "GPL-3.0-or-later"

@ -206,7 +206,7 @@ fn compute<T: Read>(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)?;
}

@ -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::<T>());
&std::slice::from_raw_parts(&src[0] as *const u8 as *const T, 1)[0]
}

@ -15,7 +15,7 @@ where I: Iterator,
type Item = u16;
fn next(&mut self) -> Option<Self::Item>
{
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::<u16>()];
if let Some(a) = self.iter.next()
{
ar[0] = *a.borrow();
} else {
return None;
}
if let Some(b) = self.iter.next()
{
ar[1] = *b.borrow();
}
Some(c)
Some(u16::from_le_bytes(ar))
}
}

Loading…
Cancel
Save