Skip to content

Commit b6c09e8

Browse files
committed
Add a trait conversion test
1 parent 9f6f7c8 commit b6c09e8

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

rand_core/src/lib.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,4 +639,37 @@ mod test {
639639
// value-breakage test:
640640
assert_eq!(results[0], 5029875928683246316);
641641
}
642+
643+
#[test]
644+
fn dyn_trait_conversion() {
645+
// Illustrates the need for `+ ?Sized` bound in `impl<R: RngCore> TryRngCore for R`.
646+
647+
// A method in another crate taking a fallible RNG
648+
fn third_party_api(_rng: &mut (impl TryRngCore + ?Sized)) -> bool {
649+
true
650+
}
651+
652+
// A method in our crate requiring an infallible RNG
653+
fn my_api(rng: &mut dyn RngCore) -> bool {
654+
// We want to call the method above
655+
third_party_api(rng)
656+
}
657+
658+
// A stub RNG.
659+
struct SomeRng;
660+
661+
impl RngCore for SomeRng {
662+
fn next_u32(&mut self) -> u32 {
663+
unimplemented!()
664+
}
665+
fn next_u64(&mut self) -> u64 {
666+
unimplemented!()
667+
}
668+
fn fill_bytes(&mut self, _: &mut [u8]) {
669+
unimplemented!()
670+
}
671+
}
672+
673+
assert!(my_api(&mut SomeRng));
674+
}
642675
}

0 commit comments

Comments
 (0)