
# Objective - Fixes #6370 - Closes #6581 ## Solution - Added the following lints to the workspace: - `std_instead_of_core` - `std_instead_of_alloc` - `alloc_instead_of_core` - Used `cargo +nightly fmt` with [item level use formatting](https://rust-lang.github.io/rustfmt/?version=v1.6.0&search=#Item%5C%3A) to split all `use` statements into single items. - Used `cargo clippy --workspace --all-targets --all-features --fix --allow-dirty` to _attempt_ to resolve the new linting issues, and intervened where the lint was unable to resolve the issue automatically (usually due to needing an `extern crate alloc;` statement in a crate root). - Manually removed certain uses of `std` where negative feature gating prevented `--all-features` from finding the offending uses. - Used `cargo +nightly fmt` with [crate level use formatting](https://rust-lang.github.io/rustfmt/?version=v1.6.0&search=#Crate%5C%3A) to re-merge all `use` statements matching Bevy's previous styling. - Manually fixed cases where the `fmt` tool could not re-merge `use` statements due to conditional compilation attributes. ## Testing - Ran CI locally ## Migration Guide The MSRV is now 1.81. Please update to this version or higher. ## Notes - This is a _massive_ change to try and push through, which is why I've outlined the semi-automatic steps I used to create this PR, in case this fails and someone else tries again in the future. - Making this change has no impact on user code, but does mean Bevy contributors will be warned to use `core` and `alloc` instead of `std` where possible. - This lint is a critical first step towards investigating `no_std` options for Bevy. --------- Co-authored-by: François Mockers <francois.mockers@vleue.com>
100 lines
2.9 KiB
Rust
100 lines
2.9 KiB
Rust
//! This module holds local implementations of the [`Distribution`] trait for [`Standard`], which
|
|
//! allow certain Bevy math types (those whose values can be randomly generated without additional
|
|
//! input other than an [`Rng`]) to be produced using [`rand`]'s APIs. It also holds [`FromRng`],
|
|
//! an ergonomic extension to that functionality which permits the omission of type annotations.
|
|
//!
|
|
//! For instance:
|
|
//! ```
|
|
//! # use rand::{random, Rng, SeedableRng, rngs::StdRng, distributions::Standard};
|
|
//! # use bevy_math::{Dir3, sampling::FromRng};
|
|
//! let mut rng = StdRng::seed_from_u64(7313429298);
|
|
//! // Random direction using thread-local rng
|
|
//! let random_direction1: Dir3 = random();
|
|
//!
|
|
//! // Random direction using the rng constructed above
|
|
//! let random_direction2: Dir3 = rng.gen();
|
|
//!
|
|
//! // The same as the previous but with different syntax
|
|
//! let random_direction3 = Dir3::from_rng(&mut rng);
|
|
//!
|
|
//! // Five random directions, using Standard explicitly
|
|
//! let many_random_directions: Vec<Dir3> = rng.sample_iter(Standard).take(5).collect();
|
|
//! ```
|
|
|
|
use core::f32::consts::TAU;
|
|
|
|
use crate::{
|
|
primitives::{Circle, Sphere},
|
|
Dir2, Dir3, Dir3A, Quat, Rot2, ShapeSample, Vec3A,
|
|
};
|
|
use rand::{
|
|
distributions::{Distribution, Standard},
|
|
Rng,
|
|
};
|
|
|
|
/// Ergonomics trait for a type with a [`Standard`] distribution, allowing values to be generated
|
|
/// uniformly from an [`Rng`] by a method in its own namespace.
|
|
///
|
|
/// Example
|
|
/// ```
|
|
/// # use rand::{Rng, SeedableRng, rngs::StdRng};
|
|
/// # use bevy_math::{Dir3, sampling::FromRng};
|
|
/// let mut rng = StdRng::seed_from_u64(451);
|
|
/// let random_dir = Dir3::from_rng(&mut rng);
|
|
/// ```
|
|
pub trait FromRng
|
|
where
|
|
Self: Sized,
|
|
Standard: Distribution<Self>,
|
|
{
|
|
/// Construct a value of this type uniformly at random using `rng` as the source of randomness.
|
|
fn from_rng<R: Rng + ?Sized>(rng: &mut R) -> Self {
|
|
rng.gen()
|
|
}
|
|
}
|
|
|
|
impl Distribution<Dir2> for Standard {
|
|
#[inline]
|
|
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Dir2 {
|
|
let circle = Circle::new(1.0);
|
|
let vector = circle.sample_boundary(rng);
|
|
Dir2::new_unchecked(vector)
|
|
}
|
|
}
|
|
|
|
impl FromRng for Dir2 {}
|
|
|
|
impl Distribution<Dir3> for Standard {
|
|
#[inline]
|
|
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Dir3 {
|
|
let sphere = Sphere::new(1.0);
|
|
let vector = sphere.sample_boundary(rng);
|
|
Dir3::new_unchecked(vector)
|
|
}
|
|
}
|
|
|
|
impl FromRng for Dir3 {}
|
|
|
|
impl Distribution<Dir3A> for Standard {
|
|
#[inline]
|
|
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Dir3A {
|
|
let sphere = Sphere::new(1.0);
|
|
let vector: Vec3A = sphere.sample_boundary(rng).into();
|
|
Dir3A::new_unchecked(vector)
|
|
}
|
|
}
|
|
|
|
impl FromRng for Dir3A {}
|
|
|
|
impl Distribution<Rot2> for Standard {
|
|
#[inline]
|
|
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Rot2 {
|
|
let angle = rng.gen_range(0.0..TAU);
|
|
Rot2::radians(angle)
|
|
}
|
|
}
|
|
|
|
impl FromRng for Rot2 {}
|
|
|
|
impl FromRng for Quat {}
|