Skip to content

Commit d20f31b

Browse files
committed
Add CI test for extern_item_impls
1 parent 37a1ab8 commit d20f31b

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

.github/workflows/tests.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ jobs:
7777
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="rdrand"
7878
RUSTDOCFLAGS: -Dwarnings --cfg getrandom_backend="rdrand"
7979
run: cargo test --features=std,sys_rng
80+
- env:
81+
RUSTFLAGS: -Dwarnings --cfg getrandom_backend="extern_item_impls"
82+
RUSTDOCFLAGS: -Dwarnings --cfg getrandom_backend="extern_item_impls"
83+
run: cargo test --target=${{ matrix.target }} --features=std --test mod extern_item_impls
8084

8185
ios:
8286
name: iOS Simulator

tests/mod.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,36 @@ mod custom {
291291
assert!(res.is_err());
292292
}
293293
}
294+
295+
#[cfg(getrandom_backend = "extern_item_impls")]
296+
mod extern_item_impls {
297+
use core::mem::MaybeUninit;
298+
use getrandom::Error;
299+
300+
// This implementation for fill_uninit will always fail.
301+
//
302+
// WARNING: this custom implementation is for testing purposes ONLY!
303+
304+
#[getrandom::implementation::fill_uninit]
305+
fn my_fill_uninit_implementation(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
306+
Err(Error::new_custom(4))
307+
}
308+
309+
// This implementation returns a fixed value to demonstrate overriding defaults.
310+
//
311+
// WARNING: this custom implementation is for testing purposes ONLY!
312+
313+
#[getrandom::implementation::u32]
314+
fn my_u32_implementation() -> Result<u32, Error> {
315+
// Chosen by fair dice roll
316+
Ok(4)
317+
}
318+
319+
// Test that enabling the custom feature indeed uses the custom implementation
320+
#[test]
321+
fn test_extern_item_impls() {
322+
let mut buf = [0u8; 123];
323+
assert_eq!(getrandom::fill(&mut buf), Err(Error::new_custom(4)));
324+
assert_eq!(getrandom::u32(), Ok(4));
325+
}
326+
}

0 commit comments

Comments
 (0)