Skip to content

Commit 11eefaa

Browse files
josephlrnewpavlov
authored andcommitted
Use shared File instead of shared FD. (#45)
1 parent 31ce376 commit 11eefaa

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ impl From<NonZeroU32> for Error {
7474
}
7575
}
7676

77+
impl From<&Error> for Error {
78+
fn from(error: &Error) -> Self {
79+
*error
80+
}
81+
}
82+
7783
#[cfg(test)]
7884
mod tests {
7985
use super::Error;

src/use_file.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use lazy_static::lazy_static;
1515
use std::{
1616
fs::File,
1717
io::Read,
18-
os::unix::io::{FromRawFd, IntoRawFd, RawFd},
1918
};
2019

2120
#[cfg(target_os = "redox")]
@@ -33,9 +32,9 @@ const FILE_PATH: &str = "/dev/random";
3332

3433
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
3534
lazy_static! {
36-
static ref RNG_FD: Result<RawFd, Error> = init_file();
35+
static ref FILE: Result<File, Error> = init_file();
3736
}
38-
let mut f = unsafe { File::from_raw_fd((*RNG_FD)?) };
37+
let mut f = FILE.as_ref()?;
3938

4039
if cfg!(target_os = "emscripten") {
4140
// `Crypto.getRandomValues` documents `dest` should be at most 65536 bytes.
@@ -48,12 +47,12 @@ pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
4847
Ok(())
4948
}
5049

51-
fn init_file() -> Result<RawFd, Error> {
50+
fn init_file() -> Result<File, Error> {
5251
if FILE_PATH == "/dev/urandom" {
5352
// read one byte from "/dev/random" to ensure that OS RNG has initialized
5453
File::open("/dev/random")?.read_exact(&mut [0u8; 1])?;
5554
}
56-
Ok(File::open(FILE_PATH)?.into_raw_fd())
55+
Ok(File::open(FILE_PATH)?)
5756
}
5857

5958
#[inline(always)]

0 commit comments

Comments
 (0)