This is an automated email from the ASF dual-hosted git repository. lizhanhui pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/rocketmq-clients.git
The following commit(s) were added to refs/heads/master by this push: new bd9c1a11 Replace lazy_static with once_cell (#636) bd9c1a11 is described below commit bd9c1a11344670053aab2c818434651d571a29d4 Author: jy <mxon...@foxmail.com> AuthorDate: Fri Nov 10 10:04:41 2023 +0800 Replace lazy_static with once_cell (#636) * Replace lazy_static with once_cell Signed-off-by: iMXonren.Space <mxon...@foxmail.com> * fix: sync transaction_producer example Signed-off-by: Li Zhanhui <lizhan...@gmail.com> --------- Signed-off-by: iMXonren.Space <mxon...@foxmail.com> Signed-off-by: Li Zhanhui <lizhan...@gmail.com> Co-authored-by: Li Zhanhui <lizhan...@gmail.com> --- rust/Cargo.toml | 5 ++--- rust/examples/transaction_producer.rs | 5 ++--- rust/src/client.rs | 13 +++++-------- rust/src/util.rs | 11 +++++------ 4 files changed, 14 insertions(+), 20 deletions(-) diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 6cc106bb..da47ed29 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -36,7 +36,6 @@ tokio = { version = "1", features = ["full"] } tokio-rustls = { version = "0.24.0", features = ["default", "dangerous_configuration"] } tokio-stream = "0.1.12" async-trait = "0.1.68" -lazy_static = "1.4" tonic = { version = "0.9.0", features = ["tls", "default", "channel", "tls-roots"] } prost = "0.11.8" prost-types = "0.11.8" @@ -60,7 +59,7 @@ byteorder = "1" mac_address = "1.1.4" hex = "0.4.3" time = "0.3" -once_cell = "1.9.0" +once_cell = "1.18.0" mockall = "0.11.4" mockall_double = "0.3.0" @@ -82,4 +81,4 @@ awaitility = "0.3.0" [features] default = ["example_ack"] example_ack = [] -example_change_invisible_duration = [] \ No newline at end of file +example_change_invisible_duration = [] diff --git a/rust/examples/transaction_producer.rs b/rust/examples/transaction_producer.rs index 6df6cb65..4bc621fa 100644 --- a/rust/examples/transaction_producer.rs +++ b/rust/examples/transaction_producer.rs @@ -17,14 +17,13 @@ use std::collections::HashSet; use std::sync::Mutex; +use once_cell::sync::Lazy; use rocketmq::conf::{ClientOption, ProducerOption}; use rocketmq::model::message::MessageBuilder; use rocketmq::model::transaction::{Transaction, TransactionResolution}; use rocketmq::Producer; -lazy_static::lazy_static! { - static ref MESSAGE_ID_SET: Mutex<HashSet<String>> = Mutex::new(HashSet::new()); -} +static MESSAGE_ID_SET: Lazy<Mutex<HashSet<String>>> = Lazy::new(|| Mutex::new(HashSet::new())); #[tokio::main] async fn main() { diff --git a/rust/src/client.rs b/rust/src/client.rs index ae034682..69000be2 100644 --- a/rust/src/client.rs +++ b/rust/src/client.rs @@ -21,6 +21,7 @@ use std::{collections::HashMap, sync::atomic::AtomicUsize, sync::Arc}; use mockall::automock; use mockall_double::double; +use once_cell::sync::Lazy; use parking_lot::Mutex; use prost_types::Duration; use slog::{debug, error, info, o, warn, Logger}; @@ -58,9 +59,7 @@ pub(crate) struct Client { shutdown_tx: Option<oneshot::Sender<()>>, } -lazy_static::lazy_static! { - static ref CLIENT_ID_SEQUENCE: AtomicUsize = AtomicUsize::new(0); -} +static CLIENT_ID_SEQUENCE: Lazy<AtomicUsize> = Lazy::new(|| AtomicUsize::new(0)); const OPERATION_CLIENT_NEW: &str = "client.new"; const OPERATION_CLIENT_START: &str = "client.start"; @@ -698,7 +697,7 @@ pub(crate) mod tests { use std::thread::sleep; use std::time::Duration; - use lazy_static::lazy_static; + use once_cell::sync::Lazy; use crate::client::Client; use crate::conf::ClientOption; @@ -717,10 +716,8 @@ pub(crate) mod tests { use super::*; - lazy_static! { - // The lock is used to prevent the mocking static function at same time during parallel testing. - pub(crate) static ref MTX: Mutex<()> = Mutex::new(()); - } + // The lock is used to prevent the mocking static function at same time during parallel testing. + pub(crate) static MTX: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(())); fn new_client_for_test() -> Client { Client { diff --git a/rust/src/util.rs b/rust/src/util.rs index 6ea92cb5..ac935b6b 100644 --- a/rust/src/util.rs +++ b/rust/src/util.rs @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +use once_cell::sync::Lazy; use std::hash::Hasher; use std::sync::atomic::Ordering; use std::sync::Arc; @@ -33,12 +34,10 @@ pub(crate) static SDK_LANGUAGE: Language = Language::Rust; pub(crate) static SDK_VERSION: &str = "5.0.0"; pub(crate) static PROTOCOL_VERSION: &str = "2.0.0"; -lazy_static::lazy_static! { - pub(crate) static ref HOST_NAME: String = match hostname::get() { - Ok(name) => name.to_str().unwrap_or("localhost").to_string(), - Err(_) => "localhost".to_string(), - }; -} +pub(crate) static HOST_NAME: Lazy<String> = Lazy::new(|| match hostname::get() { + Ok(name) => name.to_str().unwrap_or("localhost").to_string(), + Err(_) => "localhost".to_string(), +}); pub(crate) fn select_message_queue(route: Arc<Route>) -> MessageQueue { let i = route.index.fetch_add(1, Ordering::Relaxed);