@@ -99,16 +99,17 @@ or `js-sys` on "unknown" WASM targets then it's acceptable to enable this featur
9999` getrandom ` also provides optional (opt-in) backends, which allow users to customize the source
100100of randomness based on their specific needs:
101101
102- | Backend name | Target | Target Triple | Implementation
103- | ----------------- | -------------------- | ------------------------ | --------------
104- | ` linux_getrandom ` | Linux, Android | ` *‑linux‑* ` | [ ` getrandom ` ] [ 1 ] system call (without ` /dev/urandom ` fallback). Bumps minimum supported Linux kernel version to 3.17 and Android API level to 23 (Marshmallow).
105- | ` linux_raw ` | Linux, Android | ` *‑linux‑* ` | Same as ` linux_getrandom ` , but uses raw ` asm! ` -based syscalls instead of ` libc ` .
106- | ` rdrand ` | x86, x86-64 | ` x86_64-* ` , ` i686-* ` | [ ` RDRAND ` ] instruction
107- | ` rndr ` | AArch64 | ` aarch64-* ` | [ ` RNDR ` ] register
108- | ` efi_rng ` | UEFI | ` *-unknown‑uefi ` | [ ` EFI_RNG_PROTOCOL ` ] with ` EFI_RNG_ALGORITHM_RAW ` (requires ` std ` and Nightly compiler)
109- | ` windows_legacy ` | Windows | ` *-windows-* ` | [ ` RtlGenRandom ` ]
110- | ` custom ` | All targets | ` * ` | User-provided custom implementation (see [ custom backend] )
111- | ` unsupported ` | All targets | ` * ` | Always returns ` Err(Error::UNSUPPORTED) ` (see [ unsupported backend] )
102+ | Backend name | Target | Target Triple | Implementation
103+ | ------------------- | -------------------- | ------------------------ | --------------
104+ | ` linux_getrandom ` | Linux, Android | ` *‑linux‑* ` | [ ` getrandom ` ] [ 1 ] system call (without ` /dev/urandom ` fallback). Bumps minimum supported Linux kernel version to 3.17 and Android API level to 23 (Marshmallow).
105+ | ` linux_raw ` | Linux, Android | ` *‑linux‑* ` | Same as ` linux_getrandom ` , but uses raw ` asm! ` -based syscalls instead of ` libc ` .
106+ | ` rdrand ` | x86, x86-64 | ` x86_64-* ` , ` i686-* ` | [ ` RDRAND ` ] instruction
107+ | ` rndr ` | AArch64 | ` aarch64-* ` | [ ` RNDR ` ] register
108+ | ` efi_rng ` | UEFI | ` *-unknown‑uefi ` | [ ` EFI_RNG_PROTOCOL ` ] with ` EFI_RNG_ALGORITHM_RAW ` (requires ` std ` and Nightly compiler)
109+ | ` windows_legacy ` | Windows | ` *-windows-* ` | [ ` RtlGenRandom ` ]
110+ | ` custom ` | All targets | ` * ` | User-provided custom implementation (see [ custom backend] )
111+ | ` unsupported ` | All targets | ` * ` | Always returns ` Err(Error::UNSUPPORTED) ` (see [ unsupported backend] )
112+ | ` extern_item_impls ` | All targets | ` * ` | User or library provided custom implementation (see [ externally implemented interface] )
112113
113114Opt-in backends can be enabled using the ` getrandom_backend ` configuration flag.
114115The flag can be set either by specifying the ` rustflags ` field in [ ` .cargo/config.toml ` ] :
@@ -201,6 +202,37 @@ unsafe extern "Rust" fn __getrandom_v03_custom(
201202}
202203```
203204
205+ ### Externally Implemented Interface
206+
207+ Using the nightly-only feature [ ` extern_item_impls ` ] ( https://github.com/rust-lang/rust/issues/125418 )
208+ it is possible to provide a custom backend for ` getrandom ` , even to override
209+ an existing first-party implementation. First, enable the ` extern_item_impls `
210+ opt-in backend to allow usage of this nightly feature. Then, you may provide
211+ implementations for ` fill_uninit ` , ` u32 ` , and/or ` u64 ` with an attribute macro
212+ from the ` implementation ` module.
213+
214+ ``` rust
215+ use core :: mem :: MaybeUninit ;
216+
217+ #[cfg(getrandom_backend = " extern_item_impls" )]
218+ #[getrandom:: implementation:: fill_uninit]
219+ fn my_fill_uninit_implementation (
220+ dest : & mut [MaybeUninit <u8 >]
221+ ) -> Result <(), getrandom :: Error > {
222+ // ...
223+ Ok (())
224+ }
225+ ```
226+
227+ For further details on what a suitable implementation for ` fill_uninit ` may look
228+ like, see [ custom backend] .
229+
230+ ` getrandom ` will provide a default implementation for ` u32 ` and ` u64 ` , but does
231+ not currently provide a default for ` fill_uninit ` , even if one is normally
232+ available for the current target. If no implementation is available,
233+ a compilation error will be raised with instructions for how to provide
234+ an implementation.
235+
204236### Unsupported backend
205237
206238In some rare scenarios, you might be compiling this crate for an unsupported
@@ -373,6 +405,7 @@ dual licensed as above, without any additional terms or conditions.
373405[ WASI ] : https://github.com/WebAssembly/WASI
374406[ Emscripten ] : https://emscripten.org
375407[ opt-in ] : #opt-in-backends
408+ [ externally implemented interface ] : #externally-implemented-interface
376409
377410[ // ] : # ( licenses )
378411
0 commit comments