Skip to main content

rand/distr/
uniform_other.rs

1// Copyright 2018-2020 Developers of the Rand project.
2// Copyright 2017 The Rust Project Developers.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10//! `UniformChar`, `UniformDuration` implementations
11
12use super::{Error, SampleBorrow, SampleUniform, Uniform, UniformInt, UniformSampler};
13use crate::Rng;
14use crate::distr::Distribution;
15use core::time::Duration;
16
17#[cfg(feature = "serde")]
18use serde::{Deserialize, Serialize};
19
20impl SampleUniform for char {
21    type Sampler = UniformChar;
22}
23
24/// The back-end implementing [`UniformSampler`] for `char`.
25///
26/// Unless you are implementing [`UniformSampler`] for your own type, this type
27/// should not be used directly, use [`Uniform`] instead.
28///
29/// This differs from integer range sampling since the range `0xD800..=0xDFFF`
30/// are used for surrogate pairs in UCS and UTF-16, and consequently are not
31/// valid Unicode code points. We must therefore avoid sampling values in this
32/// range.
33#[derive(#[automatically_derived]
impl ::core::clone::Clone for UniformChar {
    #[inline]
    fn clone(&self) -> UniformChar {
        let _: ::core::clone::AssertParamIsClone<UniformInt<u32>>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for UniformChar { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for UniformChar {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field1_finish(f, "UniformChar",
            "sampler", &&self.sampler)
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for UniformChar {
    #[inline]
    fn eq(&self, other: &UniformChar) -> bool {
        self.sampler == other.sampler
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for UniformChar {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<UniformInt<u32>>;
    }
}Eq)]
34#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
35pub struct UniformChar {
36    sampler: UniformInt<u32>,
37}
38
39/// UTF-16 surrogate range start
40const CHAR_SURROGATE_START: u32 = 0xD800;
41/// UTF-16 surrogate range size
42const CHAR_SURROGATE_LEN: u32 = 0xE000 - CHAR_SURROGATE_START;
43
44/// Convert `char` to compressed `u32`
45fn char_to_comp_u32(c: char) -> u32 {
46    match c as u32 {
47        c if c >= CHAR_SURROGATE_START => c - CHAR_SURROGATE_LEN,
48        c => c,
49    }
50}
51
52impl UniformSampler for UniformChar {
53    type X = char;
54
55    #[inline] // if the range is constant, this helps LLVM to do the
56    // calculations at compile-time.
57    fn new<B1, B2>(low_b: B1, high_b: B2) -> Result<Self, Error>
58    where
59        B1: SampleBorrow<Self::X> + Sized,
60        B2: SampleBorrow<Self::X> + Sized,
61    {
62        let low = char_to_comp_u32(*low_b.borrow());
63        let high = char_to_comp_u32(*high_b.borrow());
64        let sampler = UniformInt::<u32>::new(low, high);
65        sampler.map(|sampler| UniformChar { sampler })
66    }
67
68    #[inline] // if the range is constant, this helps LLVM to do the
69    // calculations at compile-time.
70    fn new_inclusive<B1, B2>(low_b: B1, high_b: B2) -> Result<Self, Error>
71    where
72        B1: SampleBorrow<Self::X> + Sized,
73        B2: SampleBorrow<Self::X> + Sized,
74    {
75        let low = char_to_comp_u32(*low_b.borrow());
76        let high = char_to_comp_u32(*high_b.borrow());
77        let sampler = UniformInt::<u32>::new_inclusive(low, high);
78        sampler.map(|sampler| UniformChar { sampler })
79    }
80
81    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::X {
82        let mut x = self.sampler.sample(rng);
83        if x >= CHAR_SURROGATE_START {
84            x += CHAR_SURROGATE_LEN;
85        }
86        // SAFETY: x must not be in surrogate range or greater than char::MAX.
87        // This relies on range constructors which accept char arguments.
88        // Validity of input char values is assumed.
89        unsafe { core::char::from_u32_unchecked(x) }
90    }
91}
92
93#[cfg(feature = "alloc")]
94impl crate::distr::SampleString for Uniform<char> {
95    fn append_string<R: Rng + ?Sized>(
96        &self,
97        rng: &mut R,
98        string: &mut alloc::string::String,
99        len: usize,
100    ) {
101        // Getting the hi value to assume the required length to reserve in string.
102        let mut hi = self.0.sampler.low + self.0.sampler.range - 1;
103        if hi >= CHAR_SURROGATE_START {
104            hi += CHAR_SURROGATE_LEN;
105        }
106        // Get the utf8 length of hi to minimize extra space.
107        let max_char_len = char::from_u32(hi).map(char::len_utf8).unwrap_or(4);
108        string.reserve(max_char_len * len);
109        string.extend(self.sample_iter(rng).take(len))
110    }
111}
112
113/// The back-end implementing [`UniformSampler`] for `Duration`.
114///
115/// Unless you are implementing [`UniformSampler`] for your own types, this type
116/// should not be used directly, use [`Uniform`] instead.
117#[derive(#[automatically_derived]
impl ::core::clone::Clone for UniformDuration {
    #[inline]
    fn clone(&self) -> UniformDuration {
        let _: ::core::clone::AssertParamIsClone<UniformDurationMode>;
        let _: ::core::clone::AssertParamIsClone<u32>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for UniformDuration { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for UniformDuration {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "UniformDuration", "mode", &self.mode, "offset", &&self.offset)
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for UniformDuration {
    #[inline]
    fn eq(&self, other: &UniformDuration) -> bool {
        self.offset == other.offset && self.mode == other.mode
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for UniformDuration {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<UniformDurationMode>;
        let _: ::core::cmp::AssertParamIsEq<u32>;
    }
}Eq)]
118#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
119pub struct UniformDuration {
120    mode: UniformDurationMode,
121    offset: u32,
122}
123
124#[derive(#[automatically_derived]
impl ::core::fmt::Debug for UniformDurationMode {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            UniformDurationMode::Small { secs: __self_0, nanos: __self_1 } =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f, "Small",
                    "secs", __self_0, "nanos", &__self_1),
            UniformDurationMode::Medium { nanos: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "Medium", "nanos", &__self_0),
            UniformDurationMode::Large {
                max_secs: __self_0, max_nanos: __self_1, secs: __self_2 } =>
                ::core::fmt::Formatter::debug_struct_field3_finish(f, "Large",
                    "max_secs", __self_0, "max_nanos", __self_1, "secs",
                    &__self_2),
        }
    }
}Debug, #[automatically_derived]
impl ::core::marker::Copy for UniformDurationMode { }Copy, #[automatically_derived]
impl ::core::clone::Clone for UniformDurationMode {
    #[inline]
    fn clone(&self) -> UniformDurationMode {
        let _: ::core::clone::AssertParamIsClone<u64>;
        let _: ::core::clone::AssertParamIsClone<Uniform<u32>>;
        let _: ::core::clone::AssertParamIsClone<Uniform<u64>>;
        let _: ::core::clone::AssertParamIsClone<u32>;
        let _: ::core::clone::AssertParamIsClone<Uniform<u64>>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for UniformDurationMode {
    #[inline]
    fn eq(&self, other: &UniformDurationMode) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (UniformDurationMode::Small { secs: __self_0, nanos: __self_1
                    }, UniformDurationMode::Small {
                    secs: __arg1_0, nanos: __arg1_1 }) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1,
                (UniformDurationMode::Medium { nanos: __self_0 },
                    UniformDurationMode::Medium { nanos: __arg1_0 }) =>
                    __self_0 == __arg1_0,
                (UniformDurationMode::Large {
                    max_secs: __self_0, max_nanos: __self_1, secs: __self_2 },
                    UniformDurationMode::Large {
                    max_secs: __arg1_0, max_nanos: __arg1_1, secs: __arg1_2 })
                    =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1 &&
                        __self_2 == __arg1_2,
                _ => unsafe { ::core::intrinsics::unreachable() }
            }
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for UniformDurationMode {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<u64>;
        let _: ::core::cmp::AssertParamIsEq<Uniform<u32>>;
        let _: ::core::cmp::AssertParamIsEq<Uniform<u64>>;
        let _: ::core::cmp::AssertParamIsEq<u32>;
        let _: ::core::cmp::AssertParamIsEq<Uniform<u64>>;
    }
}Eq)]
125#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
126enum UniformDurationMode {
127    Small {
128        secs: u64,
129        nanos: Uniform<u32>,
130    },
131    Medium {
132        nanos: Uniform<u64>,
133    },
134    Large {
135        max_secs: u64,
136        max_nanos: u32,
137        secs: Uniform<u64>,
138    },
139}
140
141impl SampleUniform for Duration {
142    type Sampler = UniformDuration;
143}
144
145impl UniformSampler for UniformDuration {
146    type X = Duration;
147
148    #[inline]
149    fn new<B1, B2>(low_b: B1, high_b: B2) -> Result<Self, Error>
150    where
151        B1: SampleBorrow<Self::X> + Sized,
152        B2: SampleBorrow<Self::X> + Sized,
153    {
154        let low = *low_b.borrow();
155        let high = *high_b.borrow();
156        if !(low < high) {
157            return Err(Error::EmptyRange);
158        }
159        UniformDuration::new_inclusive(low, high - Duration::new(0, 1))
160    }
161
162    #[inline]
163    fn new_inclusive<B1, B2>(low_b: B1, high_b: B2) -> Result<Self, Error>
164    where
165        B1: SampleBorrow<Self::X> + Sized,
166        B2: SampleBorrow<Self::X> + Sized,
167    {
168        let low = *low_b.borrow();
169        let high = *high_b.borrow();
170        if !(low <= high) {
171            return Err(Error::EmptyRange);
172        }
173
174        let low_s = low.as_secs();
175        let low_n = low.subsec_nanos();
176        let mut high_s = high.as_secs();
177        let mut high_n = high.subsec_nanos();
178
179        if high_n < low_n {
180            high_s -= 1;
181            high_n += 1_000_000_000;
182        }
183
184        let mode = if low_s == high_s {
185            UniformDurationMode::Small {
186                secs: low_s,
187                nanos: Uniform::new_inclusive(low_n, high_n)?,
188            }
189        } else {
190            let max = high_s
191                .checked_mul(1_000_000_000)
192                .and_then(|n| n.checked_add(u64::from(high_n)));
193
194            if let Some(higher_bound) = max {
195                let lower_bound = low_s * 1_000_000_000 + u64::from(low_n);
196                UniformDurationMode::Medium {
197                    nanos: Uniform::new_inclusive(lower_bound, higher_bound)?,
198                }
199            } else {
200                // An offset is applied to simplify generation of nanoseconds
201                let max_nanos = high_n - low_n;
202                UniformDurationMode::Large {
203                    max_secs: high_s,
204                    max_nanos,
205                    secs: Uniform::new_inclusive(low_s, high_s)?,
206                }
207            }
208        };
209        Ok(UniformDuration {
210            mode,
211            offset: low_n,
212        })
213    }
214
215    #[inline]
216    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Duration {
217        match self.mode {
218            UniformDurationMode::Small { secs, nanos } => {
219                let n = nanos.sample(rng);
220                Duration::new(secs, n)
221            }
222            UniformDurationMode::Medium { nanos } => {
223                let nanos = nanos.sample(rng);
224                Duration::new(nanos / 1_000_000_000, (nanos % 1_000_000_000) as u32)
225            }
226            UniformDurationMode::Large {
227                max_secs,
228                max_nanos,
229                secs,
230            } => {
231                // constant folding means this is at least as fast as `Rng::sample(Range)`
232                let nano_range = Uniform::new(0, 1_000_000_000).unwrap();
233                loop {
234                    let s = secs.sample(rng);
235                    let n = nano_range.sample(rng);
236                    if !(s == max_secs && n > max_nanos) {
237                        let sum = n + self.offset;
238                        break Duration::new(s, sum);
239                    }
240                }
241            }
242        }
243    }
244}
245
246#[cfg(test)]
247mod tests {
248    use super::*;
249    use crate::RngExt;
250
251    #[test]
252    #[cfg(feature = "serde")]
253    fn test_serialization_uniform_duration() {
254        let distr = UniformDuration::new(Duration::from_secs(10), Duration::from_secs(60)).unwrap();
255        let de_distr: UniformDuration =
256            postcard::from_bytes(&postcard::to_allocvec(&distr).unwrap()).unwrap();
257        assert_eq!(distr, de_distr);
258    }
259
260    #[test]
261    #[cfg_attr(miri, ignore)] // Miri is too slow
262    fn test_char() {
263        let mut rng = crate::test::rng(891);
264        let mut max = core::char::from_u32(0).unwrap();
265        for _ in 0..100 {
266            let c = rng.random_range('A'..='Z');
267            assert!(c.is_ascii_uppercase());
268            max = max.max(c);
269        }
270        assert_eq!(max, 'Z');
271        let d = Uniform::new(
272            core::char::from_u32(0xD7F0).unwrap(),
273            core::char::from_u32(0xE010).unwrap(),
274        )
275        .unwrap();
276        for _ in 0..100 {
277            let c = d.sample(&mut rng);
278            assert!((c as u32) < 0xD800 || (c as u32) > 0xDFFF);
279        }
280        #[cfg(feature = "alloc")]
281        {
282            use crate::distr::SampleString;
283            let string1 = d.sample_string(&mut rng, 100);
284            assert_eq!(string1.capacity(), 300);
285            let string2 = Uniform::new(
286                core::char::from_u32(0x0000).unwrap(),
287                core::char::from_u32(0x0080).unwrap(),
288            )
289            .unwrap()
290            .sample_string(&mut rng, 100);
291            assert_eq!(string2.capacity(), 100);
292            let string3 = Uniform::new_inclusive(
293                core::char::from_u32(0x0000).unwrap(),
294                core::char::from_u32(0x0080).unwrap(),
295            )
296            .unwrap()
297            .sample_string(&mut rng, 100);
298            assert_eq!(string3.capacity(), 200);
299        }
300    }
301
302    #[test]
303    #[cfg_attr(miri, ignore)] // Miri is too slow
304    fn test_durations() {
305        let mut rng = crate::test::rng(253);
306
307        let v = &[
308            (Duration::new(10, 50000), Duration::new(100, 1234)),
309            (Duration::new(0, 100), Duration::new(1, 50)),
310            (Duration::new(0, 0), Duration::new(u64::MAX, 999_999_999)),
311        ];
312        for &(low, high) in v.iter() {
313            let my_uniform = Uniform::new(low, high).unwrap();
314            for _ in 0..1000 {
315                let v = rng.sample(my_uniform);
316                assert!(low <= v && v < high);
317            }
318        }
319    }
320}