⎇ 9e62e959cb49f18f8b452f0dd1a9c7adb8dd02cb
benches/src/random_style.rs
Historia
Quién tocó qué
crudo
Rename `Points`/`points` to `Length`/`length` in many APIs (#459)
1//! Pseudo-random generation of data (e.g. UI nodes) to use in benchmarks.
2
3use rand::Rng;
4
5use taffy::geometry::Size;
6use taffy::style::Dimension;
7use taffy::style::Style;
8
9/// A trait for generating pseudo-random instances.
10pub trait Randomizeable {
11 /// Generate a pseudo-random instance of this type.
12 ///
13 /// This can be useful for benchmarking.
14 fn random<R>(rng: &mut R) -> Self
15 where
16 R: Rng;
17}
18
19impl Randomizeable for Dimension {
20 fn random<R>(rng: &mut R) -> Self
21 where
22 R: Rng,
23 {
24 let switch: f32 = rng.gen_range(0.0..=1.0);
25
26 if switch < 0.2 {
27 Dimension::Auto
28 } else if switch < 0.8 {
29 Dimension::Length(rng.gen_range(0.0..500.0))
30 } else {
31 Dimension::Percent(rng.gen_range(0.0..1.0))
32 }
33 }
34}
35
36impl Randomizeable for Size<Dimension> {
37 fn random<R>(rng: &mut R) -> Self
38 where
39 R: Rng,
40 {
41 Size { width: Dimension::random(rng), height: Dimension::random(rng) }
42 }
43}
44
45impl Randomizeable for Style {
46 fn random<R>(rng: &mut R) -> Self
47 where
48 R: Rng,
49 {
50 // TODO: Add more attributes
51 Style { size: Size::<Dimension>::random(rng), ..Default::default() }
52 }
53}