-
Notifications
You must be signed in to change notification settings - Fork 220
Add custom-fallback
Backend
#684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
92b8034
d30f559
63b9e5f
17d7d58
7b98ff7
ca81b68
f4d8a45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
**/*.rs.bk | ||
nopanic_check/Cargo.lock | ||
nopanic_check/target/ | ||
fallback_test/Cargo.lock | ||
fallback_test/target/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,12 @@ rustc-dep-of-std = ["dep:compiler_builtins", "dep:core"] | |
# i.e. avoid unconditionally enabling it in library crates. | ||
wasm_js = ["dep:wasm-bindgen", "dep:js-sys"] | ||
|
||
# Optional backend: custom-fallback | ||
# This flag allows a custom fallback backend. | ||
# This will be used as a last-resort instead of raising a compiler error when | ||
# no other backend is available. | ||
custom-fallback = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for the annoying status ping, but what are the odds of this landing in the next week or so? Trying to make plans for the next Bevy release. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whether we add this feature or not should not influence your Bevy plans. The custom fallback feature is intended to be used either by users controlling the root crate or by "privileged" crates like |
||
|
||
[dependencies] | ||
cfg-if = "1" | ||
|
||
|
@@ -90,6 +96,7 @@ check-cfg = [ | |
'cfg(getrandom_test_linux_without_fallback)', | ||
'cfg(getrandom_test_netbsd_fallback)', | ||
'cfg(target_os, values("cygwin"))', # TODO(MSRV 1.86): Remove this. | ||
'cfg(getrandom_no_custom_fallback)', | ||
] | ||
|
||
[package.metadata.docs.rs] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "fallback_test" | ||
description = "Test crate for assessing the custom-fallback feature" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
getrandom = { path = "..", default-features = false } | ||
|
||
[features] | ||
fallback = ["getrandom/custom-fallback"] | ||
fail-double-definition = ["fallback"] | ||
|
||
[profile.release] | ||
panic = "abort" | ||
|
||
[profile.dev] | ||
panic = "abort" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
#[panic_handler] | ||
fn handle(_info: &core::panic::PanicInfo) -> ! { | ||
loop {} | ||
} | ||
|
||
use getrandom::{Backend, Error}; | ||
|
||
/// This implementation fills using a constant value of 4. | ||
/// | ||
/// WARNING: this custom implementation is for testing purposes ONLY! | ||
struct ConstantBackend; | ||
|
||
unsafe impl Backend for ConstantBackend { | ||
#[inline] | ||
fn fill_uninit(dest: &mut [core::mem::MaybeUninit<u8>]) -> Result<(), Error> { | ||
for out in dest { | ||
// Chosen by fair dice roll | ||
out.write(4); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(feature = "fallback")] | ||
const _: () = { | ||
getrandom::set_backend!(ConstantBackend); | ||
}; | ||
|
||
// This second call will cause the following compile-time error: | ||
/* | ||
error: symbol `__getrandom_v03_fallback_fill_uninit` is already defined | ||
--> src\main.rs:43:1 | ||
| | ||
43 | getrandom::set_backend!(ConstantBackend); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this error originates in the macro `getrandom::set_backend` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: could not compile `fallback_test` (bin "fallback_test") due to 1 previous error | ||
*/ | ||
#[cfg(feature = "fail-double-definition")] | ||
const _: () = { | ||
getrandom::set_backend!(ConstantBackend); | ||
}; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn _start() { | ||
let mut dest = [0u8; 13]; | ||
let result = getrandom::fill(&mut dest); | ||
assert!(result.is_ok()); | ||
assert_eq!(dest, [4; 13]); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change the documentation to say that this feature should only be enabled by a crate which provides a custom fallback.