Skip to content

Commit d40ec2c

Browse files
authored
Add support for the Nintendo 3DS (armv6k-nintendo-3ds) (#248)
1 parent 157d6f2 commit d40ec2c

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

.github/workflows/tests.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ jobs:
316316
run: cargo build -Z build-std=core --target=x86_64-wrs-vxworks
317317
- name: SOLID
318318
run: cargo build -Z build-std=core --target=aarch64-kmc-solid_asp3
319+
- name: Nintendo 3DS
320+
run: cargo build -Z build-std=core --target=armv6k-nintendo-3ds
319321

320322
clippy-fmt:
321323
name: Clippy + rustfmt

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ compiler_builtins = { version = "0.1", optional = true }
1818
core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" }
1919

2020
[target.'cfg(unix)'.dependencies]
21-
libc = { version = "0.2.64", default-features = false }
21+
libc = { version = "0.2.120", default-features = false }
2222

2323
[target.'cfg(target_os = "wasi")'.dependencies]
2424
wasi = "0.10"

src/3ds.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2021 Developers of the Rand project.
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
//! Implementation for Nintendo 3DS
10+
use crate::util_libc::sys_fill_exact;
11+
use crate::Error;
12+
13+
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
14+
sys_fill_exact(dest, |buf| unsafe {
15+
libc::getrandom(buf.as_mut_ptr() as *mut libc::c_void, buf.len(), 0)
16+
})
17+
}

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
//! | Web Browser | `wasm32‑*‑unknown` | [`Crypto.getRandomValues`], see [WebAssembly support]
3434
//! | Node.js | `wasm32‑*‑unknown` | [`crypto.randomBytes`], see [WebAssembly support]
3535
//! | SOLID | `*-kmc-solid_*` | `SOLID_RNG_SampleRandomBytes`
36+
//! | Nintendo 3DS | `armv6k-nintendo-3ds` | [`getrandom`][1]
3637
//!
3738
//! There is no blanket implementation on `unix` targets that reads from
3839
//! `/dev/urandom`. This ensures all supported targets are using the recommended
@@ -223,6 +224,11 @@ cfg_if! {
223224
} else if #[cfg(all(feature = "js",
224225
target_arch = "wasm32", target_os = "unknown"))] {
225226
#[path = "js.rs"] mod imp;
227+
} else if #[cfg(all(target_os = "horizon", target_arch = "arm"))] {
228+
// We check for target_arch = "arm" because the Nintendo Switch also
229+
// uses Horizon OS (it is aarch64).
230+
mod util_libc;
231+
#[path = "3ds.rs"] mod imp;
226232
} else if #[cfg(feature = "custom")] {
227233
use custom as imp;
228234
} else if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] {

src/util_libc.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ cfg_if! {
2020
use libc::__error as errno_location;
2121
} else if #[cfg(target_os = "haiku")] {
2222
use libc::_errnop as errno_location;
23+
} else if #[cfg(all(target_os = "horizon", target_arch = "arm"))] {
24+
extern "C" {
25+
// Not provided by libc: https://github.com/rust-lang/libc/issues/1995
26+
fn __errno() -> *mut libc::c_int;
27+
}
28+
use __errno as errno_location;
2329
}
2430
}
2531

0 commit comments

Comments
 (0)