1use crate::{Arbitrary, Result, Unstructured};
23impl<'a> Arbitrary<'a> for char {
4fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
5// The highest unicode code point is 0x11_FFFF
6const CHAR_END: u32 = 0x11_0000;
7// The size of the surrogate blocks
8const SURROGATES_START: u32 = 0xD800;
9let mut c = <u32 as Arbitrary<'a>>::arbitrary(u)? % CHAR_END;
10if let Some(c) = char::from_u32(c) {
11Ok(c)
12 } else {
13// We found a surrogate, wrap and try again
14c -= SURROGATES_START;
15Ok(char::from_u32(c)
16 .expect("Generated character should be valid! This is a bug in arbitrary-rs"))
17 }
18 }
1920#[inline]
21fn size_hint(depth: usize) -> (usize, Option<usize>) {
22 <u32 as Arbitrary<'a>>::size_hint(depth)
23 }
24}