From c54d3bcda76ee5b49266b479ff5970dd0d5ba18b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=9C=E4=B8=96=E6=A9=8B=20Du=20Shiqiao?= Date: Wed, 29 May 2024 11:25:18 +0900 Subject: [PATCH 1/2] add wasm support to use web-time instead of std::time (#30) --- Cargo.toml | 3 +++ src/lib.rs | 3 +++ src/optim/base.rs | 4 +--- src/optim/epsilon_greedy.rs | 4 +--- src/optim/generic.rs | 9 ++------- src/optim/hill_climbing.rs | 4 +--- src/optim/logistic_annealing.rs | 4 +--- src/optim/relative_annealing.rs | 4 +--- src/optim/simulated_annealing.rs | 8 ++------ src/optim/tabu_search.rs | 9 ++------- src/time_wrapper.rs | 5 +++++ 11 files changed, 22 insertions(+), 35 deletions(-) create mode 100644 src/time_wrapper.rs diff --git a/Cargo.toml b/Cargo.toml index b48f78e..3fb2f74 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,9 @@ rayon = "1.8.0" trait-set = "0.3.0" auto_impl = "1.1.0" +[target.'cfg(target_family = "wasm")'.dependencies] +web-time = "1.1.0" + [dev-dependencies] approx = "0.5.1" indicatif = "0.17.7" diff --git a/src/lib.rs b/src/lib.rs index 384f198..2bb3c7a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,9 @@ pub use callback::{OptCallbackFn, OptProgress}; mod model; pub use model::OptModel; +mod time_wrapper; +pub use time_wrapper::{Duration, Instant}; + /// Crate verison string pub const VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/src/optim/base.rs b/src/optim/base.rs index 4ebc048..f6fe26d 100644 --- a/src/optim/base.rs +++ b/src/optim/base.rs @@ -1,9 +1,7 @@ -use std::time::Duration; - use auto_impl::auto_impl; use trait_set::trait_set; -use crate::{callback::OptCallbackFn, OptModel}; +use crate::{callback::OptCallbackFn, Duration, OptModel}; /// Optimizer that implements local search algorithm #[auto_impl(&, Box, Rc, Arc)] diff --git a/src/optim/epsilon_greedy.rs b/src/optim/epsilon_greedy.rs index e3f98eb..1941c42 100644 --- a/src/optim/epsilon_greedy.rs +++ b/src/optim/epsilon_greedy.rs @@ -1,6 +1,4 @@ -use std::time::Duration; - -use crate::{callback::OptCallbackFn, OptModel}; +use crate::{callback::OptCallbackFn, Duration, OptModel}; use super::{base::LocalSearchOptimizer, GenericLocalSearchOptimizer}; diff --git a/src/optim/generic.rs b/src/optim/generic.rs index 54d944f..0fa7496 100644 --- a/src/optim/generic.rs +++ b/src/optim/generic.rs @@ -1,16 +1,11 @@ -use std::{ - cell::RefCell, - marker::PhantomData, - rc::Rc, - time::{Duration, Instant}, -}; +use std::{cell::RefCell, marker::PhantomData, rc::Rc}; use rand::Rng; use rayon::prelude::*; use crate::{ callback::{OptCallbackFn, OptProgress}, - OptModel, + Duration, Instant, OptModel, }; use super::{LocalSearchOptimizer, TransitionProbabilityFn}; diff --git a/src/optim/hill_climbing.rs b/src/optim/hill_climbing.rs index 4279777..55c4531 100644 --- a/src/optim/hill_climbing.rs +++ b/src/optim/hill_climbing.rs @@ -1,6 +1,4 @@ -use std::time::Duration; - -use crate::{callback::OptCallbackFn, OptModel}; +use crate::{callback::OptCallbackFn, Duration, OptModel}; use super::{EpsilonGreedyOptimizer, LocalSearchOptimizer}; diff --git a/src/optim/logistic_annealing.rs b/src/optim/logistic_annealing.rs index 59472d6..6fd0a02 100644 --- a/src/optim/logistic_annealing.rs +++ b/src/optim/logistic_annealing.rs @@ -1,8 +1,6 @@ -use std::time::Duration; - use ordered_float::NotNan; -use crate::{callback::OptCallbackFn, OptModel}; +use crate::{callback::OptCallbackFn, Duration, OptModel}; use super::{GenericLocalSearchOptimizer, LocalSearchOptimizer}; diff --git a/src/optim/relative_annealing.rs b/src/optim/relative_annealing.rs index 003644b..d41b44d 100644 --- a/src/optim/relative_annealing.rs +++ b/src/optim/relative_annealing.rs @@ -1,8 +1,6 @@ -use std::time::Duration; - use ordered_float::NotNan; -use crate::{callback::OptCallbackFn, OptModel}; +use crate::{callback::OptCallbackFn, Duration, OptModel}; use super::{GenericLocalSearchOptimizer, LocalSearchOptimizer}; diff --git a/src/optim/simulated_annealing.rs b/src/optim/simulated_annealing.rs index d043a28..cb1086f 100644 --- a/src/optim/simulated_annealing.rs +++ b/src/optim/simulated_annealing.rs @@ -1,8 +1,4 @@ -use std::{ - cell::RefCell, - rc::Rc, - time::{Duration, Instant}, -}; +use std::{cell::RefCell, rc::Rc}; use ordered_float::NotNan; use rand::Rng; @@ -10,7 +6,7 @@ use rayon::prelude::*; use crate::{ callback::{OptCallbackFn, OptProgress}, - OptModel, + Duration, Instant, OptModel, }; use super::LocalSearchOptimizer; diff --git a/src/optim/tabu_search.rs b/src/optim/tabu_search.rs index 441bc82..f2962b8 100644 --- a/src/optim/tabu_search.rs +++ b/src/optim/tabu_search.rs @@ -1,16 +1,11 @@ -use std::{ - cell::RefCell, - marker::PhantomData, - rc::Rc, - time::{Duration, Instant}, -}; +use std::{cell::RefCell, marker::PhantomData, rc::Rc}; use auto_impl::auto_impl; use rayon::prelude::*; use crate::{ callback::{OptCallbackFn, OptProgress}, - OptModel, + Duration, Instant, OptModel, }; use super::LocalSearchOptimizer; diff --git a/src/time_wrapper.rs b/src/time_wrapper.rs new file mode 100644 index 0000000..ef42010 --- /dev/null +++ b/src/time_wrapper.rs @@ -0,0 +1,5 @@ +#[cfg(not(target_family = "wasm"))] +pub use std::time::{Duration, Instant}; + +#[cfg(target_family = "wasm")] +pub use web_time::{Duration, Instant}; From cc922816c5ab77523337c74023f34b1b2f225979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=9C=20=E4=B8=96=E6=A9=8B=20Du=20Shiqiao?= Date: Wed, 29 May 2024 11:40:23 +0900 Subject: [PATCH 2/2] bump version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 3fb2f74..1fc1b80 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ categories = ["algorithms"] repository = "https://github.com/lucidfrontier45/localsearch" license-file = "LICENSE" readme = "README.md" -version = "0.12.0" +version = "0.12.1" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html