Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡ zvariant, zbus: use borrowed clone if possible #407

Merged
merged 3 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions zbus/src/message_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl<'f> Serialize for MessageField<'f> {
S: Serializer,
{
let tuple: (MessageFieldCode, Value<'_>) = match self {
MessageField::Path(value) => (MessageFieldCode::Path, value.clone().into()),
MessageField::Path(value) => (MessageFieldCode::Path, value.as_ref().into()),
MessageField::Interface(value) => (MessageFieldCode::Interface, value.as_str().into()),
MessageField::Member(value) => (MessageFieldCode::Member, value.as_str().into()),
MessageField::ErrorName(value) => (MessageFieldCode::ErrorName, value.as_str().into()),
Expand All @@ -139,7 +139,7 @@ impl<'f> Serialize for MessageField<'f> {
(MessageFieldCode::Destination, value.as_str().into())
}
MessageField::Sender(value) => (MessageFieldCode::Sender, value.as_str().into()),
MessageField::Signature(value) => (MessageFieldCode::Signature, value.clone().into()),
MessageField::Signature(value) => (MessageFieldCode::Signature, value.as_ref().into()),
MessageField::UnixFDs(value) => (MessageFieldCode::UnixFDs, (*value).into()),
// This is a programmer error
MessageField::Invalid => panic!("Attempt to serialize invalid MessageField"),
Expand Down
9 changes: 8 additions & 1 deletion zvariant/src/from_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,14 @@ value_try_from_all!(Maybe, Maybe<'a>);

value_try_from!(Str, String);
value_try_from_ref!(Str, str);
value_try_from_ref_clone!(Str, String);

impl<'a> TryFrom<&'a Value<'a>> for String {
type Error = Error;

fn try_from(value: &'a Value<'_>) -> Result<Self, Self::Error> {
Ok(<&str>::try_from(value)?.into())
}
}

impl<'a, T> TryFrom<Value<'a>> for Vec<T>
where
Expand Down
18 changes: 18 additions & 0 deletions zvariant/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ impl<'b> Bytes<'b> {
fn owned(bytes: Vec<u8>) -> Self {
Self::Owned(bytes.into())
}

/// A borrowed clone (this never allocates, unlike clone).
fn as_ref(&self) -> Bytes<'_> {
match &self {
Bytes::Static(s) => Bytes::Static(s),
Bytes::Borrowed(s) => Bytes::Borrowed(s),
Bytes::Owned(s) => Bytes::Borrowed(s),
}
}
}

impl<'b> std::ops::Deref for Bytes<'b> {
Expand Down Expand Up @@ -104,6 +113,15 @@ impl<'a> Signature<'a> {
&self.bytes[self.pos..self.end]
}

/// A borrowed clone (this never allocates, unlike clone).
pub fn as_ref(&self) -> Signature<'_> {
Signature {
bytes: self.bytes.as_ref(),
pos: self.pos,
end: self.end,
}
}

/// Create a new Signature from given bytes.
///
/// Since the passed bytes are not checked for correctness, it's provided for ease of
Expand Down
6 changes: 3 additions & 3 deletions zvariant/src/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,13 @@ impl<'a> Default for Structure<'a> {

impl<'a> DynamicType for Structure<'a> {
fn dynamic_signature(&self) -> Signature<'_> {
self.signature.clone()
self.signature.as_ref()
}
}

impl<'a> DynamicType for StructureSeed<'a> {
fn dynamic_signature(&self) -> Signature<'_> {
self.0.clone()
self.0.as_ref()
}
}

Expand All @@ -274,7 +274,7 @@ impl<'a> DynamicDeserialize<'a> for Structure<'a> {
}

// The signature might be something like "(i)u(i)" - we need to parse it to check.
let mut parser = SignatureParser::new(signature.clone());
let mut parser = SignatureParser::new(signature.as_ref());
parser.parse_next_signature()?;
if !parser.done() {
// more than one element - we must wrap it
Expand Down
2 changes: 1 addition & 1 deletion zvariant/src/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ where
return Ok(PhantomData);
}

let mut signature = original.clone();
let mut signature = original.as_ref();
while expected.len() < signature.len()
&& signature.starts_with(STRUCT_SIG_START_CHAR)
&& signature.ends_with(STRUCT_SIG_END_CHAR)
Expand Down
10 changes: 5 additions & 5 deletions zvariant/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,11 @@ impl<'a> Value<'a> {
Value::Value(_) => Signature::from_static_str_unchecked("v"),

// Container types
Value::Array(value) => value.full_signature().clone(),
Value::Dict(value) => value.full_signature().clone(),
Value::Structure(value) => value.full_signature().clone(),
Value::Array(value) => value.full_signature().as_ref(),
Value::Dict(value) => value.full_signature().as_ref(),
Value::Structure(value) => value.full_signature().as_ref(),
#[cfg(feature = "gvariant")]
Value::Maybe(value) => value.full_signature().clone(),
Value::Maybe(value) => value.full_signature().as_ref(),

#[cfg(unix)]
Value::Fd(_) => Fd::signature(),
Expand Down Expand Up @@ -584,7 +584,7 @@ impl<'de> SignatureSeed<'de> {
let mut builder = StructureBuilder::new();
while i < signature_end {
let fields_signature = self.signature.slice(i..signature_end);
let parser = SignatureParser::new(fields_signature.clone());
let parser = SignatureParser::new(fields_signature.as_ref());
let len = parser.next_signature().map_err(Error::custom)?.len();
let field_signature = fields_signature.slice(0..len);
i += field_signature.len();
Expand Down
Loading