From d4f4949cd83972092f40253a10972d5388a7fd34 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 14 Dec 2021 11:07:01 +0100 Subject: [PATCH] Store a Cow<'a, str> in StringValue And parametrize all serialize-only types with a lifetime such that building requests no longer requires unnecessary allocations. --- src/common.rs | 38 +++++++++++++-------------- src/contact.rs | 48 +++++++++++++++++------------------ src/contact/check.rs | 27 +++++++++----------- src/contact/create.rs | 34 ++++++++++++------------- src/contact/delete.rs | 18 ++++++------- src/contact/info.rs | 46 ++++++++++++++++----------------- src/contact/update.rs | 34 ++++++++++++------------- src/domain.rs | 28 ++++++++++---------- src/domain/check.rs | 27 +++++++++----------- src/domain/create.rs | 36 +++++++++++++------------- src/domain/delete.rs | 18 ++++++------- src/domain/info.rs | 46 ++++++++++++++++----------------- src/domain/renew.rs | 24 +++++++++--------- src/domain/transfer.rs | 48 +++++++++++++++++------------------ src/domain/update.rs | 40 ++++++++++++++--------------- src/extensions/consolidate.rs | 4 +-- src/extensions/namestore.rs | 38 +++++++++++++-------------- src/extensions/rgp/report.rs | 46 ++++++++++++++++----------------- src/extensions/rgp/request.rs | 4 +-- src/hello.rs | 18 ++++++------- src/host/check.rs | 22 ++++++++-------- src/host/create.rs | 22 ++++++++-------- src/host/delete.rs | 18 ++++++------- src/host/info.rs | 34 ++++++++++++------------- src/host/update.rs | 26 +++++++++---------- src/login.rs | 28 ++++++++++---------- src/message/poll.rs | 14 +++++----- src/request.rs | 2 +- src/response.rs | 12 ++++----- 29 files changed, 396 insertions(+), 404 deletions(-) diff --git a/src/common.rs b/src/common.rs index 24604e4..bd43687 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,6 +1,6 @@ //! Common data types included in EPP Requests and Responses -use std::fmt::Display; +use std::{borrow::Cow, fmt::Display}; use serde::{Deserialize, Serialize}; @@ -11,23 +11,23 @@ pub(crate) const EPP_XMLNS: &str = "urn:ietf:params:xml:ns:epp-1.0"; /// Wraps String for easier serialization to and from values that are inner text /// for tags rather than attributes #[derive(Default, Serialize, Deserialize, Debug, PartialEq, Clone)] -pub struct StringValue(String); +pub struct StringValue<'a>(Cow<'a, str>); -impl Display for StringValue { +impl Display for StringValue<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.0) } } -impl From<&str> for StringValue { - fn from(s: &str) -> Self { - Self(s.to_owned()) +impl<'a> From<&'a str> for StringValue<'a> { + fn from(s: &'a str) -> Self { + Self(s.into()) } } -impl From for StringValue { +impl From for StringValue<'static> { fn from(s: String) -> Self { - Self(s) + Self(s.into()) } } @@ -42,17 +42,17 @@ impl Extension for NoExtension { /// The