public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox] subscription: Use const rather than static
@ 2024-02-28  9:54 Maximiliano Sandoval
  2024-02-28 10:54 ` Max Carrara
  0 siblings, 1 reply; 2+ messages in thread
From: Maximiliano Sandoval @ 2024-02-28  9:54 UTC (permalink / raw)
  To: pbs-devel

A static is like a const that can mutate, we don't need that.

Signed-off-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
---
 proxmox-rest-server/src/formatter.rs          | 2 +-
 proxmox-subscription/src/subscription_info.rs | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/proxmox-rest-server/src/formatter.rs b/proxmox-rest-server/src/formatter.rs
index d19d6805..b9ed3bce 100644
--- a/proxmox-rest-server/src/formatter.rs
+++ b/proxmox-rest-server/src/formatter.rs
@@ -41,7 +41,7 @@ pub trait OutputFormatter: Send + Sync {
     }
 }
 
-static JSON_CONTENT_TYPE: &str = "application/json;charset=UTF-8";
+const JSON_CONTENT_TYPE: &str = "application/json;charset=UTF-8";
 
 fn json_data_response(data: Value) -> Response<Body> {
     let json_str = data.to_string();
diff --git a/proxmox-subscription/src/subscription_info.rs b/proxmox-subscription/src/subscription_info.rs
index ae40dbff..c2e3e7ea 100644
--- a/proxmox-subscription/src/subscription_info.rs
+++ b/proxmox-subscription/src/subscription_info.rs
@@ -302,7 +302,7 @@ pub(crate) fn md5sum(data: &[u8]) -> Result<DigestBytes, Error> {
 
 /// Generate the current system's "server ID".
 pub fn get_hardware_address() -> Result<String, Error> {
-    static FILENAME: &str = "/etc/ssh/ssh_host_rsa_key.pub";
+    const FILENAME: &str = "/etc/ssh/ssh_host_rsa_key.pub";
 
     let contents = proxmox_sys::fs::file_get_contents(FILENAME)
         .map_err(|e| format_err!("Error getting host key - {}", e))?;
-- 
2.39.2





^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [pbs-devel] [PATCH proxmox] subscription: Use const rather than static
  2024-02-28  9:54 [pbs-devel] [PATCH proxmox] subscription: Use const rather than static Maximiliano Sandoval
@ 2024-02-28 10:54 ` Max Carrara
  0 siblings, 0 replies; 2+ messages in thread
From: Max Carrara @ 2024-02-28 10:54 UTC (permalink / raw)
  To: pbs-devel

On 2/28/24 10:54, Maximiliano Sandoval wrote:
> A static is like a const that can mutate, we don't need that.

This is unfortunately incorrect - there is no reason to use `const`
here.

`const` items are essentially "inlined" wherever they are used;
they do *not* have a fixed location in memory. [0]

However, `static` items have a fixed location in memory. [1]

The static items here in particular are immutable; they're
string slice references with a static lifetime (`&'static str`).
Mutating a `&str` is not something that one usually wants either,
and we don't do that here anyways.

[0]: https://doc.rust-lang.org/reference/items/constant-items.html
[1]: https://doc.rust-lang.org/reference/items/static-items.html

> 
> Signed-off-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
> ---
>  proxmox-rest-server/src/formatter.rs          | 2 +-
>  proxmox-subscription/src/subscription_info.rs | 2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/proxmox-rest-server/src/formatter.rs b/proxmox-rest-server/src/formatter.rs
> index d19d6805..b9ed3bce 100644
> --- a/proxmox-rest-server/src/formatter.rs
> +++ b/proxmox-rest-server/src/formatter.rs
> @@ -41,7 +41,7 @@ pub trait OutputFormatter: Send + Sync {
>      }
>  }
>  
> -static JSON_CONTENT_TYPE: &str = "application/json;charset=UTF-8";
> +const JSON_CONTENT_TYPE: &str = "application/json;charset=UTF-8";
>  
>  fn json_data_response(data: Value) -> Response<Body> {
>      let json_str = data.to_string();
> diff --git a/proxmox-subscription/src/subscription_info.rs b/proxmox-subscription/src/subscription_info.rs
> index ae40dbff..c2e3e7ea 100644
> --- a/proxmox-subscription/src/subscription_info.rs
> +++ b/proxmox-subscription/src/subscription_info.rs
> @@ -302,7 +302,7 @@ pub(crate) fn md5sum(data: &[u8]) -> Result<DigestBytes, Error> {
>  
>  /// Generate the current system's "server ID".
>  pub fn get_hardware_address() -> Result<String, Error> {
> -    static FILENAME: &str = "/etc/ssh/ssh_host_rsa_key.pub";
> +    const FILENAME: &str = "/etc/ssh/ssh_host_rsa_key.pub";
>  
>      let contents = proxmox_sys::fs::file_get_contents(FILENAME)
>          .map_err(|e| format_err!("Error getting host key - {}", e))?;





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-02-28 10:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-28  9:54 [pbs-devel] [PATCH proxmox] subscription: Use const rather than static Maximiliano Sandoval
2024-02-28 10:54 ` Max Carrara

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal