all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Maximiliano Sandoval <m.sandoval@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox v2 05/12] rest-server: remove lazy_static dependency
Date: Wed, 14 Aug 2024 09:19:54 +0200	[thread overview]
Message-ID: <20240814072001.53422-5-m.sandoval@proxmox.com> (raw)
In-Reply-To: <20240814072001.53422-1-m.sandoval@proxmox.com>

Signed-off-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
---
 proxmox-rest-server/Cargo.toml                      |  2 +-
 proxmox-rest-server/examples/minimal-rest-server.rs |  8 +++-----
 proxmox-rest-server/src/lib.rs                      | 11 +++++++----
 proxmox-rest-server/src/rest.rs                     |  7 ++-----
 proxmox-rest-server/src/worker_task.rs              |  9 +++------
 5 files changed, 16 insertions(+), 21 deletions(-)

diff --git a/proxmox-rest-server/Cargo.toml b/proxmox-rest-server/Cargo.toml
index 3f69e550..e35cb42f 100644
--- a/proxmox-rest-server/Cargo.toml
+++ b/proxmox-rest-server/Cargo.toml
@@ -6,6 +6,7 @@ edition.workspace = true
 license.workspace = true
 repository.workspace = true
 description = "REST server implementation"
+rust-version.workspace = true
 
 exclude.workspace = true
 
@@ -19,7 +20,6 @@ futures.workspace = true
 handlebars = { workspace = true, optional = true }
 http.workspace = true
 hyper = { workspace = true, features = [ "full" ] }
-lazy_static.workspace = true
 libc.workspace = true
 log.workspace = true
 nix.workspace = true
diff --git a/proxmox-rest-server/examples/minimal-rest-server.rs b/proxmox-rest-server/examples/minimal-rest-server.rs
index a93ef0f5..d6a5f2eb 100644
--- a/proxmox-rest-server/examples/minimal-rest-server.rs
+++ b/proxmox-rest-server/examples/minimal-rest-server.rs
@@ -1,13 +1,12 @@
 use std::collections::HashMap;
 use std::future::Future;
 use std::pin::Pin;
-use std::sync::Mutex;
+use std::sync::{LazyLock, Mutex};
 
 use anyhow::{bail, format_err, Error};
 use http::request::Parts;
 use http::HeaderMap;
 use hyper::{Body, Method, Response};
-use lazy_static::lazy_static;
 
 use proxmox_router::{
     list_subdirs_api_method, Router, RpcEnvironmentType, SubdirMap, UserInformation,
@@ -70,9 +69,8 @@ fn ping() -> Result<String, Error> {
     Ok("pong".to_string())
 }
 
-lazy_static! {
-    static ref ITEM_MAP: Mutex<HashMap<String, String>> = Mutex::new(HashMap::new());
-}
+static ITEM_MAP: LazyLock<Mutex<HashMap<String, String>>> =
+    LazyLock::new(|| Mutex::new(HashMap::new()));
 
 #[api]
 /// Lists all current items
diff --git a/proxmox-rest-server/src/lib.rs b/proxmox-rest-server/src/lib.rs
index 3c9e887b..43dafa91 100644
--- a/proxmox-rest-server/src/lib.rs
+++ b/proxmox-rest-server/src/lib.rs
@@ -17,6 +17,7 @@
 #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
 
 use std::fmt;
+use std::sync::LazyLock;
 
 use anyhow::{format_err, Error};
 use nix::unistd::Pid;
@@ -46,10 +47,12 @@ pub use worker_task::*;
 mod h2service;
 pub use h2service::*;
 
-lazy_static::lazy_static! {
-    static ref PID: i32 = unsafe { libc::getpid() };
-    static ref PSTART: u64 = PidStat::read_from_pid(Pid::from_raw(*PID)).unwrap().starttime;
-}
+static PID: LazyLock<i32> = LazyLock::new(|| unsafe { libc::getpid() });
+static PSTART: LazyLock<u64> = LazyLock::new(|| {
+    PidStat::read_from_pid(Pid::from_raw(*PID))
+        .unwrap()
+        .starttime
+});
 
 /// Returns the current process ID (see [libc::getpid])
 ///
diff --git a/proxmox-rest-server/src/rest.rs b/proxmox-rest-server/src/rest.rs
index fa9e043a..8b444c80 100644
--- a/proxmox-rest-server/src/rest.rs
+++ b/proxmox-rest-server/src/rest.rs
@@ -4,7 +4,7 @@ use std::hash::BuildHasher;
 use std::io;
 use std::path::{Path, PathBuf};
 use std::pin::Pin;
-use std::sync::{Arc, Mutex};
+use std::sync::{Arc, LazyLock, Mutex};
 use std::task::{Context, Poll};
 
 use anyhow::{bail, format_err, Error};
@@ -14,7 +14,6 @@ use hyper::body::HttpBody;
 use hyper::header::{self, HeaderMap};
 use hyper::http::request::Parts;
 use hyper::{Body, Request, Response, StatusCode};
-use lazy_static::lazy_static;
 use regex::Regex;
 use serde_json::Value;
 use tokio::fs::File;
@@ -289,9 +288,7 @@ fn log_response(
 }
 
 fn get_proxied_peer(headers: &HeaderMap) -> Option<std::net::SocketAddr> {
-    lazy_static! {
-        static ref RE: Regex = Regex::new(r#"for="([^"]+)""#).unwrap();
-    }
+    static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#"for="([^"]+)""#).unwrap());
     let forwarded = headers.get(header::FORWARDED)?.to_str().ok()?;
     let capture = RE.captures(forwarded)?;
     let rhost = capture.get(1)?.as_str();
diff --git a/proxmox-rest-server/src/worker_task.rs b/proxmox-rest-server/src/worker_task.rs
index 62e5893f..228abb7f 100644
--- a/proxmox-rest-server/src/worker_task.rs
+++ b/proxmox-rest-server/src/worker_task.rs
@@ -4,12 +4,11 @@ use std::io::{BufRead, BufReader, Read, Write};
 use std::panic::UnwindSafe;
 use std::path::PathBuf;
 use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
-use std::sync::{Arc, Mutex, OnceLock};
+use std::sync::{Arc, LazyLock, Mutex, OnceLock};
 use std::time::{Duration, SystemTime};
 
 use anyhow::{bail, format_err, Error};
 use futures::*;
-use lazy_static::lazy_static;
 use nix::fcntl::OFlag;
 use once_cell::sync::OnceCell;
 use serde::{Deserialize, Serialize};
@@ -497,10 +496,8 @@ pub fn upid_read_status(upid: &UPID) -> Result<TaskState, Error> {
     Ok(TaskState::Unknown { endtime }) // no last line with both, end-time and task-state, found.
 }
 
-lazy_static! {
-    static ref WORKER_TASK_LIST: Mutex<HashMap<usize, Arc<WorkerTask>>> =
-        Mutex::new(HashMap::new());
-}
+static WORKER_TASK_LIST: LazyLock<Mutex<HashMap<usize, Arc<WorkerTask>>>> =
+    LazyLock::new(|| Mutex::new(HashMap::new()));
 
 /// checks if the task UPID refers to a worker from this process
 fn is_local_worker(upid: &UPID) -> bool {
-- 
2.39.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


  parent reply	other threads:[~2024-08-14  7:19 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-14  7:19 [pbs-devel] [PATCH proxmox v2 01/12] cargo: set msrv to 1.80 Maximiliano Sandoval
2024-08-14  7:19 ` [pbs-devel] [PATCH proxmox v2 02/12] dns-api: remove lazy-static dependency Maximiliano Sandoval
2024-08-14  7:19 ` [pbs-devel] [PATCH proxmox v2 03/12] async: remove lazy_static dependency Maximiliano Sandoval
2024-08-14  7:19 ` [pbs-devel] [PATCH proxmox v2 04/12] subscription: " Maximiliano Sandoval
2024-08-14  7:19 ` Maximiliano Sandoval [this message]
2024-08-14  7:19 ` [pbs-devel] [PATCH proxmox v2 06/12] time: " Maximiliano Sandoval
2024-08-14  7:19 ` [pbs-devel] [PATCH proxmox v2 07/12] sys: " Maximiliano Sandoval
2024-08-14  7:19 ` [pbs-devel] [PATCH proxmox v2 08/12] network-api: " Maximiliano Sandoval
2024-08-14  7:19 ` [pbs-devel] [PATCH proxmox v2 09/12] auth-api: " Maximiliano Sandoval
2024-08-14  7:19 ` [pbs-devel] [PATCH proxmox v2 10/12] acme-api: " Maximiliano Sandoval
2024-08-14  7:20 ` [pbs-devel] [PATCH proxmox v2 11/12] schema: " Maximiliano Sandoval
2024-08-14  7:20 ` [pbs-devel] [PATCH proxmox v2 12/12] cargo: remove lazy_static dependency on workspace Maximiliano Sandoval
2024-08-14  8:51 ` [pbs-devel] [PATCH proxmox v2 01/12] cargo: set msrv to 1.80 Wolfgang Bumiller
2024-08-14 10:04 ` [pbs-devel] applied-series: " Wolfgang Bumiller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240814072001.53422-5-m.sandoval@proxmox.com \
    --to=m.sandoval@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal