Skip to content

Commit

Permalink
Merge pull request #482 from Flying-Toast/append-cow
Browse files Browse the repository at this point in the history
implement Append for Cow<'a, str> and Cow<'a, [T]>
  • Loading branch information
diwic authored Sep 8, 2024
2 parents 9e0fe15 + 8d66c53 commit c7e336e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions dbus/src/arg/array_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::ffi::{CString};
use std::os::raw::{c_void, c_int};
use std::collections::{HashMap, BTreeMap};
use std::hash::{Hash, BuildHasher};
use std::borrow::Cow;

// Map DBus-Type -> Alignment. Copied from _dbus_marshal_write_fixed_multi in
// http://dbus.freedesktop.org/doc/api/html/dbus-marshal-basic_8c_source.html#l01020
Expand Down Expand Up @@ -53,6 +54,12 @@ impl<'a, T: Arg + Append + Clone> Append for &'a [T] {
}
}

impl<'a, T: Arg + Append + Clone> Append for Cow<'a, [T]> {
fn append_by_ref(&self, i: &mut IterAppend) {
(&*self).append_by_ref(i)
}
}

impl<'a, T: Arg + RefArg> RefArg for &'a [T] {
fn arg_type(&self) -> ArgType { ArgType::Array }
fn signature(&self) -> Signature<'static> { Signature::from(format!("a{}", <T as Arg>::signature())) }
Expand Down
7 changes: 7 additions & 0 deletions dbus/src/arg/basic_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{ptr, any, mem};
use std::ffi::CStr;
use std::os::raw::{c_void, c_char, c_int};
use std::fs::File;
use std::borrow::Cow;


fn arg_append_basic<T>(i: *mut ffi::DBusMessageIter, arg_type: ArgType, v: T) {
Expand Down Expand Up @@ -184,6 +185,12 @@ impl<'a> Get<'a> for &'a str {
.and_then(|s| s.to_str().ok()) }
}

impl<'a> Append for Cow<'a, str> {
fn append_by_ref(&self, i: &mut IterAppend) {
(&*self).append_by_ref(i)
}
}

impl<'a> Arg for String {
const ARG_TYPE: ArgType = ArgType::String;
fn signature() -> Signature<'static> { unsafe { Signature::from_slice_unchecked("s\0") } }
Expand Down

0 comments on commit c7e336e

Please sign in to comment.