summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeal H. Walfield <neal@sequoia-pgp.org>2024-01-23 16:11:43 +0100
committerNeal H. Walfield <neal@sequoia-pgp.org>2024-01-23 16:15:17 +0100
commit8354a954b2b0c25ee142475de88328cb8f49d270 (patch)
tree2bb6d0d46c79a0cdce1aa1e0ef8f6194be0a70e3
parent0fa2a240fceee0d317e90be939fe0729ea2df9dd (diff)
openpgp: Avoid unnecessary heap allocations when creating UserIDs.
- When creating a `UserID`, avoid unnecessary heap allocations by making better use of what we have. For example, we can directly convert a `String` to a `Vec<u8>` without allocating a `Vec<u8>`, and copying the contents.
-rw-r--r--openpgp/src/packet/userid.rs21
1 files changed, 9 insertions, 12 deletions
diff --git a/openpgp/src/packet/userid.rs b/openpgp/src/packet/userid.rs
index 0b8b0ae6..53613d4d 100644
--- a/openpgp/src/packet/userid.rs
+++ b/openpgp/src/packet/userid.rs
@@ -1,3 +1,4 @@
+use std::borrow::Cow;
use std::fmt;
use std::str;
use std::hash::{Hash, Hasher};
@@ -498,26 +499,22 @@ impl From<&[u8]> for UserID {
impl<'a> From<&'a str> for UserID {
fn from(u: &'a str) -> Self {
- let b = u.as_bytes();
- let mut v = Vec::with_capacity(b.len());
- v.extend_from_slice(b);
- v.into()
+ u.as_bytes().into()
}
}
impl From<String> for UserID {
fn from(u: String) -> Self {
- let u = &u[..];
- u.into()
+ u.into_bytes().into()
}
}
-impl<'a> From<::std::borrow::Cow<'a, str>> for UserID {
- fn from(u: ::std::borrow::Cow<'a, str>) -> Self {
- let b = u.as_bytes();
- let mut v = Vec::with_capacity(b.len());
- v.extend_from_slice(b);
- v.into()
+impl<'a> From<Cow<'a, str>> for UserID {
+ fn from(u: Cow<'a, str>) -> Self {
+ match u {
+ Cow::Owned(u) => u.into(),
+ Cow::Borrowed(u) => u.into(),
+ }
}
}