Skip to content

Commit

Permalink
chore: Make TypePath more stable, README note
Browse files Browse the repository at this point in the history
  • Loading branch information
Bluefinger committed Aug 16, 2023
1 parent 86e1dfe commit 772ec20
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ All recommended crates implement the necessary traits to be compatible with `bev
- **`thread_local_entropy`** - Enables `ThreadLocalEntropy`, overriding `SeedableRng::from_entropy` implementations to make use of thread local entropy sources for faster PRNG initialisation. Enabled by default.
- **`serialize`** - Enables [`Serialize`] and [`Deserialize`] derives. Enabled by default.

## Note about Reflection & `TypePath` stability.

All `rand` PRNGs do not implement `TypePath` in any form, even as an optional feature, due to lack of native support for reflection in Rust. As such, while the components/resource in this library are *stable* and implement a stable `TypePath` for themselves, PRNGs rely on `std::any::type_name` for returning the type's name/path, which is NOT guaranteed to be stable for all versions of the compiler. As such, there may be instabilities/compatibilities with different compiler versions and compilations, as this instability infects the overall `TypePath` via the generic portion of the type path.

If there arises problems with regards to stability for reflection purposes, please make an issue with regards to that in this repository, so I can track such occurrences. Tests are included in the crate anyway to hopefully detect such cases in the future.

## Supported Versions & MSRV

`bevy_rand` uses the same MSRV as `bevy`.
Expand Down
24 changes: 21 additions & 3 deletions src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,20 @@ impl<R: SeedableEntropySource + 'static> EntropyComponent<R> {
}
}


impl<R: SeedableEntropySource + 'static> TypePath for EntropyComponent<R> {
fn type_path() -> &'static str {
std::any::type_name::<Self>()
static CELL: GenericTypePathCell = GenericTypePathCell::new();
CELL.get_or_insert::<Self, _>(|| {
format!(
"bevy_rand::component::EntropyComponent<{}>",
std::any::type_name::<R>()
)
})
}

fn short_type_path() -> &'static str {
static CELL: GenericTypePathCell = GenericTypePathCell::new();
CELL.get_or_insert::<Self, _>(|| bevy::utils::get_short_name(std::any::type_name::<Self>()))
CELL.get_or_insert::<Self, _>(|| bevy::utils::get_short_name(Self::type_path()))
}

fn type_ident() -> Option<&'static str> {
Expand Down Expand Up @@ -250,6 +255,19 @@ mod tests {
);
}

#[test]
fn type_paths() {
assert_eq!(
"bevy_rand::component::EntropyComponent<rand_chacha::chacha::ChaCha8Rng>",
EntropyComponent::<ChaCha8Rng>::type_path()
);

assert_eq!(
"EntropyComponent<ChaCha8Rng>",
EntropyComponent::<ChaCha8Rng>::short_type_path()
);
}

#[cfg(feature = "serialize")]
#[test]
fn rng_reflection() {
Expand Down
29 changes: 25 additions & 4 deletions src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,18 @@ impl<R: SeedableEntropySource + 'static> GlobalEntropy<R> {

impl<R: SeedableEntropySource + 'static> TypePath for GlobalEntropy<R> {
fn type_path() -> &'static str {
std::any::type_name::<Self>()
static CELL: GenericTypePathCell = GenericTypePathCell::new();
CELL.get_or_insert::<Self, _>(|| {
format!(
"bevy_rand::resource::GlobalEntropy<{}>",
std::any::type_name::<R>()
)
})
}

fn short_type_path() -> &'static str {
static CELL: GenericTypePathCell = GenericTypePathCell::new();
CELL.get_or_insert::<Self, _>(|| bevy::utils::get_short_name(std::any::type_name::<Self>()))
CELL.get_or_insert::<Self, _>(|| bevy::utils::get_short_name(Self::type_path()))
}

fn type_ident() -> Option<&'static str> {
Expand Down Expand Up @@ -160,15 +166,30 @@ impl<R: SeedableEntropySource + 'static> From<&mut R> for GlobalEntropy<R> {

#[cfg(test)]
mod tests {
use rand_chacha::ChaCha8Rng;

use super::*;

#[test]
fn type_paths() {
assert_eq!(
"bevy_rand::resource::GlobalEntropy<rand_chacha::chacha::ChaCha8Rng>",
GlobalEntropy::<ChaCha8Rng>::type_path()
);

assert_eq!(
"GlobalEntropy<ChaCha8Rng>",
GlobalEntropy::<ChaCha8Rng>::short_type_path()
);
}

#[cfg(feature = "serialize")]
#[test]
fn rng_reflection() {
use super::*;
use bevy::reflect::{
serde::{ReflectSerializer, UntypedReflectDeserializer},
TypeRegistryInternal,
};
use rand_chacha::ChaCha8Rng;
use ron::ser::to_string;
use serde::de::DeserializeSeed;

Expand Down

0 comments on commit 772ec20

Please sign in to comment.