Skip to content

Commit 4340154

Browse files
committed
first-pass on splitting features,
1 parent 4768517 commit 4340154

File tree

9 files changed

+189
-111
lines changed

9 files changed

+189
-111
lines changed

crates/bevy_entropy/Cargo.toml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bevy_entropy"
3-
version = "0.9.0"
3+
version = "0.11.0-dev"
44
edition = "2021"
55
description = "Bevy Engine's RNG integration"
66
homepage = "https://bevyengine.org"
@@ -9,15 +9,21 @@ license = "MIT OR Apache-2.0"
99
keywords = ["game", "bevy", "rand", "rng"]
1010
categories = ["game-engines", "algorithms"]
1111

12-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
12+
[features]
13+
bevy_reflect = ["dep:bevy_reflect", "bevy_app/bevy_reflect", "serialize"]
14+
default = ["bevy_reflect"]
15+
serialize = ["dep:serde", "rand_core/serde1", "rand_chacha/serde1"]
1316

1417
[dependencies]
15-
bevy_app = { path = "../bevy_app", version = "0.9.0" }
16-
bevy_ecs = { path = "../bevy_ecs", version = "0.9.0" }
17-
bevy_reflect = { path = "../bevy_reflect", version = "0.9.0" }
18-
serde = { version = "1.0", features = ["derive"] }
19-
rand_core = { version = "0.6", features = ["std", "serde1"] }
20-
rand_chacha = { version = "0.3", features = ["serde1"] }
18+
# bevy
19+
bevy_app = { path = "../bevy_app", version = "0.9.0", default-features = false }
20+
bevy_ecs = { path = "../bevy_ecs", version = "0.9.0", default-features = false }
21+
bevy_reflect = { path = "../bevy_reflect", version = "0.9.0", optional = true }
22+
23+
# others
24+
serde = { version = "1.0", features = ["derive"], optional = true }
25+
rand_core = { version = "0.6", features = ["std"] }
26+
rand_chacha = "0.3"
2127

2228
[dev-dependencies]
2329
rand = "0.8"

crates/bevy_entropy/src/component.rs

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

33
use crate::{
4-
thread_local_entropy::ThreadLocalEntropy,
54
resource::GlobalEntropy,
5+
thread_local_entropy::ThreadLocalEntropy,
66
traits::{EntropySource, SeedableEntropySource},
7-
Deserialize, RngCore, SeedableRng, Serialize,
87
};
98
use bevy_ecs::{prelude::Component, system::ResMut, world::Mut};
9+
use rand_core::{RngCore, SeedableRng};
10+
11+
#[cfg(feature = "serialize")]
12+
use serde::{Deserialize, Serialize};
13+
14+
#[cfg(feature = "bevy_reflect")]
1015
use bevy_reflect::{FromReflect, Reflect, ReflectDeserialize, ReflectSerialize};
1116

12-
#[derive(Debug, Clone, PartialEq, Eq, Component, Reflect, FromReflect, Serialize, Deserialize)]
13-
#[serde(bound(deserialize = "R: for<'a> Deserialize<'a>"))]
14-
#[reflect_value(Debug, PartialEq, Serialize, Deserialize)]
17+
#[derive(Debug, Clone, PartialEq, Eq, Component)]
18+
#[cfg_attr(feature = "bevy_reflect", derive(Reflect, FromReflect))]
19+
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
20+
#[cfg_attr(
21+
feature = "serialize",
22+
serde(bound(deserialize = "R: for<'a> Deserialize<'a>"))
23+
)]
24+
#[cfg_attr(
25+
all(feature = "serialize", feature = "bevy_reflect"),
26+
reflect_value(Debug, PartialEq, Serialize, Deserialize)
27+
)]
1528
pub struct EntropyComponent<R: EntropySource + 'static>(R);
1629

1730
impl<R: EntropySource + 'static> EntropyComponent<R> {
@@ -92,7 +105,9 @@ impl<R: SeedableEntropySource + 'static> From<&mut Mut<'_, R>> for EntropyCompon
92105
}
93106
}
94107

95-
impl<R: SeedableEntropySource + 'static> From<&mut ResMut<'_, GlobalEntropy<R>>> for EntropyComponent<R> {
108+
impl<R: SeedableEntropySource + 'static> From<&mut ResMut<'_, GlobalEntropy<R>>>
109+
for EntropyComponent<R>
110+
{
96111
fn from(rng: &mut ResMut<'_, GlobalEntropy<R>>) -> Self {
97112
Self::from_rng(rng.as_mut()).unwrap()
98113
}

crates/bevy_entropy/src/lib.rs

Lines changed: 1 addition & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -4,90 +4,6 @@
44
pub mod component;
55
pub mod prelude;
66
pub mod resource;
7+
pub mod plugin;
78
mod thread_local_entropy;
89
mod traits;
9-
10-
use std::marker::PhantomData;
11-
12-
use bevy_app::{App, Plugin};
13-
use component::EntropyComponent;
14-
use rand_core::{RngCore, SeedableRng};
15-
use resource::GlobalEntropy;
16-
use serde::{Deserialize, Serialize};
17-
use traits::SeedableEntropySource;
18-
19-
/// Plugin for integrating a PRNG that implements `RngCore` into
20-
/// the bevy engine, registering types for a global resource and
21-
/// entropy components.
22-
///
23-
/// ```
24-
/// use bevy_ecs::prelude::ResMut;
25-
/// use bevy_app::App;
26-
/// use bevy_entropy::prelude::*;
27-
/// use rand_core::RngCore;
28-
/// use rand_chacha::ChaCha8Rng;
29-
///
30-
/// fn main() {
31-
/// App::new()
32-
/// .add_plugin(EntropyPlugin::<ChaCha8Rng>::default())
33-
/// .add_system(print_random_value)
34-
/// .run();
35-
/// }
36-
///
37-
/// fn print_random_value(mut rng: ResMut<GlobalEntropy<ChaCha8Rng>>) {
38-
/// println!("Random value: {}", rng.next_u32());
39-
/// }
40-
/// ```
41-
pub struct EntropyPlugin<R: SeedableEntropySource + 'static> {
42-
seed: Option<R::Seed>,
43-
_marker: PhantomData<&'static mut R>,
44-
}
45-
46-
impl<R: SeedableEntropySource + 'static> EntropyPlugin<R>
47-
where
48-
R::Seed: Send + Sync + Copy,
49-
{
50-
#[inline]
51-
#[must_use]
52-
pub fn new() -> Self {
53-
Self {
54-
seed: None,
55-
_marker: PhantomData,
56-
}
57-
}
58-
59-
#[inline]
60-
pub fn with_seed(mut self, seed: R::Seed) -> Self {
61-
self.seed = Some(seed);
62-
self
63-
}
64-
}
65-
66-
impl<R: SeedableEntropySource + 'static> Default for EntropyPlugin<R>
67-
where
68-
R::Seed: Send + Sync + Copy,
69-
{
70-
fn default() -> Self {
71-
Self::new()
72-
}
73-
}
74-
75-
impl<R: SeedableEntropySource + 'static> Plugin for EntropyPlugin<R>
76-
where
77-
R::Seed: Send + Sync + Copy,
78-
{
79-
fn build(&self, app: &mut App) {
80-
app.register_type::<GlobalEntropy<R>>()
81-
.register_type::<EntropyComponent<R>>();
82-
83-
if let Some(seed) = self.seed {
84-
app.insert_resource(GlobalEntropy::<R>::from_seed(seed));
85-
} else {
86-
app.init_resource::<GlobalEntropy<R>>();
87-
}
88-
}
89-
90-
fn is_unique(&self) -> bool {
91-
false
92-
}
93-
}

crates/bevy_entropy/src/plugin.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use std::marker::PhantomData;
2+
3+
use crate::{resource::GlobalEntropy, traits::SeedableEntropySource};
4+
use bevy_app::{App, Plugin};
5+
use rand_core::SeedableRng;
6+
7+
#[cfg(feature = "bevy_reflect")]
8+
use crate::component::EntropyComponent;
9+
10+
/// Plugin for integrating a PRNG that implements `RngCore` into
11+
/// the bevy engine, registering types for a global resource and
12+
/// entropy components.
13+
///
14+
/// ```
15+
/// use bevy_ecs::prelude::ResMut;
16+
/// use bevy_app::App;
17+
/// use bevy_entropy::prelude::*;
18+
/// use rand_core::RngCore;
19+
/// use rand_chacha::ChaCha8Rng;
20+
///
21+
/// fn main() {
22+
/// App::new()
23+
/// .add_plugin(EntropyPlugin::<ChaCha8Rng>::default())
24+
/// .add_system(print_random_value)
25+
/// .run();
26+
/// }
27+
///
28+
/// fn print_random_value(mut rng: ResMut<GlobalEntropy<ChaCha8Rng>>) {
29+
/// println!("Random value: {}", rng.next_u32());
30+
/// }
31+
/// ```
32+
pub struct EntropyPlugin<R: SeedableEntropySource + 'static> {
33+
seed: Option<R::Seed>,
34+
_marker: PhantomData<&'static mut R>,
35+
}
36+
37+
impl<R: SeedableEntropySource + 'static> EntropyPlugin<R>
38+
where
39+
R::Seed: Send + Sync + Copy,
40+
{
41+
#[inline]
42+
#[must_use]
43+
pub fn new() -> Self {
44+
Self {
45+
seed: None,
46+
_marker: PhantomData,
47+
}
48+
}
49+
50+
#[inline]
51+
pub fn with_seed(mut self, seed: R::Seed) -> Self {
52+
self.seed = Some(seed);
53+
self
54+
}
55+
}
56+
57+
impl<R: SeedableEntropySource + 'static> Default for EntropyPlugin<R>
58+
where
59+
R::Seed: Send + Sync + Copy,
60+
{
61+
fn default() -> Self {
62+
Self::new()
63+
}
64+
}
65+
66+
impl<R: SeedableEntropySource + 'static> Plugin for EntropyPlugin<R>
67+
where
68+
R::Seed: Send + Sync + Copy,
69+
{
70+
fn build(&self, app: &mut App) {
71+
#[cfg(feature = "bevy_reflect")]
72+
app.register_type::<GlobalEntropy<R>>()
73+
.register_type::<EntropyComponent<R>>();
74+
75+
if let Some(seed) = self.seed {
76+
app.insert_resource(GlobalEntropy::<R>::from_seed(seed));
77+
} else {
78+
app.init_resource::<GlobalEntropy<R>>();
79+
}
80+
}
81+
82+
fn is_unique(&self) -> bool {
83+
false
84+
}
85+
}

crates/bevy_entropy/src/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
pub use crate::component::EntropyComponent;
22
pub use crate::resource::GlobalEntropy;
3-
pub use crate::EntropyPlugin;
3+
pub use crate::plugin::EntropyPlugin;

crates/bevy_entropy/src/resource.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
use std::fmt::Debug;
22

3-
use crate::{
4-
traits::{EntropySource, SeedableEntropySource},
5-
Deserialize, RngCore, SeedableRng, Serialize,
6-
};
3+
use crate::traits::{EntropySource, SeedableEntropySource};
74
use bevy_ecs::prelude::Resource;
5+
use rand_core::{RngCore, SeedableRng};
6+
7+
#[cfg(feature = "serialize")]
8+
use serde::{Deserialize, Serialize};
9+
10+
#[cfg(feature = "bevy_reflect")]
811
use bevy_reflect::{FromReflect, Reflect, ReflectDeserialize, ReflectSerialize};
912

10-
#[derive(Debug, Clone, PartialEq, Eq, Resource, Reflect, FromReflect, Serialize, Deserialize)]
11-
#[serde(bound(deserialize = "R: for<'a> Deserialize<'a>"))]
12-
#[reflect_value(Debug, PartialEq, Serialize, Deserialize)]
13+
#[derive(Debug, Clone, PartialEq, Eq, Resource)]
14+
#[cfg_attr(feature = "bevy_reflect", derive(Reflect, FromReflect))]
15+
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
16+
#[cfg_attr(
17+
feature = "serialize",
18+
serde(bound(deserialize = "R: for<'a> Deserialize<'a>"))
19+
)]
20+
#[cfg_attr(
21+
all(feature = "serialize", feature = "bevy_reflect"),
22+
reflect_value(Debug, PartialEq, Serialize, Deserialize)
23+
)]
1324
pub struct GlobalEntropy<R: EntropySource + 'static>(R);
1425

1526
impl<R: EntropySource + 'static> GlobalEntropy<R> {

crates/bevy_entropy/src/thread_local_entropy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{cell::UnsafeCell, rc::Rc};
22

3-
use crate::{RngCore, SeedableRng};
43
use rand_chacha::ChaCha12Rng;
4+
use rand_core::{RngCore, SeedableRng};
55

66
thread_local! {
77
// We require `Rc` to avoid premature freeing when `ThreadLocalEntropy` is used within thread-local destructors.

crates/bevy_entropy/src/traits.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
11
use std::fmt::Debug;
22

3-
use crate::{Deserialize, RngCore, SeedableRng, Serialize};
3+
use rand_core::{RngCore, SeedableRng};
44

5+
#[cfg(feature = "serialize")]
6+
use serde::{Deserialize, Serialize};
7+
8+
#[cfg(feature = "serialize")]
59
pub trait EntropySource:
6-
RngCore + Clone + Debug + PartialEq + Sync + Send + Serialize + for<'a> Deserialize<'a>
10+
RngCore
11+
+ Clone
12+
+ Debug
13+
+ PartialEq
14+
+ Sync
15+
+ Send
16+
+ Serialize
17+
+ for<'a> Deserialize<'a>
18+
+ private::SealedEntropy
719
{
820
}
921

22+
#[cfg(feature = "serialize")]
1023
impl<T> EntropySource for T where
1124
T: RngCore + Clone + Debug + PartialEq + Sync + Send + Serialize + for<'a> Deserialize<'a>
1225
{
1326
}
1427

28+
#[cfg(not(feature = "serialize"))]
29+
pub trait EntropySource:
30+
RngCore + Clone + Debug + PartialEq + Sync + Send + private::SealedEntropy
31+
{
32+
}
33+
34+
#[cfg(not(feature = "serialize"))]
35+
impl<T> EntropySource for T where T: RngCore + Clone + Debug + PartialEq + Sync + Send {}
36+
37+
#[cfg(feature = "serialize")]
1538
pub trait SeedableEntropySource:
1639
RngCore
1740
+ SeedableRng
@@ -22,9 +45,11 @@ pub trait SeedableEntropySource:
2245
+ Send
2346
+ Serialize
2447
+ for<'a> Deserialize<'a>
48+
+ private::SealedSeedable
2549
{
2650
}
2751

52+
#[cfg(feature = "serialize")]
2853
impl<T> SeedableEntropySource for T where
2954
T: RngCore
3055
+ SeedableRng
@@ -37,3 +62,23 @@ impl<T> SeedableEntropySource for T where
3762
+ for<'a> Deserialize<'a>
3863
{
3964
}
65+
66+
#[cfg(not(feature = "serialize"))]
67+
pub trait SeedableEntropySource:
68+
RngCore + SeedableRng + Clone + Debug + PartialEq + Sync + Send + private::SealedSeedable
69+
{
70+
}
71+
72+
#[cfg(not(feature = "serialize"))]
73+
impl<T> SeedableEntropySource for T where
74+
T: RngCore + SeedableRng + Clone + Debug + PartialEq + Sync + Send
75+
{
76+
}
77+
78+
mod private {
79+
pub trait SealedEntropy {}
80+
pub trait SealedSeedable {}
81+
82+
impl<T: super::EntropySource> SealedEntropy for T {}
83+
impl<T: super::SeedableEntropySource> SealedSeedable for T {}
84+
}

crates/bevy_internal/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ symphonia-wav = ["bevy_audio/symphonia-wav"]
5858
# Enable watching file system for asset hot reload
5959
filesystem_watcher = ["bevy_asset/filesystem_watcher"]
6060

61-
serialize = ["bevy_core/serialize", "bevy_input/serialize", "bevy_time/serialize", "bevy_window/serialize", "bevy_transform/serialize", "bevy_math/serialize", "bevy_scene/serialize"]
61+
serialize = ["bevy_core/serialize", "bevy_input/serialize", "bevy_time/serialize", "bevy_window/serialize", "bevy_transform/serialize", "bevy_math/serialize", "bevy_scene/serialize", "bevy_entropy?/serialize"]
6262

6363
# Display server protocol support (X11 is enabled by default)
6464
wayland = ["bevy_winit/wayland"]

0 commit comments

Comments
 (0)