From d0646eb5022703978fec7cc3e41bb70e632de761 Mon Sep 17 00:00:00 2001
From: Georgy Moshkin <gmoshkin@picodata.io>
Date: Tue, 27 Dec 2022 15:30:17 +0300
Subject: [PATCH] chore: remove lazy-static from dependencies

lazy-static crate is deprecated and is only used in 2 places, where it
can be easily replaced with something simpler.
---
 Cargo.lock       |  1 -
 Cargo.toml       |  1 -
 src/traft/mod.rs | 19 +++++++++++--------
 3 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index ebe7d21467..6fbf96e7e0 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -808,7 +808,6 @@ dependencies = [
  "futures",
  "indoc",
  "inventory",
- "lazy_static",
  "libc",
  "linkme",
  "nix 0.23.2",
diff --git a/Cargo.toml b/Cargo.toml
index df17a407a0..10217fad6b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -22,7 +22,6 @@ thiserror = "1.0"
 indoc = "1.0"
 nix = "0.23.1"
 base64 = "0.13"
-lazy_static = "1.4"
 uuid = {version = "1.0", features = ["v3"]}
 linkme = "0.2.10"
 futures = "0.3.25"
diff --git a/src/traft/mod.rs b/src/traft/mod.rs
index aaddea5b84..fe2adb9c24 100644
--- a/src/traft/mod.rs
+++ b/src/traft/mod.rs
@@ -372,27 +372,30 @@ pub trait ContextCoercion: Serialize + DeserializeOwned {
 }
 
 ///////////////////////////////////////////////////////////////////////////////
-lazy_static::lazy_static! {
-    static ref NAMESPACE_INSTANCE_UUID: Uuid =
-        Uuid::new_v3(&Uuid::nil(), "INSTANCE_UUID".as_bytes());
-    static ref NAMESPACE_REPLICASET_UUID: Uuid =
-        Uuid::new_v3(&Uuid::nil(), "REPLICASET_UUID".as_bytes());
-}
 
 /// Generate UUID for an instance from `instance_id` (String).
 /// Use Version-3 (MD5) UUID.
 pub fn instance_uuid(instance_id: &str) -> String {
-    let uuid = Uuid::new_v3(&NAMESPACE_INSTANCE_UUID, instance_id.as_bytes());
+    static mut NAMESPACE_INSTANCE_UUID: Option<Uuid> = None;
+    let ns = unsafe { NAMESPACE_INSTANCE_UUID.get_or_insert_with(|| uuid_v3("INSTANCE_UUID")) };
+    let uuid = Uuid::new_v3(ns, instance_id.as_bytes());
     uuid.hyphenated().to_string()
 }
 
 /// Generate UUID for a replicaset from `replicaset_id` (String).
 /// Use Version-3 (MD5) UUID.
 pub fn replicaset_uuid(replicaset_id: &str) -> String {
-    let uuid = Uuid::new_v3(&NAMESPACE_REPLICASET_UUID, replicaset_id.as_bytes());
+    static mut NAMESPACE_REPLICASET_UUID: Option<Uuid> = None;
+    let ns = unsafe { NAMESPACE_REPLICASET_UUID.get_or_insert_with(|| uuid_v3("REPLICASET_UUID")) };
+    let uuid = Uuid::new_v3(ns, replicaset_id.as_bytes());
     uuid.hyphenated().to_string()
 }
 
+#[inline(always)]
+fn uuid_v3(name: &str) -> Uuid {
+    Uuid::new_v3(&Uuid::nil(), name.as_bytes())
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 /// Migration
 #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
-- 
GitLab