Skip to content

Commit

Permalink
⚡️ zu: Add Signature::to_string()
Browse files Browse the repository at this point in the history
This produces the same output as the `ToString::to_string`, except it
preallocates the required memory and hence avoids reallocations and moving
of data.
  • Loading branch information
zeenix committed Nov 30, 2024
1 parent b7a21a8 commit 35efa6c
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion zvariant_utils/src/signature/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl Signature {

/// Convert `self` to a string, without any enclosing parenthesis.
///
/// This produces the same output as the `ToString::to_string`, unless `self` is a
/// This produces the same output as the [`Signature::to_string`], unless `self` is a
/// [`Signature::Structure`], in which case the written string will **not** be wrapped in
/// parenthesis (`()`).
pub fn to_string_no_parens(&self) -> String {
Expand All @@ -159,6 +159,18 @@ impl Signature {
s
}

/// Convert `self` to a string.
///
/// This produces the same output as the `ToString::to_string`, except it preallocates the
/// required memory and hence avoids reallocations and moving of data.
#[allow(clippy::inherent_to_string_shadow_display)]
pub fn to_string(&self) -> String {
let mut s = String::with_capacity(self.string_len());
self.write_as_string(&mut s, true).unwrap();

s
}

/// Parse signature from a byte slice.
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
parse(bytes, false)
Expand Down

0 comments on commit 35efa6c

Please sign in to comment.