gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 684HTML 640Markdown 12TOML 62286 Definiciones

⎇ 0.1.x src/randomizable.rs Rust · 55 líneas · 1.3 KB Historia Quién tocó qué crudo

Disable super-deep benchmarks (0.1 is too slow to complete these at all) (#280) 7db11fa87 · Nico Burns ·

1//! Pseudo-random generation of data (e.g. UI nodes) to use in benchmarks.
2
3use rand::Rng;
4
5use crate::geometry::Size;
6use crate::style::Dimension;
7use crate::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.4 {
29            Dimension::Undefined
30        } else if switch < 0.8 {
31            Dimension::Points(rng.gen_range(0.0..500.0))
32        } else {
33            Dimension::Percent(rng.gen_range(0.0..1.0))
34        }
35    }
36}
37
38impl Randomizeable for Size<Dimension> {
39    fn random<R>(rng: &mut R) -> Self
40    where
41        R: Rng,
42    {
43        Size { width: Dimension::random(rng), height: Dimension::random(rng) }
44    }
45}
46
47impl Randomizeable for Style {
48    fn random<R>(rng: &mut R) -> Self
49    where
50        R: Rng,
51    {
52        // TODO: Add more attributes
53        Style { size: Size::<Dimension>::random(rng), ..Default::default() }
54    }
55}