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 65FF062829 for ; Tue, 27 Oct 2020 09:24:02 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 584C215886 for ; Tue, 27 Oct 2020 09:24:02 +0100 (CET) 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 BAC001587C for ; Tue, 27 Oct 2020 09:24:01 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 59C8345F4E for ; Tue, 27 Oct 2020 09:24:01 +0100 (CET) Date: Tue, 27 Oct 2020 09:24:00 +0100 From: Wolfgang Bumiller To: Thomas Lamprecht Cc: pbs-devel@lists.proxmox.com Message-ID: <20201027082400.c4a53onc6wxugxir@wobu-vie.proxmox.com> References: <20201026172927.22843-1-t.lamprecht@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201026172927.22843-1-t.lamprecht@proxmox.com> User-Agent: NeoMutt/20180716 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.014 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. [foo.rs, method.rs] Subject: [pbs-devel] applied: [PATCH] api macro: reuse generated default const for "unwrap_or" 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, 27 Oct 2020 08:24:02 -0000 applied On Mon, Oct 26, 2020 at 06:29:27PM +0100, Thomas Lamprecht wrote: > Instead of setting a default value to a const and inside an > .unwrap_or_else closure, lets set it only to the const and reuse that > later in .unwrap_or > > To achieve that we move the "unrwap_or" code for param plumbing code generation > a bit later so that we have easy access to the generated const name. > As all this code is related to optional/default-value stuff it does read still > relatively OK with that change, IMO. > > This has the advantage of not getting a warning like: > > > warning: constant is never used: `API_METHOD_EXAMPLE_FOO_PARAM_DEFAULT_FORCE` > > --> src/api2/node/foo.rs > > | > > XY | force: { > > | ^^^^^ > > = note: `#[warn(dead_code)]` on by default > > When one has a API endpoint like: > > > #[api( > > input: { > > properties: { > > force: { > > type: bool, > > optional: true, > > default: false, > > }, > > }, > > }, > > ... > > )] > > /// Example > > fn example_foo(force: bool) -> Result<(), Error> { > > if force { > > // do something > > } > > Ok(()) > > } > > It effectively changes the output for optional parameters with a default set > and no Option from > > > let p = p.unwrap_or_else(|| #default_value); > > to > > > let p = p.unwrap_or(#const_name_for_default); > > where the "#const_name_for_default" is a pub const with value > "#default_value" > > Signed-off-by: Thomas Lamprecht > --- > > So, I did not checked the surrounding stuff in depth, but it seemed relatively > clear what is happening here. So, while a good double check is surely good, and > soem implementation details may be discussed, it does not breaks tests (and I > negative checked that, to ensure there are tests for this) plus my use case > works too, the "constant is never used" warning goes away. > > proxmox-api-macro/src/api/method.rs | 22 +++++++++++----------- > 1 file changed, 11 insertions(+), 11 deletions(-) > > diff --git a/proxmox-api-macro/src/api/method.rs b/proxmox-api-macro/src/api/method.rs > index e94594f..1d32c60 100644 > --- a/proxmox-api-macro/src/api/method.rs > +++ b/proxmox-api-macro/src/api/method.rs > @@ -414,18 +414,8 @@ fn create_wrapper_function( > #name_str, > ))? > }); > - } else if util::is_option_type(ty).is_none() { > - // Optional parameter without an Option type requires a default: > - if let Some(def) = &default_value { > - body.extend(quote_spanned! { span => > - .unwrap_or_else(|| #def) > - }); > - } else { > - bail!(ty => "Optional parameter without Option requires a default"); > - } > } > - body.extend(quote_spanned! { span => ; }); > - args.extend(quote_spanned! { span => #arg_name, }); > + let no_option_type = util::is_option_type(ty).is_none(); > > if let Some(def) = &default_value { > let name_uc = name.as_ident().to_string().to_uppercase(); > @@ -438,7 +428,17 @@ fn create_wrapper_function( > default_consts.extend(quote_spanned! { span => > pub const #name: #ty = #def; > }); > + if optional && no_option_type { > + // Optional parameter without an Option type requires a default: > + body.extend(quote_spanned! { span => > + .unwrap_or(#name) > + }); > + } > + } else if optional && no_option_type { > + bail!(ty => "Optional parameter without Option requires a default"); > } > + body.extend(quote_spanned! { span => ; }); > + args.extend(quote_spanned! { span => #arg_name, }); > } > } > } > -- > 2.27.0