Skip to content

Commit

Permalink
💥 zb,zm: Drop Arc wrapping of Message everywhere
Browse files Browse the repository at this point in the history
Message is now cheaply cloneable so this is not needed anymore.
  • Loading branch information
zeenix committed Oct 11, 2023
1 parent 262d5f8 commit a59cedb
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 71 deletions.
4 changes: 2 additions & 2 deletions zbus/src/blocking/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use enumflags2::BitFlags;
use event_listener::EventListener;
use static_assertions::assert_impl_all;
use std::{io, ops::Deref, sync::Arc};
use std::{io, ops::Deref};
use zbus_names::{BusName, ErrorName, InterfaceName, MemberName, OwnedUniqueName, WellKnownName};
use zvariant::ObjectPath;

Expand Down Expand Up @@ -84,7 +84,7 @@ impl Connection {
iface: Option<I>,
method_name: M,
body: &B,
) -> Result<Arc<Message>>
) -> Result<Message>
where
D: TryInto<BusName<'d>>,
P: TryInto<ObjectPath<'p>>,
Expand Down
3 changes: 1 addition & 2 deletions zbus/src/blocking/message_iterator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use futures_util::StreamExt;
use static_assertions::assert_impl_all;
use std::sync::Arc;

use crate::{
blocking::Connection, message::Message, utils::block_on, MatchRule, OwnedMatchRule, Result,
Expand Down Expand Up @@ -112,7 +111,7 @@ impl MessageIterator {
}

impl Iterator for MessageIterator {
type Item = Result<Arc<Message>>;
type Item = Result<Message>;

fn next(&mut self) -> Option<Self::Item> {
block_on(self.azync.as_mut().expect("Inner stream is `None`").next())
Expand Down
6 changes: 3 additions & 3 deletions zbus/src/blocking/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use enumflags2::BitFlags;
use futures_util::StreamExt;
use static_assertions::assert_impl_all;
use std::{ops::Deref, sync::Arc};
use std::ops::Deref;
use zbus_names::{BusName, InterfaceName, MemberName, UniqueName};
use zvariant::{ObjectPath, OwnedValue, Value};

Expand Down Expand Up @@ -207,7 +207,7 @@ impl<'a> Proxy<'a> {
/// allocation/copying, by deserializing the reply to an unowned type).
///
/// [`call`]: struct.Proxy.html#method.call
pub fn call_method<'m, M, B>(&self, method_name: M, body: &B) -> Result<Arc<Message>>
pub fn call_method<'m, M, B>(&self, method_name: M, body: &B) -> Result<Message>
where
M: TryInto<MemberName<'m>>,
M::Error: Into<Error>,
Expand Down Expand Up @@ -393,7 +393,7 @@ impl<'a> SignalIterator<'a> {
assert_impl_all!(SignalIterator<'_>: Send, Sync, Unpin);

impl std::iter::Iterator for SignalIterator<'_> {
type Item = Arc<Message>;
type Item = Message;

fn next(&mut self) -> Option<Self::Item> {
block_on(self.0.as_mut().expect("`SignalStream` is `None`").next())
Expand Down
16 changes: 8 additions & 8 deletions zbus/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ pub(crate) struct ConnectionInner {
#[allow(unused)]
socket_reader_task: OnceCell<Task<()>>,

pub(crate) msg_receiver: InactiveReceiver<Result<Arc<Message>>>,
pub(crate) method_return_receiver: InactiveReceiver<Result<Arc<Message>>>,
pub(crate) msg_receiver: InactiveReceiver<Result<Message>>,
pub(crate) method_return_receiver: InactiveReceiver<Result<Message>>,
msg_senders: Arc<Mutex<HashMap<Option<OwnedMatchRule>, MsgBroadcaster>>>,

subscriptions: Mutex<Subscriptions>,
Expand All @@ -76,9 +76,9 @@ pub(crate) struct ConnectionInner {
object_server_dispatch_task: OnceCell<Task<()>>,
}

type Subscriptions = HashMap<OwnedMatchRule, (u64, InactiveReceiver<Result<Arc<Message>>>)>;
type Subscriptions = HashMap<OwnedMatchRule, (u64, InactiveReceiver<Result<Message>>)>;

pub(crate) type MsgBroadcaster = Broadcaster<Result<Arc<Message>>>;
pub(crate) type MsgBroadcaster = Broadcaster<Result<Message>>;

/// A D-Bus connection.
///
Expand Down Expand Up @@ -206,7 +206,7 @@ pub(crate) struct PendingMethodCall {
}

impl Future for PendingMethodCall {
type Output = Result<Arc<Message>>;
type Output = Result<Message>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.poll_before(cx, None).map(|ret| {
Expand All @@ -220,7 +220,7 @@ impl Future for PendingMethodCall {
}

impl OrderedFuture for PendingMethodCall {
type Output = Result<Arc<Message>>;
type Output = Result<Message>;
type Ordering = zbus::message::Sequence;

fn poll_before(
Expand Down Expand Up @@ -311,7 +311,7 @@ impl Connection {
interface: Option<I>,
method_name: M,
body: &B,
) -> Result<Arc<Message>>
) -> Result<Message>
where
D: TryInto<BusName<'d>>,
P: TryInto<ObjectPath<'p>>,
Expand Down Expand Up @@ -1028,7 +1028,7 @@ impl Connection {
&self,
rule: OwnedMatchRule,
max_queued: Option<usize>,
) -> Result<Receiver<Result<Arc<Message>>>> {
) -> Result<Receiver<Result<Message>>> {
use std::collections::hash_map::Entry;

if self.inner.msg_senders.lock().await.is_empty() {
Expand Down
3 changes: 1 addition & 2 deletions zbus/src/connection/socket_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl SocketReader {
}

#[instrument]
async fn read_socket(&mut self) -> crate::Result<Arc<Message>> {
async fn read_socket(&mut self) -> crate::Result<Message> {
self.activity_event.notify(usize::MAX);
let mut bytes = self
.already_received_bytes
Expand Down Expand Up @@ -185,6 +185,5 @@ impl SocketReader {
fds,
seq,
)
.map(Arc::new)
}
}
8 changes: 1 addition & 7 deletions zbus/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub enum Error {
/// A D-Bus method error reply.
// According to the spec, there can be all kinds of details in D-Bus errors but nobody adds
// anything more than a string description.
MethodError(OwnedErrorName, Option<String>, Arc<Message>),
MethodError(OwnedErrorName, Option<String>, Message),
/// A required field is missing in the message headers.
MissingField,
/// Invalid D-Bus GUID.
Expand Down Expand Up @@ -226,12 +226,6 @@ impl From<Infallible> for Error {
// For messages that are D-Bus error returns
impl From<Message> for Error {
fn from(message: Message) -> Error {
Self::from(Arc::new(message))
}
}

impl From<Arc<Message>> for Error {
fn from(message: Arc<Message>) -> Error {
// FIXME: Instead of checking this, we should have Method as trait and specific types for
// each message type.
let header = message.header();
Expand Down
8 changes: 4 additions & 4 deletions zbus/src/message_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl MessageStream {
}

pub(crate) fn for_subscription_channel(
msg_receiver: ActiveReceiver<Result<Arc<Message>>>,
msg_receiver: ActiveReceiver<Result<Message>>,
rule: Option<OwnedMatchRule>,
conn: &Connection,
) -> Self {
Expand All @@ -182,7 +182,7 @@ impl MessageStream {
}

impl stream::Stream for MessageStream {
type Item = Result<Arc<Message>>;
type Item = Result<Message>;

fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.get_mut();
Expand All @@ -192,7 +192,7 @@ impl stream::Stream for MessageStream {
}

impl OrderedStream for MessageStream {
type Data = Result<Arc<Message>>;
type Data = Result<Message>;
type Ordering = Sequence;

fn poll_next_before(
Expand Down Expand Up @@ -274,7 +274,7 @@ impl From<&MessageStream> for Connection {
#[derive(Clone, Debug)]
struct Inner {
conn_inner: Arc<ConnectionInner>,
msg_receiver: ActiveReceiver<Result<Arc<Message>>>,
msg_receiver: ActiveReceiver<Result<Message>>,
match_rule: Option<OwnedMatchRule>,
}

Expand Down
8 changes: 4 additions & 4 deletions zbus/src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ impl<'a> Proxy<'a> {
/// allocation/copying, by deserializing the reply to an unowned type).
///
/// [`call`]: struct.Proxy.html#method.call
pub async fn call_method<'m, M, B>(&self, method_name: M, body: &B) -> Result<Arc<Message>>
pub async fn call_method<'m, M, B>(&self, method_name: M, body: &B) -> Result<Message>
where
M: TryInto<MemberName<'m>>,
M::Error: Into<Error>,
Expand Down Expand Up @@ -1231,7 +1231,7 @@ impl<'a> SignalStream<'a> {
})
}

fn filter(&mut self, msg: &Arc<Message>) -> Result<bool> {
fn filter(&mut self, msg: &Message) -> Result<bool> {
let header = msg.header();
let sender = header.sender();
if sender == self.src_unique_name.as_ref() {
Expand All @@ -1251,15 +1251,15 @@ impl<'a> SignalStream<'a> {
assert_impl_all!(SignalStream<'_>: Send, Sync, Unpin);

impl<'a> stream::Stream for SignalStream<'a> {
type Item = Arc<Message>;
type Item = Message;

fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
OrderedStream::poll_next_before(self, cx, None).map(|res| res.into_data())
}
}

impl<'a> OrderedStream for SignalStream<'a> {
type Data = Arc<Message>;
type Data = Message;
type Ordering = Sequence;

fn poll_next_before(
Expand Down
10 changes: 2 additions & 8 deletions zbus_macros/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ fn gen_proxy_signal(
quote! {
#[doc = #args_struct_gen_doc]
#[derive(Debug, Clone)]
pub struct #signal_name_ident(::std::sync::Arc<#zbus::message::Message>);
pub struct #signal_name_ident(#zbus::message::Message);

impl ::std::ops::Deref for #signal_name_ident {
type Target = #zbus::message::Message;
Expand All @@ -943,12 +943,6 @@ fn gen_proxy_signal(
}
}

impl ::std::convert::AsRef<::std::sync::Arc<#zbus::message::Message>> for #signal_name_ident {
fn as_ref(&self) -> &::std::sync::Arc<#zbus::message::Message> {
&self.0
}
}

impl ::std::convert::AsRef<#zbus::message::Message> for #signal_name_ident {
fn as_ref(&self) -> &#zbus::message::Message {
&self.0
Expand All @@ -961,7 +955,7 @@ fn gen_proxy_signal(
#[doc = " from a [::zbus::message::Message]."]
pub fn from_message<M>(msg: M) -> ::std::option::Option<Self>
where
M: ::std::convert::Into<::std::sync::Arc<#zbus::message::Message>>,
M: ::std::convert::Into<#zbus::message::Message>,
{
let msg = msg.into();
let hdr = msg.header();
Expand Down
55 changes: 24 additions & 31 deletions zbus_macros/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ fn test_interface() {

mod signal_from_message {
use super::*;
use std::sync::Arc;
use zbus::message::Message;

#[dbus_proxy(
Expand All @@ -275,16 +274,14 @@ mod signal_from_message {

#[test]
fn signal_u8() {
let message = Arc::new(
Message::signal(
"/org/freedesktop/zbus_macros/test",
"org.freedesktop.zbus_macros.Test",
"SignalU8",
)
.expect("Failed to create signal message builder")
.build(&(1u8,))
.expect("Failed to build signal message"),
);
let message = Message::signal(
"/org/freedesktop/zbus_macros/test",
"org.freedesktop.zbus_macros.Test",
"SignalU8",
)
.expect("Failed to create signal message builder")
.build(&(1u8,))
.expect("Failed to build signal message");

assert!(
SignalU8::from_message(message.clone()).is_some(),
Expand All @@ -298,16 +295,14 @@ mod signal_from_message {

#[test]
fn signal_string() {
let message = Arc::new(
Message::signal(
"/org/freedesktop/zbus_macros/test",
"org.freedesktop.zbus_macros.Test",
"SignalString",
)
.expect("Failed to create signal message builder")
.build(&(String::from("test"),))
.expect("Failed to build signal message"),
);
let message = Message::signal(
"/org/freedesktop/zbus_macros/test",
"org.freedesktop.zbus_macros.Test",
"SignalString",
)
.expect("Failed to create signal message builder")
.build(&(String::from("test"),))
.expect("Failed to build signal message");

assert!(
SignalString::from_message(message.clone()).is_some(),
Expand All @@ -321,16 +316,14 @@ mod signal_from_message {

#[test]
fn wrong_data() {
let message = Arc::new(
Message::signal(
"/org/freedesktop/zbus_macros/test",
"org.freedesktop.zbus_macros.Test",
"SignalU8",
)
.expect("Failed to create signal message builder")
.build(&(String::from("test"),))
.expect("Failed to build signal message"),
);
let message = Message::signal(
"/org/freedesktop/zbus_macros/test",
"org.freedesktop.zbus_macros.Test",
"SignalU8",
)
.expect("Failed to create signal message builder")
.build(&(String::from("test"),))
.expect("Failed to build signal message");

let signal = SignalU8::from_message(message).expect("Message is a SignalU8");
signal
Expand Down

0 comments on commit a59cedb

Please sign in to comment.