From: Wolfgang Bumiller <w.bumiller@proxmox.com>
To: Gabriel Goller <g.goller@proxmox.com>
Cc: pve-devel@lists.proxmox.com
Subject: applied: [PATCH proxmox v2] schema: api_string_type: impl FromStr to avoid allocating in error path
Date: Tue, 24 Mar 2026 13:37:12 +0100 [thread overview]
Message-ID: <ukbgxjjst2e2pyiuk7rmoid52mikcl3ybtli4uv23gsjakpgol@igujl7tez55n> (raw)
In-Reply-To: <20260323092140.42275-1-g.goller@proxmox.com>
applied, and added a follow up replacing the two schema type checks with
a const block for compile time checking using or
`.unwrap_string_schema()` const fn helper.
On Mon, Mar 23, 2026 at 10:21:33AM +0100, Gabriel Goller wrote:
> The existing from_string takes ownership of a String, meaning callers
> with a &str had to heap-allocate first and discard the allocation on
> validation failure. The new FromStr implementation validates the &str
> against the schema's check_constraints before calling to_string(), so no
> allocation happens on the error path.
>
> Suggested-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
> Signed-off-by: Gabriel Goller <g.goller@proxmox.com>
> ---
>
> Changelog:
> v2:
> - implemented FromStr trait instead of simple from_str method
>
> proxmox-schema/src/api_type_macros.rs | 14 ++++++++++++++
> 1 file changed, 14 insertions(+)
>
> diff --git a/proxmox-schema/src/api_type_macros.rs b/proxmox-schema/src/api_type_macros.rs
> index f3740d14fd35..4f2e54d4e63e 100644
> --- a/proxmox-schema/src/api_type_macros.rs
> +++ b/proxmox-schema/src/api_type_macros.rs
> @@ -21,6 +21,7 @@
> /// * `Display` as a pass-through to `String`'s `Display`
> /// * `Deref`
> /// * `DerefMut`
> +/// * `FromStr`
> /// * `AsRef<str>`
> /// * `TryFrom<String>`
> /// * `fn into_string(self) -> String`
> @@ -68,6 +69,19 @@ macro_rules! api_string_type {
> }
> }
>
> + impl ::std::str::FromStr for $name {
> + type Err = ::anyhow::Error;
> +
> + fn from_str(inner: &str) -> Result<Self, Self::Err> {
> + use $crate::ApiType;
> + match &Self::API_SCHEMA {
> + $crate::Schema::String(s) => s.check_constraints(inner)?,
> + _ => unreachable!(),
> + }
> + Ok(Self(inner.to_string()))
> + }
> + }
> +
> impl $name {
> /// Get the contained string.
> pub fn into_string(self) -> String {
> --
> 2.47.3
>
>
>
>
>
prev parent reply other threads:[~2026-03-24 12:36 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-23 9:21 Gabriel Goller
2026-03-24 12:37 ` Wolfgang Bumiller [this message]
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=ukbgxjjst2e2pyiuk7rmoid52mikcl3ybtli4uv23gsjakpgol@igujl7tez55n \
--to=w.bumiller@proxmox.com \
--cc=g.goller@proxmox.com \
--cc=pve-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