Skip to content

Commit

Permalink
Add a trait conversion test
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Feb 19, 2025
1 parent 9f6f7c8 commit 5714860
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions rand_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,4 +639,29 @@ mod test {
// value-breakage test:
assert_eq!(results[0], 5029875928683246316);
}

#[test]
fn dyn_trait_conversion() {
// Illustrates the need for `+ ?Sized` bound in `impl<R: RngCore> TryRngCore for R`.

// A method in another crate taking a fallible RNG
fn third_party_api(_rng: &mut (impl TryRngCore + ?Sized)) -> bool { true }

// A method in our crate requiring an infallible RNG
fn my_api(rng: &mut dyn RngCore) -> bool {
// We want to call the method above
third_party_api(rng)
}

// A stub RNG.
struct SomeRng;

impl RngCore for SomeRng {
fn next_u32(&mut self) -> u32 { unimplemented!() }
fn next_u64(&mut self) -> u64 { unimplemented!() }
fn fill_bytes(&mut self, _: &mut [u8]) { unimplemented!() }
}

assert!(my_api(&mut SomeRng));
}
}

0 comments on commit 5714860

Please sign in to comment.