Skip to content

Commit

Permalink
Import TryInto
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Nov 8, 2023
1 parent 5945455 commit a9e2d1c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/hermit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::Error;
use core::{cmp::min, mem::MaybeUninit, num::NonZeroU32};
use core::{convert::TryInto, mem::MaybeUninit, num::NonZeroU32};

extern "C" {
fn sys_read_entropy(buffer: *mut u8, length: usize, flags: u32) -> isize;
Expand All @@ -17,8 +17,7 @@ pub fn getrandom_inner(mut dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
.unwrap_or(Error::UNEXPECTED);
return Err(err);
}
let len = min(res as usize, dest.len());
dest = dest.get_mut(len..).ok_or(Error::UNEXPECTED)?;
dest = dest.get_mut(res as usize..).ok_or(Error::UNEXPECTED)?;
}
Ok(())
}
10 changes: 5 additions & 5 deletions src/util_libc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#![allow(dead_code)]
use crate::Error;
use core::{
cmp::min,
mem::MaybeUninit,
num::NonZeroU32,
ptr::NonNull,
Expand Down Expand Up @@ -70,17 +69,18 @@ pub fn sys_fill_exact(
) -> Result<(), Error> {
while !buf.is_empty() {
let res = sys_fill(buf);
if res < 0 {
if res > 0 {
buf = buf.get_mut(res as usize..).ok_or(Error::UNEXPECTED)?;
} else if res < 0 {
let err = last_os_error();
// We should try again if the call was interrupted.
if err.raw_os_error() != Some(libc::EINTR) {
return Err(err);
}
} else {
// We don't check for EOF (ret = 0) as the data we are reading
// EOF (ret = 0) should be impossible, as the data we are reading
// should be an infinite stream of random bytes.
let len = min(res as usize, buf.len());
buf = buf.get_mut(len..).ok_or(Error::UNEXPECTED)?;
return Err(Error::UNEXPECTED);
}
}
Ok(())
Expand Down

0 comments on commit a9e2d1c

Please sign in to comment.