tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 501HTML 238Markdown 7TOML 31453 Definiciones

⎇ ec371711989b7f4d870d91bea4b66bdd94b45c9e src/randomizable.rs Rust · 53 líneas · 1.2 KB Historia Quién tocó qué crudo

Strict types for style values (#269) 0d8c198a9 · 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.8 {
29            Dimension::Points(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}