all lists on lists.proxmox.com
 help / color / mirror / Atom feed
* [PATCH proxmox v2] schema: api_string_type: impl FromStr to avoid allocating in error path
@ 2026-03-23  9:21 Gabriel Goller
  2026-03-24 12:37 ` applied: " Wolfgang Bumiller
  0 siblings, 1 reply; 2+ messages in thread
From: Gabriel Goller @ 2026-03-23  9:21 UTC (permalink / raw)
  To: pve-devel; +Cc: Wolfgang Bumiller

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





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

end of thread, other threads:[~2026-03-24 12:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-03-23  9:21 [PATCH proxmox v2] schema: api_string_type: impl FromStr to avoid allocating in error path Gabriel Goller
2026-03-24 12:37 ` applied: " Wolfgang Bumiller

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal