From: Shannon Sterz <s.sterz@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox 2/2] product-config: use inner mutability for PRODUCT_CONFIG
Date: Tue, 3 Dec 2024 14:53:25 +0100 [thread overview]
Message-ID: <20241203135325.298743-2-s.sterz@proxmox.com> (raw)
In-Reply-To: <20241203135325.298743-1-s.sterz@proxmox.com>
with edition 2024 `static mut` references will be disallowed [1]. the
recommended way to work around this is to use inner mutability, so use a
`OnceLock` for the `PRODUCT_CONFIG` as that should not change throughout
the run time of an application.
[1]:
https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html
Signed-off-by: Shannon Sterz <s.sterz@proxmox.com>
---
proxmox-product-config/src/init.rs | 33 +++++++++++++++---------------
1 file changed, 16 insertions(+), 17 deletions(-)
diff --git a/proxmox-product-config/src/init.rs b/proxmox-product-config/src/init.rs
index a244559a..1bc47110 100644
--- a/proxmox-product-config/src/init.rs
+++ b/proxmox-product-config/src/init.rs
@@ -1,18 +1,21 @@
+use std::sync::OnceLock;
+
+#[derive(Debug)]
struct ProxmoxProductConfig {
api_user: nix::unistd::User,
priv_user: nix::unistd::User,
}
-static mut PRODUCT_CONFIG: Option<ProxmoxProductConfig> = None;
+static PRODUCT_CONFIG: OnceLock<ProxmoxProductConfig> = OnceLock::new();
/// Initialize the global product configuration.
pub fn init(api_user: nix::unistd::User, priv_user: nix::unistd::User) {
- unsafe {
- PRODUCT_CONFIG = Some(ProxmoxProductConfig {
+ PRODUCT_CONFIG
+ .set(ProxmoxProductConfig {
api_user,
priv_user,
- });
- }
+ })
+ .expect("cannot init proxmox product config twice");
}
/// Returns the global api user set with [init].
@@ -21,12 +24,10 @@ pub fn init(api_user: nix::unistd::User, priv_user: nix::unistd::User) {
///
/// Panics if [init] wasn't called before.
pub fn get_api_user() -> &'static nix::unistd::User {
- unsafe {
- &PRODUCT_CONFIG
- .as_ref()
- .expect("ProxmoxProductConfig is not initialized!")
- .api_user
- }
+ &PRODUCT_CONFIG
+ .get()
+ .expect("ProxmoxProductConfig is not initialized!")
+ .api_user
}
// Returns the global privileged user set with [init].
@@ -35,10 +36,8 @@ pub fn get_api_user() -> &'static nix::unistd::User {
///
/// Panics if [init] wasn't called before.
pub fn get_priv_user() -> &'static nix::unistd::User {
- unsafe {
- &PRODUCT_CONFIG
- .as_ref()
- .expect("ProxmoxProductConfig is not initialized!")
- .priv_user
- }
+ &PRODUCT_CONFIG
+ .get()
+ .expect("ProxmoxProductConfig is not initialized!")
+ .priv_user
}
--
2.39.5
_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel
next prev parent reply other threads:[~2024-12-03 13:53 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-03 13:53 [pbs-devel] [PATCH proxmox 1/2] acme-api: use inner mutability for ACME_ACME_CONFIG Shannon Sterz
2024-12-03 13:53 ` Shannon Sterz [this message]
2024-12-04 8:56 ` [pbs-devel] applied: " Dietmar Maurer
-- strict thread matches above, loose matches on Subject: below --
2024-12-03 13:48 [pbs-devel] " Shannon Sterz
2024-12-03 13:48 ` [pbs-devel] [PATCH proxmox 2/2] product-config: use inner mutability for PRODUCT_CONFIG Shannon Sterz
2024-12-03 13:53 ` Shannon Sterz
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=20241203135325.298743-2-s.sterz@proxmox.com \
--to=s.sterz@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox