From 3692476c9c4110ccbc7e67f2071f438b1d6552ee Mon Sep 17 00:00:00 2001 From: Tristan Murphy <72839119+HyperCodec@users.noreply.github.com> Date: Tue, 7 Oct 2025 11:50:01 +0000 Subject: [PATCH 1/2] remove tracing feature and simplify some traits --- Cargo.lock | 44 ------------- genetic-rs-common/Cargo.toml | 2 - genetic-rs-common/src/builtin/repopulator.rs | 65 +++----------------- genetic-rs-common/src/lib.rs | 34 +--------- genetic-rs-common/src/prelude.rs | 2 +- genetic-rs-macros/src/lib.rs | 10 +-- genetic-rs/Cargo.toml | 1 - 7 files changed, 16 insertions(+), 142 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6dba5d6..d55ab0d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -60,7 +60,6 @@ version = "1.0.0" dependencies = [ "rand", "rayon", - "tracing", ] [[package]] @@ -91,18 +90,6 @@ version = "0.2.171" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6" -[[package]] -name = "once_cell" -version = "1.21.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" - -[[package]] -name = "pin-project-lite" -version = "0.2.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" - [[package]] name = "ppv-lite86" version = "0.2.17" @@ -193,37 +180,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "tracing" -version = "0.1.41" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" -dependencies = [ - "pin-project-lite", - "tracing-attributes", - "tracing-core", -] - -[[package]] -name = "tracing-attributes" -version = "0.1.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "tracing-core" -version = "0.1.33" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" -dependencies = [ - "once_cell", -] - [[package]] name = "unicode-ident" version = "1.0.12" diff --git a/genetic-rs-common/Cargo.toml b/genetic-rs-common/Cargo.toml index f7f92d3..b013587 100644 --- a/genetic-rs-common/Cargo.toml +++ b/genetic-rs-common/Cargo.toml @@ -18,7 +18,6 @@ crossover = ["builtin"] speciation = ["crossover"] genrand = ["dep:rand"] rayon = ["dep:rayon"] -tracing = ["dep:tracing"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -29,4 +28,3 @@ rustdoc-args = ["--cfg", "docsrs"] [dependencies] rand = { version = "0.9.2", optional = true } rayon = { version = "1.10.0", optional = true } -tracing = { version = "0.1.41", optional = true } diff --git a/genetic-rs-common/src/builtin/repopulator.rs b/genetic-rs-common/src/builtin/repopulator.rs index de0af12..a0e748e 100644 --- a/genetic-rs-common/src/builtin/repopulator.rs +++ b/genetic-rs-common/src/builtin/repopulator.rs @@ -1,34 +1,17 @@ use rand::Rng as RandRng; -use crate::{Repopulator, Rng}; - -#[cfg(feature = "tracing")] -use tracing::*; +use crate::Repopulator; /// Used in other traits to randomly mutate genomes a given amount pub trait RandomlyMutable { /// Mutate the genome with a given mutation rate (0..1) - fn mutate(&mut self, rate: f32, rng: &mut impl Rng); + fn mutate(&mut self, rate: f32, rng: &mut impl rand::Rng); } -/// Internal trait that simply deals with the trait bounds of features to avoid duplicate code. -/// It is blanket implemented, so you should never have to reference this directly. -#[cfg(not(feature = "tracing"))] -pub trait FeatureBoundedRandomlyMutable: RandomlyMutable {} -#[cfg(not(feature = "tracing"))] -impl FeatureBoundedRandomlyMutable for T {} - -/// Internal trait that simply deals with the trait bounds of features to avoid duplicate code. -/// It is blanket implemented, so you should never have to reference this directly. -#[cfg(feature = "tracing")] -pub trait FeatureBoundedRandomlyMutable: RandomlyMutable + std::fmt::Debug {} -#[cfg(feature = "tracing")] -impl FeatureBoundedRandomlyMutable for T {} - /// Used in dividually-reproducing [`Repopulator`]s -pub trait Mitosis: Clone + FeatureBoundedRandomlyMutable { +pub trait Mitosis: Clone + RandomlyMutable { /// Create a new child with mutation. Similar to [RandomlyMutable::mutate], but returns a new instance instead of modifying the original. - fn divide(&self, rate: f32, rng: &mut impl Rng) -> Self { + fn divide(&self, rate: f32, rng: &mut impl rand::Rng) -> Self { let mut child = self.clone(); child.mutate(rate, rng); child @@ -36,19 +19,11 @@ pub trait Mitosis: Clone + FeatureBoundedRandomlyMutable { } /// Used in crossover-reproducing [`Repopulator`]s -#[cfg(all(feature = "crossover", not(feature = "tracing")))] +#[cfg(feature = "crossover")] #[cfg_attr(docsrs, doc(cfg(feature = "crossover")))] -pub trait Crossover: Clone + PartialEq { +pub trait Crossover: Clone { /// Use crossover reproduction to create a new genome. - fn crossover(&self, other: &Self, rate: f32, rng: &mut impl Rng) -> Self; -} - -/// Used in crossover-reproducing [`next_gen`]s -#[cfg(all(feature = "crossover", feature = "tracing"))] -#[cfg_attr(docsrs, doc(cfg(feature = "crossover")))] -pub trait Crossover: Clone + std::fmt::Debug { - /// Use crossover reproduction to create a new genome. - fn crossover(&self, other: &Self, rate: f32, rng: &mut impl Rng) -> Self; + fn crossover(&self, other: &Self, rate: f32, rng: &mut impl rand::Rng) -> Self; } /// Used in speciated crossover nextgens. Allows for genomes to avoid crossover with ones that are too different. @@ -134,21 +109,8 @@ where } let parent2 = &genomes[j]; - #[cfg(feature = "tracing")] - let span = span!( - Level::DEBUG, - "crossover", - a = tracing::field::debug(parent1), - b = tracing::field::debug(parent2) - ); - #[cfg(feature = "tracing")] - let enter = span.enter(); - let child = parent1.crossover(parent2, self.mutation_rate, &mut rng); - #[cfg(feature = "tracing")] - drop(enter); - genomes.push(child); } } @@ -193,21 +155,8 @@ where parent2 = &champions[rng.random_range(0..champions.len() - 1)]; } - #[cfg(feature = "tracing")] - let span = span!( - Level::DEBUG, - "crossover", - a = tracing::field::debug(parent1), - b = tracing::field::debug(parent2) - ); - #[cfg(feature = "tracing")] - let enter = span.enter(); - let child = parent1.crossover(parent2, self.mutation_rate, &mut rng); - #[cfg(feature = "tracing")] - drop(enter); - genomes.push(child); } } diff --git a/genetic-rs-common/src/lib.rs b/genetic-rs-common/src/lib.rs index a3d2c8f..187dd82 100644 --- a/genetic-rs-common/src/lib.rs +++ b/genetic-rs-common/src/lib.rs @@ -16,23 +16,6 @@ pub mod prelude; #[cfg(feature = "rayon")] use rayon::prelude::*; -#[cfg(feature = "tracing")] -use tracing::*; - -#[cfg(feature = "tracing")] -#[allow(missing_docs)] -pub trait Rng: rand::Rng + std::fmt::Debug {} - -#[cfg(feature = "tracing")] -impl Rng for T {} - -#[cfg(not(feature = "tracing"))] -#[allow(missing_docs)] -pub trait Rng: rand::Rng {} - -#[cfg(not(feature = "tracing"))] -impl Rng for T {} - /// Tests and eliminates the unfit from the simulation. pub trait Eliminator { /// Tests and eliminates the unfit from the simulation. @@ -118,20 +101,11 @@ where /// Uses the [`Eliminator`] and [`Repopulator`] provided in [`GeneticSim::new`] to create the next generation of genomes. pub fn next_generation(&mut self) { - #[cfg(feature = "tracing")] - let span = span!(Level::TRACE, "next_generation"); - - #[cfg(feature = "tracing")] - let enter = span.enter(); - let genomes = std::mem::take(&mut self.genomes); let target_size = genomes.len(); self.genomes = self.eliminator.eliminate(genomes); self.repopulator.repopulate(&mut self.genomes, target_size); - - #[cfg(feature = "tracing")] - drop(enter); } /// Calls [`next_generation`][GeneticSim::next_generation] `count` number of times. @@ -147,7 +121,7 @@ where #[cfg_attr(docsrs, doc(cfg(feature = "genrand")))] pub trait GenerateRandom { /// Create a completely random instance of the genome - fn gen_random(rng: &mut impl Rng) -> Self; + fn gen_random(rng: &mut impl rand::Rng) -> Self; } /// Blanket trait used on collections that contain objects implementing [`GenerateRandom`] @@ -158,7 +132,7 @@ where T: GenerateRandom, { /// Generate a random collection of the inner objects with a given amount - fn gen_random(rng: &mut impl Rng, amount: usize) -> Self; + fn gen_random(rng: &mut impl rand::Rng, amount: usize) -> Self; } /// Rayon version of the [`GenerateRandomCollection`] trait @@ -177,8 +151,7 @@ where C: FromIterator, T: GenerateRandom, { - #[cfg_attr(feature = "tracing", instrument)] - fn gen_random(rng: &mut impl Rng, amount: usize) -> Self { + fn gen_random(rng: &mut impl rand::Rng, amount: usize) -> Self { (0..amount).map(|_| T::gen_random(rng)).collect() } } @@ -189,7 +162,6 @@ where C: FromParallelIterator, T: GenerateRandom + Send, { - #[cfg_attr(feature = "tracing", instrument)] fn gen_random(amount: usize) -> Self { (0..amount) .into_par_iter() diff --git a/genetic-rs-common/src/prelude.rs b/genetic-rs-common/src/prelude.rs index d8d60b5..912dd16 100644 --- a/genetic-rs-common/src/prelude.rs +++ b/genetic-rs-common/src/prelude.rs @@ -5,4 +5,4 @@ pub use crate::*; #[cfg(feature = "builtin")] pub use crate::builtin::{eliminator::*, repopulator::*}; -pub use rand::Rng as RandRng; +pub use rand::prelude::*; \ No newline at end of file diff --git a/genetic-rs-macros/src/lib.rs b/genetic-rs-macros/src/lib.rs index acd3050..3a44d39 100644 --- a/genetic-rs-macros/src/lib.rs +++ b/genetic-rs-macros/src/lib.rs @@ -35,7 +35,7 @@ pub fn randmut_derive(input: TokenStream) -> TokenStream { quote! { impl genetic_rs_common::prelude::RandomlyMutable for #name { - fn mutate(&mut self, rate: f32, rng: &mut impl genetic_rs_common::Rng) { + fn mutate(&mut self, rate: f32, rng: &mut impl rand::Rng) { #inner } } @@ -95,7 +95,7 @@ pub fn crossover_derive(input: TokenStream) -> TokenStream { if tuple_struct { quote! { impl genetic_rs_common::prelude::Crossover for #name { - fn crossover(&self, other: &Self, rate: f32, rng: &mut impl genetic_rs_common::Rng) -> Self { + fn crossover(&self, other: &Self, rate: f32, rng: &mut impl rand::Rng) -> Self { Self(#inner) } } @@ -103,7 +103,7 @@ pub fn crossover_derive(input: TokenStream) -> TokenStream { } else { quote! { impl genetic_rs_common::prelude::Crossover for #name { - fn crossover(&self, other: &Self, rate: f32, rng: &mut impl genetic_rs_common::Rng) -> Self { + fn crossover(&self, other: &Self, rate: f32, rng: &mut impl rand::Rng) -> Self { Self { #inner } @@ -153,7 +153,7 @@ pub fn genrand_derive(input: TokenStream) -> TokenStream { if tuple_struct { quote! { impl genetic_rs_common::prelude::GenerateRandom for #name { - fn gen_random(rng: &mut impl genetic_rs_common::Rng) -> Self { + fn gen_random(rng: &mut impl rand::Rng) -> Self { Self(#inner) } } @@ -162,7 +162,7 @@ pub fn genrand_derive(input: TokenStream) -> TokenStream { } else { quote! { impl genetic_rs_common::prelude::GenerateRandom for #name { - fn gen_random(rng: &mut impl genetic_rs_common::Rng) -> Self { + fn gen_random(rng: &mut impl rand::Rng) -> Self { Self { #inner } diff --git a/genetic-rs/Cargo.toml b/genetic-rs/Cargo.toml index 93e6357..f2376aa 100644 --- a/genetic-rs/Cargo.toml +++ b/genetic-rs/Cargo.toml @@ -19,7 +19,6 @@ speciation = ["crossover", "genetic-rs-common/speciation"] genrand = ["genetic-rs-common/genrand"] rayon = ["genetic-rs-common/rayon"] derive = ["dep:genetic-rs-macros", "genetic-rs-common/builtin"] -tracing = ["genetic-rs-common/tracing"] [dependencies] genetic-rs-common = { path = "../genetic-rs-common", version = "1.0.0" } From cd2ca035d2d5860ea47f616b894181218a8534c2 Mon Sep 17 00:00:00 2001 From: Tristan Murphy <72839119+HyperCodec@users.noreply.github.com> Date: Tue, 7 Oct 2025 11:50:55 +0000 Subject: [PATCH 2/2] cargo fmt --- genetic-rs-common/src/prelude.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/genetic-rs-common/src/prelude.rs b/genetic-rs-common/src/prelude.rs index 912dd16..bd3c19f 100644 --- a/genetic-rs-common/src/prelude.rs +++ b/genetic-rs-common/src/prelude.rs @@ -5,4 +5,4 @@ pub use crate::*; #[cfg(feature = "builtin")] pub use crate::builtin::{eliminator::*, repopulator::*}; -pub use rand::prelude::*; \ No newline at end of file +pub use rand::prelude::*;