Skip to content

Commit 8968c3c

Browse files
committed
reorganise trait soup into wrapper traits
1 parent 580cc7d commit 8968c3c

File tree

5 files changed

+94
-280
lines changed

5 files changed

+94
-280
lines changed

crates/bevy_entropy/src/component.rs

Lines changed: 21 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,35 @@
11
use std::fmt::Debug;
22

33
use crate::{
4-
entropy_source::EntropySource, resource::GlobalEntropy, Deserialize, RngCore, SeedableRng,
5-
Serialize,
4+
thread_local_entropy::ThreadLocalEntropy,
5+
resource::GlobalEntropy,
6+
traits::{EntropySource, SeedableEntropySource},
7+
Deserialize, RngCore, SeedableRng, Serialize,
68
};
79
use bevy_ecs::{prelude::Component, system::ResMut, world::Mut};
810
use bevy_reflect::{FromReflect, Reflect, ReflectDeserialize, ReflectSerialize};
911

1012
#[derive(Debug, Clone, PartialEq, Eq, Component, Reflect, FromReflect, Serialize, Deserialize)]
1113
#[serde(bound(deserialize = "R: for<'a> Deserialize<'a>"))]
1214
#[reflect_value(Debug, PartialEq, Serialize, Deserialize)]
13-
pub struct EntropyComponent<
14-
R: RngCore
15-
+ Clone
16-
+ Debug
17-
+ PartialEq
18-
+ Sync
19-
+ Send
20-
+ Serialize
21-
+ for<'a> Deserialize<'a>
22-
+ 'static,
23-
>(R);
24-
25-
impl<
26-
R: RngCore
27-
+ Clone
28-
+ Debug
29-
+ PartialEq
30-
+ Sync
31-
+ Send
32-
+ Serialize
33-
+ for<'a> Deserialize<'a>
34-
+ 'static,
35-
> EntropyComponent<R>
36-
{
15+
pub struct EntropyComponent<R: EntropySource + 'static>(R);
16+
17+
impl<R: EntropySource + 'static> EntropyComponent<R> {
3718
#[inline]
3819
#[must_use]
3920
pub fn new(rng: R) -> Self {
4021
Self(rng)
4122
}
4223
}
4324

44-
impl<
45-
R: RngCore
46-
+ SeedableRng
47-
+ Clone
48-
+ Debug
49-
+ PartialEq
50-
+ Sync
51-
+ Send
52-
+ Serialize
53-
+ for<'a> Deserialize<'a>
54-
+ 'static,
55-
> EntropyComponent<R>
56-
{
25+
impl<R: SeedableEntropySource + 'static> EntropyComponent<R> {
5726
#[inline]
5827
#[must_use]
5928
pub fn from_entropy() -> Self {
6029
// Source entropy from thread local user-space RNG instead of
6130
// system entropy source to reduce overhead when creating many
6231
// rng instances for many entities at once.
63-
Self(R::from_rng(EntropySource).unwrap())
32+
Self(R::from_rng(ThreadLocalEntropy).unwrap())
6433
}
6534

6635
#[inline]
@@ -69,53 +38,13 @@ impl<
6938
}
7039
}
7140

72-
impl<
73-
R: RngCore
74-
+ Clone
75-
+ Debug
76-
+ PartialEq
77-
+ Sync
78-
+ Send
79-
+ Serialize
80-
+ for<'a> Deserialize<'a>
81-
+ 'static,
82-
> From<R> for EntropyComponent<R>
83-
{
84-
fn from(value: R) -> Self {
85-
Self::new(value)
86-
}
87-
}
88-
89-
impl<
90-
R: RngCore
91-
+ SeedableRng
92-
+ Clone
93-
+ Debug
94-
+ PartialEq
95-
+ Sync
96-
+ Send
97-
+ Serialize
98-
+ for<'a> Deserialize<'a>
99-
+ 'static,
100-
> Default for EntropyComponent<R>
101-
{
41+
impl<R: SeedableEntropySource + 'static> Default for EntropyComponent<R> {
10242
fn default() -> Self {
10343
Self::from_entropy()
10444
}
10545
}
10646

107-
impl<
108-
R: RngCore
109-
+ Clone
110-
+ Debug
111-
+ PartialEq
112-
+ Sync
113-
+ Send
114-
+ Serialize
115-
+ for<'a> Deserialize<'a>
116-
+ 'static,
117-
> RngCore for EntropyComponent<R>
118-
{
47+
impl<R: EntropySource + 'static> RngCore for EntropyComponent<R> {
11948
#[inline]
12049
fn next_u32(&mut self) -> u32 {
12150
self.0.next_u32()
@@ -137,75 +66,33 @@ impl<
13766
}
13867
}
13968

140-
impl<
141-
R: RngCore
142-
+ SeedableRng
143-
+ Clone
144-
+ Debug
145-
+ PartialEq
146-
+ Sync
147-
+ Send
148-
+ Serialize
149-
+ for<'a> Deserialize<'a>
150-
+ 'static,
151-
> SeedableRng for EntropyComponent<R>
152-
{
69+
impl<R: SeedableEntropySource + 'static> SeedableRng for EntropyComponent<R> {
15370
type Seed = R::Seed;
15471

15572
fn from_seed(seed: Self::Seed) -> Self {
15673
Self::new(R::from_seed(seed))
15774
}
15875
}
15976

160-
impl<
161-
R: RngCore
162-
+ SeedableRng
163-
+ Clone
164-
+ Debug
165-
+ PartialEq
166-
+ Sync
167-
+ Send
168-
+ Serialize
169-
+ for<'a> Deserialize<'a>
170-
+ 'static,
171-
> From<&mut R> for EntropyComponent<R>
172-
{
77+
impl<R: EntropySource + 'static> From<R> for EntropyComponent<R> {
78+
fn from(value: R) -> Self {
79+
Self::new(value)
80+
}
81+
}
82+
83+
impl<R: SeedableEntropySource + 'static> From<&mut R> for EntropyComponent<R> {
17384
fn from(rng: &mut R) -> Self {
17485
Self::from_rng(rng).unwrap()
17586
}
17687
}
17788

178-
impl<
179-
R: RngCore
180-
+ SeedableRng
181-
+ Clone
182-
+ Debug
183-
+ PartialEq
184-
+ Sync
185-
+ Send
186-
+ Serialize
187-
+ for<'a> Deserialize<'a>
188-
+ 'static,
189-
> From<&mut Mut<'_, R>> for EntropyComponent<R>
190-
{
89+
impl<R: SeedableEntropySource + 'static> From<&mut Mut<'_, R>> for EntropyComponent<R> {
19190
fn from(rng: &mut Mut<'_, R>) -> Self {
19291
Self::from_rng(rng.as_mut()).unwrap()
19392
}
19493
}
19594

196-
impl<
197-
R: RngCore
198-
+ SeedableRng
199-
+ Clone
200-
+ Debug
201-
+ PartialEq
202-
+ Sync
203-
+ Send
204-
+ Serialize
205-
+ for<'a> Deserialize<'a>
206-
+ 'static,
207-
> From<&mut ResMut<'_, GlobalEntropy<R>>> for EntropyComponent<R>
208-
{
95+
impl<R: SeedableEntropySource + 'static> From<&mut ResMut<'_, GlobalEntropy<R>>> for EntropyComponent<R> {
20996
fn from(rng: &mut ResMut<'_, GlobalEntropy<R>>) -> Self {
21097
Self::from_rng(rng.as_mut()).unwrap()
21198
}

crates/bevy_entropy/src/lib.rs

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
#![doc = include_str!("../README.md")]
33

44
pub mod component;
5-
mod entropy_source;
65
pub mod prelude;
76
pub mod resource;
7+
mod thread_local_entropy;
8+
mod traits;
89

9-
use std::{fmt::Debug, marker::PhantomData};
10+
use std::marker::PhantomData;
1011

1112
use bevy_app::{App, Plugin};
1213
use component::EntropyComponent;
1314
use rand_core::{RngCore, SeedableRng};
1415
use resource::GlobalEntropy;
1516
use serde::{Deserialize, Serialize};
17+
use traits::SeedableEntropySource;
1618

1719
/// Plugin for integrating a PRNG that implements `RngCore` into
1820
/// the bevy engine, registering types for a global resource and
@@ -36,34 +38,12 @@ use serde::{Deserialize, Serialize};
3638
/// println!("Random value: {}", rng.next_u32());
3739
/// }
3840
/// ```
39-
pub struct EntropyPlugin<
40-
R: RngCore
41-
+ SeedableRng
42-
+ Clone
43-
+ Debug
44-
+ PartialEq
45-
+ Sync
46-
+ Send
47-
+ Serialize
48-
+ for<'a> Deserialize<'a>
49-
+ 'static,
50-
> {
41+
pub struct EntropyPlugin<R: SeedableEntropySource + 'static> {
5142
seed: Option<R::Seed>,
5243
_marker: PhantomData<&'static mut R>,
5344
}
5445

55-
impl<
56-
R: RngCore
57-
+ SeedableRng
58-
+ Clone
59-
+ Debug
60-
+ PartialEq
61-
+ Sync
62-
+ Send
63-
+ Serialize
64-
+ for<'a> Deserialize<'a>
65-
+ 'static,
66-
> EntropyPlugin<R>
46+
impl<R: SeedableEntropySource + 'static> EntropyPlugin<R>
6747
where
6848
R::Seed: Send + Sync + Copy,
6949
{
@@ -83,18 +63,7 @@ where
8363
}
8464
}
8565

86-
impl<
87-
R: RngCore
88-
+ SeedableRng
89-
+ Clone
90-
+ Debug
91-
+ PartialEq
92-
+ Sync
93-
+ Send
94-
+ Serialize
95-
+ for<'a> Deserialize<'a>
96-
+ 'static,
97-
> Default for EntropyPlugin<R>
66+
impl<R: SeedableEntropySource + 'static> Default for EntropyPlugin<R>
9867
where
9968
R::Seed: Send + Sync + Copy,
10069
{
@@ -103,18 +72,7 @@ where
10372
}
10473
}
10574

106-
impl<
107-
R: RngCore
108-
+ SeedableRng
109-
+ Clone
110-
+ Debug
111-
+ PartialEq
112-
+ Sync
113-
+ Send
114-
+ Serialize
115-
+ for<'a> Deserialize<'a>
116-
+ 'static,
117-
> Plugin for EntropyPlugin<R>
75+
impl<R: SeedableEntropySource + 'static> Plugin for EntropyPlugin<R>
11876
where
11977
R::Seed: Send + Sync + Copy,
12078
{

0 commit comments

Comments
 (0)