From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 95F26642C6 for ; Tue, 6 Oct 2020 12:09:03 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 9410824268 for ; Tue, 6 Oct 2020 12:09:03 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 7352924253 for ; Tue, 6 Oct 2020 12:09:02 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 3F16E458E9 for ; Tue, 6 Oct 2020 12:09:02 +0200 (CEST) From: Thomas Lamprecht To: pbs-devel@lists.proxmox.com Date: Tue, 6 Oct 2020 12:08:52 +0200 Message-Id: <20201006100854.18897-1-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.148 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [constnamedbitmap.rs, mod.rs, constnamemap.rs] Subject: [pbs-devel] [PATCH proxmox 1/3] tools: change constnamemap to a more automatic constnamedbitmap X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 06 Oct 2020 10:09:03 -0000 We only used this for the privileges for now, and there it's a nuisance to alter all bit definitions manually if something is added. This change makes it count the bits up automatically. Rename the macro to indicate that this is not a generic name map but a more specific named bit mapping. Signed-off-by: Thomas Lamprecht --- thanks to Wolfgang for his help here! .../{constnamemap.rs => constnamedbitmap.rs} | 51 +++++++++++-------- proxmox/src/tools/mod.rs | 2 +- 2 files changed, 31 insertions(+), 22 deletions(-) rename proxmox/src/tools/{constnamemap.rs => constnamedbitmap.rs} (50%) diff --git a/proxmox/src/tools/constnamemap.rs b/proxmox/src/tools/constnamedbitmap.rs similarity index 50% rename from proxmox/src/tools/constnamemap.rs rename to proxmox/src/tools/constnamedbitmap.rs index 5a774e0..eb1ba87 100644 --- a/proxmox/src/tools/constnamemap.rs +++ b/proxmox/src/tools/constnamedbitmap.rs @@ -1,19 +1,23 @@ -/// A macro to generate a list of pub const variabales and -/// an accompaning static array of a given name + value -/// (with doc comments) +/// A macro to generate a list of pub const variabales, and +/// an accompaning static array of a given name, the values are automatically +/// assigned to a bit (with doc comments) /// /// Example: /// ``` -/// # use proxmox::constnamemap; +/// # use proxmox::constnamedbitmap; /// -/// constnamemap! { +/// constnamedbitmap! { /// /// A list of privileges /// PRIVS: u64 => { /// /// Some comment for Priv1 -/// PRIV1("Priv1") = 1; -/// PRIV2("Priv2") = 2; +/// PRIV1("Priv1"); +/// PRIV2("Priv2"); +/// PRIV3("Priv3"); /// } /// } +/// # assert!(PRIV1 == 1<<0); +/// # assert!(PRIV2 == 1<<1); +/// # assert!(PRIV3 == 1<<2); /// ``` /// /// this will generate the following variables: @@ -21,15 +25,17 @@ /// /// Some comment for Priv1 /// pub const PRIV1: u64 = 1; /// pub const PRIV2: u64 = 2; +/// pub const PRIV3: u64 = 4; /// /// /// A list of privileges /// pub const PRIVS: &[(&str, u64)] = &[ -/// ("Priv1", 1), -/// ("Priv2", 2), +/// ("Priv1", PRIV1), +/// ("Priv2", PRIV2), +/// ("Priv3", PRIV3), /// ]; /// ``` #[macro_export(local_inner_macros)] -macro_rules! constnamemap { +macro_rules! constnamedbitmap { ( $(#[$outer:meta])* $name:ident : $type:ty => { @@ -37,7 +43,7 @@ macro_rules! constnamemap { } ) => { __constnamemap_consts! { - $type => $($content)+ + ($type) (0) => $($content)+ } $(#[$outer])* @@ -51,17 +57,20 @@ macro_rules! constnamemap { #[doc(hidden)] #[macro_export(local_inner_macros)] macro_rules! __constnamemap_consts { + (($type:ty) ($counter:expr) => ) => {}; ( - $type:ty => + ($type:ty) ($counter:expr) => + $(#[$outer:meta])* + $name:ident($text:expr); $( - $(#[$outer:meta])* - $name:ident($text:expr) = $value:expr; - )+ + $content:tt + )* ) => { - $( - $(#[$outer])* - pub const $name: $type = $value; - )+ + $(#[$outer])* + pub const $name: $type = 1 << ($counter); + __constnamemap_consts! { + ($type) (1+$counter) => $($content)* + } } } @@ -71,11 +80,11 @@ macro_rules! __constnamemap_entries { ( $( $(#[$outer:meta])* - $name:ident($text:expr) = $value:expr; + $name:ident($text:expr); )* ) => { &[ - $(($text,$value),)* + $(($text,$name),)* ] } } diff --git a/proxmox/src/tools/mod.rs b/proxmox/src/tools/mod.rs index df6c429..a158372 100644 --- a/proxmox/src/tools/mod.rs +++ b/proxmox/src/tools/mod.rs @@ -9,7 +9,7 @@ pub mod as_any; pub mod borrow; pub mod byte_buffer; pub mod common_regex; -pub mod constnamemap; +pub mod constnamedbitmap; pub mod email; pub mod fd; pub mod fs;