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
25use bevy_reflect::{FromReflect, Reflectable, Typed};
26use rand_core::{RngCore, SeedableRng};
27#[cfg(feature = "serialize")]
28use serde::{Deserialize, Serialize};
29
30#[cfg(feature = "rand_chacha")]
31pub use chacha::*;
32#[cfg(feature = "rand_pcg")]
33pub use pcg::*;
34#[cfg(feature = "wyrand")]
35pub use wyrand::WyRand;
36#[cfg(feature = "rand_xoshiro")]
37pub use xoshiro::*;
38
39/// A marker trait to define the required trait bounds for a seedable PRNG to
40/// integrate into `Entropy` or `GlobalEntropy`. This is a sealed trait.
41#[cfg(feature = "serialize")]
42pub trait EntropySource:
43    RngCore
44    + SeedableRng<Seed: Typed>
45    + Clone
46    + Debug
47    + PartialEq
48    + Sync
49    + Send
50    + FromReflect
51    + Reflectable
52    + Serialize
53    + for<'a> Deserialize<'a>
54    + private::SealedSeedable
55{
56}
57
58/// Marker trait for a suitable seed for [`EntropySource`]. This is an auto trait which will
59/// apply to all suitable types that meet the trait criteria.
60#[cfg(feature = "serialize")]
61pub trait EntropySeed:
62    Debug
63    + Default
64    + PartialEq
65    + AsMut<[u8]>
66    + Clone
67    + Sync
68    + Send
69    + Reflectable
70    + FromReflect
71    + Serialize
72    + for<'a> Deserialize<'a>
73{
74}
75
76#[cfg(feature = "serialize")]
77impl<
78    T: Debug
79        + Default
80        + PartialEq
81        + AsMut<[u8]>
82        + Clone
83        + Sync
84        + Send
85        + Reflectable
86        + FromReflect
87        + Serialize
88        + for<'a> Deserialize<'a>,
89> EntropySeed for T
90{
91}
92
93/// A marker trait to define the required trait bounds for a seedable PRNG to
94/// integrate into `Entropy` or `GlobalEntropy`. This is a sealed trait.
95#[cfg(not(feature = "serialize"))]
96pub trait EntropySource:
97    RngCore
98    + SeedableRng<Seed: Typed>
99    + Clone
100    + Debug
101    + PartialEq
102    + Reflectable
103    + FromReflect
104    + Sync
105    + Send
106    + private::SealedSeedable
107{
108}
109
110#[cfg(not(feature = "serialize"))]
111/// Marker trait for a suitable seed for [`EntropySource`]. This is an auto trait which will
112/// apply to all suitable types that meet the trait criteria.
113pub trait EntropySeed:
114    Debug + Default + PartialEq + AsMut<[u8]> + Clone + Sync + Send + Reflectable + FromReflect
115{
116}
117
118#[cfg(not(feature = "serialize"))]
119impl<T: Debug + Default + PartialEq + AsMut<[u8]> + Clone + Sync + Send + Reflectable + FromReflect>
120    EntropySeed for T
121{
122}
123
124mod private {
125    pub trait SealedSeedable {}
126
127    impl<T: super::EntropySource> SealedSeedable for T {}
128}