aboutsummaryrefslogtreecommitdiff
path: root/ctr-std/src/sys
diff options
context:
space:
mode:
authorFenrir <[email protected]>2017-12-01 21:57:34 -0700
committerFenrirWolf <[email protected]>2017-12-01 22:33:03 -0700
commit8ba058552b61484248fc295dfbbe2e18a9d49e48 (patch)
tree0c1bdd596147abee8dda56ac98ca009fa699a707 /ctr-std/src/sys
parentUpdate bindings for libctru v1.4.0 (diff)
downloadctru-rs-8ba058552b61484248fc295dfbbe2e18a9d49e48.tar.xz
ctru-rs-8ba058552b61484248fc295dfbbe2e18a9d49e48.zip
Patch `std` to be compatible with Rust nightly-2017-12-01
This only fixes things enough so that the project compiles again. More standard library changes from upstream Rust will be pulled in later.
Diffstat (limited to 'ctr-std/src/sys')
-rw-r--r--ctr-std/src/sys/unix/mod.rs2
-rw-r--r--ctr-std/src/sys/unix/rand.rs65
2 files changed, 29 insertions, 38 deletions
diff --git a/ctr-std/src/sys/unix/mod.rs b/ctr-std/src/sys/unix/mod.rs
index cd583b5..c0e4eb4 100644
--- a/ctr-std/src/sys/unix/mod.rs
+++ b/ctr-std/src/sys/unix/mod.rs
@@ -13,6 +13,8 @@
use io::{self, ErrorKind};
use libc;
+pub use self::rand::hashmap_random_keys;
+
pub mod condvar;
pub mod ext;
pub mod fast_thread_local;
diff --git a/ctr-std/src/sys/unix/rand.rs b/ctr-std/src/sys/unix/rand.rs
index 39c967a..4500c9d 100644
--- a/ctr-std/src/sys/unix/rand.rs
+++ b/ctr-std/src/sys/unix/rand.rs
@@ -8,47 +8,36 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
-use io::{self, Error, ErrorKind};
use mem;
-use rand::Rng;
-
-use libctru::{sslcInit, sslcExit, sslcGenerateRandomData};
-
-pub struct OsRng(());
-
-impl OsRng {
- pub fn new() -> io::Result<OsRng> {
- unsafe {
- let r = sslcInit(0);
- if r < 0 {
- Err(Error::new(ErrorKind::Other, "Unable to initialize the RNG"))
- } else {
- Ok(OsRng(()))
- }
- }
- }
-}
-
-impl Rng for OsRng {
- fn next_u32(&mut self) -> u32 {
- let mut v = [0; 4];
- self.fill_bytes(&mut v);
- unsafe { mem::transmute(v) }
- }
-
- fn next_u64(&mut self) -> u64 {
- let mut v = [0; 8];
- self.fill_bytes(&mut v);
- unsafe { mem::transmute(v) }
- }
-
- fn fill_bytes(&mut self, v: &mut [u8]) {
- unsafe { sslcGenerateRandomData(v.as_ptr() as _, v.len() as u32); }
+use slice;
+
+pub fn hashmap_random_keys() -> (u64, u64) {
+ let mut v = (0, 0);
+ unsafe {
+ let view = slice::from_raw_parts_mut(&mut v as *mut _ as *mut u8,
+ mem::size_of_val(&v));
+ imp::fill_bytes(view);
}
+ return v
}
-impl Drop for OsRng {
- fn drop(&mut self) {
- unsafe { sslcExit() }
+mod imp {
+ use libctru;
+
+ pub fn fill_bytes(v: &mut [u8]) {
+ unsafe {
+ // Initializing and de-initializing the sslC subsystem every time
+ // we initialize a hashmap is pretty dumb, but I can't think of a
+ // better method at the moment.
+ //
+ // lazy_static won't work because
+ // destructors (for closing the subsystem on exit) won't run.
+ //
+ // Perhaps overriding __appInit() and __appExit() will work,
+ // but that's an experiment for another time.
+ libctru::sslcInit(0);
+ libctru::sslcGenerateRandomData(v.as_ptr() as _, v.len() as u32);
+ libctru::sslcExit();
+ }
}
}