From: Gabriel Goller <g.goller@proxmox.com>
To: pve-devel@lists.proxmox.com
Cc: Wolfgang Bumiller <w.bumiller@proxmox.com>
Subject: [PATCH proxmox v2] schema: api_string_type: impl FromStr to avoid allocating in error path
Date: Mon, 23 Mar 2026 10:21:33 +0100 [thread overview]
Message-ID: <20260323092140.42275-1-g.goller@proxmox.com> (raw)
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
next reply other threads:[~2026-03-23 9:21 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-23 9:21 Gabriel Goller [this message]
2026-03-24 12:37 ` applied: " Wolfgang Bumiller
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=20260323092140.42275-1-g.goller@proxmox.com \
--to=g.goller@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
--cc=w.bumiller@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