bevy_prng/
lib.rs

1#![doc = include_str!("../README.md")]
2#![deny(missing_docs)]
3#![cfg_attr(docsrs, feature(doc_cfg))]
4#![cfg_attr(docsrs, allow(unused_attributes))]
5#![no_std]
6
7#[cfg(feature = "rand_chacha")]
8mod chacha;
9#[cfg(any(
10    feature = "wyrand",
11    feature = "rand_chacha",
12    feature = "rand_pcg",
13    feature = "rand_xoshiro"
14))]
15mod newtype;
16#[cfg(feature = "rand_pcg")]
17mod pcg;
18#[cfg(feature = "wyrand")]
19mod wyrand;
20#[cfg(feature = "rand_xoshiro")]
21mod xoshiro;
22
23use core::fmt::Debug;
24
25#[cfg(feature = "bevy_reflect")]
26use bevy_reflect::{FromReflect, Reflectable, Typed};
27use rand_core::{RngCore, SeedableRng};
28#[cfg(feature = "serialize")]
29use serde::{Deserialize, Serialize};
30
31#[cfg(feature = "rand_chacha")]
32pub use chacha::*;
33#[cfg(feature = "rand_pcg")]
34pub use pcg::*;
35#[cfg(feature = "wyrand")]
36pub use wyrand::WyRand;
37#[cfg(feature = "rand_xoshiro")]
38pub use xoshiro::*;
39
40/// Trait for handling `SeedableRng` requirements, imposing constraints
41/// depending on whether reflection support is enabled or not
42#[cfg(feature = "bevy_reflect")]
43pub trait TypedSeed: SeedableRng<Seed: Typed + Debug + Send + Sync + Clone> {}
44
45#[cfg(feature = "bevy_reflect")]
46impl<T: SeedableRng<Seed: Typed + Debug + Send + Sync + Clone>> TypedSeed for T {}
47
48/// Trait for handling `SeedableRng` requirements, imposing constraints
49/// depending on whether reflection support is enabled or not
50#[cfg(not(feature = "bevy_reflect"))]
51pub trait TypedSeed: SeedableRng<Seed: Debug + Send + Sync + Clone> {}
52
53#[cfg(not(feature = "bevy_reflect"))]
54impl<T: SeedableRng<Seed: Debug + Send + Sync + Clone>> TypedSeed for T {}
55
56/// Trait for handling contraints for valid implementations of [`EntropySource`]
57/// depending on whether reflection support is enabled or not
58#[cfg(feature = "bevy_reflect")]
59pub trait RngReflectable: FromReflect + Reflectable {}
60
61#[cfg(feature = "bevy_reflect")]
62impl<T: FromReflect + Reflectable> RngReflectable for T {}
63
64/// Trait for handling contraints for valid implementations of [`EntropySource`]
65/// depending on whether reflection support is enabled or not
66#[cfg(not(feature = "bevy_reflect"))]
67pub trait RngReflectable: 'static {}
68
69#[cfg(not(feature = "bevy_reflect"))]
70impl<T: 'static> RngReflectable for T {}
71
72/// A marker trait to define the required trait bounds for a seedable PRNG to
73/// integrate into `Entropy` or `GlobalEntropy`. This is a sealed trait.
74#[cfg(feature = "serialize")]
75pub trait EntropySource:
76    RngCore
77    + RngReflectable
78    + TypedSeed
79    + Clone
80    + Debug
81    + PartialEq
82    + Sync
83    + Send
84    + Serialize
85    + for<'a> Deserialize<'a>
86    + private::SealedSeedable
87{
88}
89
90/// Marker trait for a suitable seed for [`EntropySource`]. This is an auto trait which will
91/// apply to all suitable types that meet the trait criteria.
92#[cfg(feature = "serialize")]
93pub trait EntropySeed:
94    Debug
95    + Default
96    + PartialEq
97    + AsMut<[u8]>
98    + Clone
99    + Sync
100    + Send
101    + RngReflectable
102    + Serialize
103    + for<'a> Deserialize<'a>
104{
105}
106
107#[cfg(feature = "serialize")]
108impl<
109    T: Debug
110        + Default
111        + PartialEq
112        + AsMut<[u8]>
113        + Clone
114        + Sync
115        + Send
116        + RngReflectable
117        + Serialize
118        + for<'a> Deserialize<'a>,
119> EntropySeed for T
120{
121}
122
123/// A marker trait to define the required trait bounds for a seedable PRNG to
124/// integrate into `Entropy` or `GlobalEntropy`. This is a sealed trait.
125#[cfg(not(feature = "serialize"))]
126pub trait EntropySource:
127    RngCore
128    + TypedSeed
129    + Clone
130    + Debug
131    + PartialEq
132    + RngReflectable
133    + Sync
134    + Send
135    + private::SealedSeedable
136{
137}
138
139#[cfg(not(feature = "serialize"))]
140/// Marker trait for a suitable seed for [`EntropySource`]. This is an auto trait which will
141/// apply to all suitable types that meet the trait criteria.
142pub trait EntropySeed:
143    Debug + Default + PartialEq + AsMut<[u8]> + Clone + Sync + Send + RngReflectable
144{
145}
146
147#[cfg(not(feature = "serialize"))]
148impl<T: Debug + Default + PartialEq + AsMut<[u8]> + Clone + Sync + Send + RngReflectable>
149    EntropySeed for T
150{
151}
152
153mod private {
154    pub trait SealedSeedable {}
155
156    impl<T: super::EntropySource> SealedSeedable for T {}
157}