Commit 29e74e86 authored by Thomas Mühlbacher's avatar Thomas Mühlbacher
Browse files

zb: Assert auto trait impls for pub items

Helps with making sure that no accidental API breaks happen using the
`assert_impl_all` from the `static_assertions` crate.

These asserts will need to be added to all new public items.

Closes #140.
parent cf489ca9
......@@ -37,6 +37,7 @@ rand = "0.8.2"
sha1 = { version = "0.6.0", features = ["std"] }
slotmap = "1.0"
event-listener = "2.5.1"
static_assertions = "1.1.0"
[dev-dependencies]
zbus_polkit = { path = "../zbus_polkit", version = "1" }
......
use async_io::{block_on, Async};
use async_lock::{Mutex, MutexGuard};
use once_cell::sync::OnceCell;
use static_assertions::assert_impl_all;
use std::{
collections::{hash_map::DefaultHasher, HashMap, VecDeque},
convert::TryInto,
......@@ -317,6 +318,8 @@ impl MessageReceiverThread<Box<dyn Socket>> {
#[derive(Clone, Debug)]
pub struct Connection(Arc<ConnectionInner<Box<dyn Socket>>>);
assert_impl_all!(Connection: Send, Sync, Unpin);
impl Connection {
/// Create and open a D-Bus connection from a `UnixStream`.
///
......@@ -814,6 +817,8 @@ pub struct MessageSink<'s> {
cap_unix_fd: bool,
}
assert_impl_all!(MessageSink<'_>: Send, Sync, Unpin);
impl MessageSink<'_> {
fn flush(&mut self, cx: &mut Context<'_>) -> Poll<Result<()>> {
loop {
......@@ -886,6 +891,8 @@ pub struct MessageStream {
stream: stream::BoxStream<'static, Result<Arc<Message>>>,
}
assert_impl_all!(MessageStream: Send, Unpin);
impl stream::Stream for MessageStream {
type Item = Result<Arc<Message>>;
......
......@@ -6,6 +6,7 @@ use futures_util::{
};
use once_cell::sync::OnceCell;
use slotmap::{new_key_type, SlotMap};
use static_assertions::assert_impl_all;
use std::{
borrow::Cow,
convert::{TryFrom, TryInto},
......@@ -31,6 +32,8 @@ new_key_type! {
pub struct SignalHandlerId;
}
assert_impl_all!(SignalHandlerId: Send, Sync, Unpin);
struct SignalHandlerInfo {
signal_name: &'static str,
handler: SignalHandler,
......@@ -95,6 +98,8 @@ pub struct Proxy<'a> {
pub(crate) inner: Arc<ProxyInner<'a>>,
}
assert_impl_all!(Proxy<'_>: Send, Sync, Unpin);
#[derive(derivative::Derivative)]
#[derivative(Debug)]
pub(crate) struct ProxyInner<'a> {
......@@ -555,6 +560,8 @@ pub struct SignalStream<'s> {
subscription_id: Option<u64>,
}
assert_impl_all!(SignalStream<'_>: Send, Unpin);
impl SignalStream<'_> {
/// Close this stream.
///
......
use futures_util::future::FutureExt;
use static_assertions::assert_impl_all;
use std::{
convert::TryInto,
future::ready,
......@@ -58,6 +59,8 @@ pub struct Connection {
inner: azync::Connection,
}
assert_impl_all!(Connection: Send, Sync, Unpin);
impl AsRawFd for Connection {
fn as_raw_fd(&self) -> RawFd {
block_on(self.inner.as_raw_fd())
......
use static_assertions::assert_impl_all;
use std::{convert::Infallible, error, fmt, io, sync::Arc};
use zvariant::Error as VariantError;
......@@ -41,6 +42,8 @@ pub enum Error {
Infallible,
}
assert_impl_all!(Error: Send, Sync, Unpin);
impl PartialEq for Error {
fn eq(&self, other: &Self) -> bool {
match self {
......
......@@ -6,6 +6,7 @@
use enumflags2::BitFlags;
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use static_assertions::assert_impl_all;
use std::collections::HashMap;
use zvariant::{derive::Type, ObjectPath, OwnedObjectPath, OwnedValue, Value};
......@@ -19,6 +20,9 @@ trait Introspectable {
fn introspect(&self) -> Result<String>;
}
assert_impl_all!(AsyncIntrospectableProxy<'_>: Send, Sync, Unpin);
assert_impl_all!(IntrospectableProxy<'_>: Send, Sync, Unpin);
/// Server-side implementation for the `org.freedesktop.DBus.Introspectable` interface.
/// This interface is implemented automatically for any object registered to the
/// [ObjectServer](crate::ObjectServer).
......@@ -52,11 +56,16 @@ trait Properties {
) -> Result<()>;
}
assert_impl_all!(AsyncPropertiesProxy<'_>: Send, Sync, Unpin);
assert_impl_all!(PropertiesProxy<'_>: Send, Sync, Unpin);
/// Server-side implementation for the `org.freedesktop.DBus.Properties` interface.
/// This interface is implemented automatically for any object registered to the
/// [ObjectServer](crate::ObjectServer).
pub struct Properties;
assert_impl_all!(Properties: Send, Sync, Unpin);
#[dbus_interface(name = "org.freedesktop.DBus.Properties")]
impl Properties {
fn get(&self, interface_name: &str, property_name: &str) -> Result<OwnedValue> {
......@@ -143,6 +152,9 @@ trait ObjectManager {
fn interfaces_removed(&self, object_path: ObjectPath<'_>, interfaces: Vec<&str>) -> Result<()>;
}
assert_impl_all!(AsyncObjectManagerProxy<'_>: Send, Sync, Unpin);
assert_impl_all!(ObjectManagerProxy<'_>: Send, Sync, Unpin);
/// Proxy for the `org.freedesktop.DBus.Peer` interface.
#[dbus_proxy(interface = "org.freedesktop.DBus.Peer")]
trait Peer {
......@@ -158,6 +170,9 @@ trait Peer {
fn get_machine_id(&self) -> Result<String>;
}
assert_impl_all!(AsyncPeerProxy<'_>: Send, Sync, Unpin);
assert_impl_all!(PeerProxy<'_>: Send, Sync, Unpin);
pub(crate) struct Peer;
/// Server-side implementation for the `org.freedesktop.DBus.Peer` interface.
......@@ -196,6 +211,9 @@ trait Monitoring {
fn become_monitor(&self, n1: &[&str], n2: u32) -> Result<()>;
}
assert_impl_all!(AsyncMonitoringProxy<'_>: Send, Sync, Unpin);
assert_impl_all!(MonitoringProxy<'_>: Send, Sync, Unpin);
/// Proxy for the `org.freedesktop.DBus.Stats` interface.
#[dbus_proxy(interface = "org.freedesktop.DBus.Debug.Stats")]
trait Stats {
......@@ -209,6 +227,9 @@ trait Stats {
fn get_all_match_rules(&self) -> Result<Vec<HashMap<String, Vec<String>>>>;
}
assert_impl_all!(AsyncStatsProxy<'_>: Send, Sync, Unpin);
assert_impl_all!(StatsProxy<'_>: Send, Sync, Unpin);
/// The flags used by the bus [`request_name`] method.
///
/// [`request_name`]: struct.DBusProxy.html#method.request_name
......@@ -242,6 +263,8 @@ pub enum RequestNameFlags {
DoNotQueue = 0x04,
}
assert_impl_all!(RequestNameFlags: Send, Sync, Unpin);
/// The return code of the [`request_name`] method.
///
/// [`request_name`]: struct.DBusProxy.html#method.request_name
......@@ -275,6 +298,8 @@ pub enum RequestNameReply {
AlreadyOwner = 0x04,
}
assert_impl_all!(RequestNameReply: Send, Sync, Unpin);
/// The return code of the [`release_name`] method.
///
/// [`release_name`]: struct.DBusProxy.html#method.release_name
......@@ -293,6 +318,8 @@ pub enum ReleaseNameReply {
NotOwner = 0x03,
}
assert_impl_all!(ReleaseNameReply: Send, Sync, Unpin);
/// Proxy for the `org.freedesktop.DBus` interface.
#[dbus_proxy(interface = "org.freedesktop.DBus")]
trait DBus {
......@@ -395,6 +422,9 @@ trait DBus {
fn interfaces(&self) -> Result<Vec<String>>;
}
assert_impl_all!(AsyncDBusProxy<'_>: Send, Sync, Unpin);
assert_impl_all!(DBusProxy<'_>: Send, Sync, Unpin);
/// Errors from <https://gitlab.freedesktop.org/dbus/dbus/-/blob/master/dbus/dbus-protocol.h>
#[derive(Debug, DBusError, PartialEq)]
#[dbus_error(prefix = "org.freedesktop.DBus.Error")]
......@@ -563,6 +593,8 @@ pub enum Error {
NotContainer(String),
}
assert_impl_all!(Error: Send, Sync, Unpin);
/// Alias for a `Result` with the error type [`zbus::fdo::Error`].
///
/// [`zbus::fdo::Error`]: enum.Error.html
......
......@@ -6,6 +6,8 @@ use std::{
time::{SystemTime, UNIX_EPOCH},
};
use static_assertions::assert_impl_all;
/// A D-Bus server GUID.
///
/// See the D-Bus specification [UUIDs chapter] for details.
......@@ -17,6 +19,8 @@ use std::{
#[derive(Clone, Debug, PartialEq, Hash)]
pub struct Guid(String);
assert_impl_all!(Guid: Send, Sync, Unpin);
impl Guid {
/// Generate a D-Bus GUID that can be used with e.g. [`Connection::new_unix_server`].
///
......
......@@ -6,6 +6,7 @@ use std::{
sync::{Arc, RwLock},
};
use static_assertions::assert_impl_all;
use zvariant::{EncodingContext, Error as VariantError, ObjectPath, Signature, Type};
use crate::{
......@@ -51,6 +52,8 @@ pub enum MessageError {
Infallible,
}
assert_impl_all!(MessageError: Send, Sync, Unpin);
impl PartialEq for MessageError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
......@@ -291,6 +294,8 @@ pub struct Message {
fds: Arc<RwLock<Fds>>,
}
assert_impl_all!(Message: Send, Sync, Unpin);
// TODO: Handle non-native byte order: https://gitlab.freedesktop.org/dbus/zbus/-/issues/19
impl Message {
/// Create a message of type [`MessageType::MethodCall`].
......
......@@ -6,6 +6,7 @@ use serde::{
};
use serde_repr::{Deserialize_repr, Serialize_repr};
use static_assertions::assert_impl_all;
use zvariant::{derive::Type, ObjectPath, Signature, Str, Type, Value};
/// The message field code.
......@@ -42,6 +43,8 @@ pub enum MessageFieldCode {
UnixFDs = 9,
}
assert_impl_all!(MessageFieldCode: Send, Sync, Unpin);
impl From<u8> for MessageFieldCode {
fn from(val: u8) -> MessageFieldCode {
match val {
......@@ -112,6 +115,8 @@ pub enum MessageField<'f> {
UnixFDs(u32),
}
assert_impl_all!(MessageField<'_>: Send, Sync, Unpin);
impl<'f> Type for MessageField<'f> {
fn signature() -> Signature<'static> {
Signature::from_str_unchecked("(yv)")
......
use serde::{Deserialize, Serialize};
use static_assertions::assert_impl_all;
use zvariant::derive::Type;
use crate::{MessageField, MessageFieldCode};
......@@ -12,6 +13,8 @@ const MAX_FIELDS_IN_MESSAGE: usize = 16;
#[derive(Debug, Serialize, Deserialize, Type)]
pub struct MessageFields<'m>(#[serde(borrow)] Vec<MessageField<'m>>);
assert_impl_all!(MessageFields<'_>: Send, Sync, Unpin);
impl<'m> MessageFields<'m> {
/// Creates an empty collection of fields.
pub fn new() -> Self {
......
......@@ -5,6 +5,7 @@ use once_cell::sync::OnceCell;
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use static_assertions::assert_impl_all;
use zvariant::{derive::Type, ObjectPath, Signature};
use crate::{MessageError, MessageField, MessageFieldCode, MessageFields};
......@@ -23,6 +24,8 @@ pub enum EndianSig {
Little = b'l',
}
assert_impl_all!(EndianSig: Send, Sync, Unpin);
// Such a shame I've to do this manually
impl TryFrom<u8> for EndianSig {
type Error = MessageError;
......@@ -59,6 +62,8 @@ pub enum MessageType {
Signal = 4,
}
assert_impl_all!(MessageType: Send, Sync, Unpin);
// Such a shame I've to do this manually
impl From<u8> for MessageType {
fn from(val: u8) -> MessageType {
......@@ -93,6 +98,8 @@ pub enum MessageFlags {
AllowInteractiveAuth = 0x4,
}
assert_impl_all!(MessageFlags: Send, Sync, Unpin);
#[derive(Clone, Debug)]
struct SerialNum(OnceCell<u32>);
......@@ -145,6 +152,8 @@ pub struct MessagePrimaryHeader {
serial_num: SerialNum,
}
assert_impl_all!(MessagePrimaryHeader: Send, Sync, Unpin);
impl MessagePrimaryHeader {
/// Create a new `MessagePrimaryHeader` instance.
pub fn new(msg_type: MessageType, body_len: u32) -> Self {
......@@ -244,6 +253,8 @@ pub struct MessageHeader<'m> {
end: ((),), // To ensure header end on 8-byte boundry
}
assert_impl_all!(MessageHeader<'_>: Send, Sync, Unpin);
macro_rules! get_field {
($self:ident, $kind:ident) => {
get_field!($self, $kind, (|v| v))
......
......@@ -9,6 +9,7 @@ use std::{
};
use scoped_tls::scoped_thread_local;
use static_assertions::assert_impl_all;
use zvariant::{ObjectPath, OwnedObjectPath, OwnedValue, Value};
use crate::{
......@@ -267,6 +268,8 @@ pub struct ObjectServer {
root: Node,
}
assert_impl_all!(ObjectServer: Unpin);
impl ObjectServer {
/// Creates a new D-Bus `ObjectServer` for a given connection.
pub fn new(connection: &Connection) -> Self {
......
......@@ -3,6 +3,8 @@ use std::{
os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd},
};
use static_assertions::assert_impl_all;
/// An owned representation of a file descriptor
///
/// When it is dropped, the underlying file descriptor will be dropped.
......@@ -15,6 +17,8 @@ pub struct OwnedFd {
inner: RawFd,
}
assert_impl_all!(OwnedFd: Send, Sync, Unpin);
impl FromRawFd for OwnedFd {
unsafe fn from_raw_fd(fd: RawFd) -> OwnedFd {
OwnedFd { inner: fd }
......
use async_io::block_on;
use static_assertions::assert_impl_all;
use std::{
convert::{TryFrom, TryInto},
future::ready,
......@@ -60,6 +61,8 @@ pub struct Proxy<'a> {
azync: azync::Proxy<'a>,
}
assert_impl_all!(Proxy<'_>: Send, Sync, Unpin);
impl<'a> Proxy<'a> {
/// Create a new `Proxy` for the given destination/path/interface.
pub fn new<E>(
......
use std::{borrow::Cow, convert::TryInto, marker::PhantomData, sync::Arc};
use static_assertions::assert_impl_all;
use zvariant::ObjectPath;
use crate::{azync, Error, Proxy, Result};
......@@ -14,6 +15,8 @@ pub struct ProxyBuilder<'a, T = ()> {
proxy_type: PhantomData<T>,
}
assert_impl_all!(ProxyBuilder<'_>: Send, Sync, Unpin);
impl<'a> ProxyBuilder<'a> {
/// Create a new [`ProxyBuilder`] for the given connection.
pub fn new_bare<C>(conn: &C) -> Self
......
use crate::{Connection, Error, MessageHeader, Proxy, Result};
use static_assertions::assert_impl_all;
use std::{
borrow::Cow,
collections::HashMap,
......@@ -58,6 +59,8 @@ pub struct SignalReceiver<'r, 'p> {
proxies: HashMap<ProxyKey<'static>, &'r Proxy<'p>>,
}
assert_impl_all!(SignalReceiver<'_, '_>: Send, Sync, Unpin);
impl<'r, 'p> SignalReceiver<'r, 'p> {
/// Create a new `SignalReceiver` instance.
pub fn new(conn: Connection) -> Self {
......
......@@ -18,6 +18,7 @@
use serde::{Deserialize, Serialize};
use serde_xml_rs::{from_reader, from_str, to_writer};
use static_assertions::assert_impl_all;
use std::{
io::{Read, Write},
result::Result,
......@@ -43,6 +44,8 @@ pub struct Annotation {
value: String,
}
assert_impl_all!(Annotation: Send, Sync, Unpin);
impl Annotation {
/// Return the annotation name/key.
pub fn name(&self) -> &str {
......@@ -65,6 +68,8 @@ pub struct Arg {
annotations: Vec<Annotation>,
}
assert_impl_all!(Arg: Send, Sync, Unpin);
impl Arg {
/// Return the argument name, if any.
pub fn name(&self) -> Option<&str> {
......@@ -103,6 +108,8 @@ pub struct Method {
elems: Vec<MethodElement>,
}
assert_impl_all!(Method: Send, Sync, Unpin);
impl Method {
/// Return the method name.
pub fn name(&self) -> &str {
......@@ -136,6 +143,8 @@ pub struct Signal {
elems: Vec<SignalElement>,
}
assert_impl_all!(Signal: Send, Sync, Unpin);
impl Signal {
/// Return the signal name.
pub fn name(&self) -> &str {
......@@ -164,6 +173,8 @@ pub struct Property {
annotations: Vec<Annotation>,
}
assert_impl_all!(Property: Send, Sync, Unpin);
impl Property {
/// Returns the property name.
pub fn name(&self) -> &str {
......@@ -204,6 +215,8 @@ pub struct Interface {
elems: Vec<InterfaceElement>,
}
assert_impl_all!(Interface: Send, Sync, Unpin);
impl Interface {
/// Returns the interface name.
pub fn name(&self) -> &str {
......@@ -247,6 +260,8 @@ pub struct Node {
elems: Vec<NodeElement>,
}
assert_impl_all!(Node: Send, Sync, Unpin);
impl Node {
/// Parse the introspection XML document from reader.
pub fn from_reader<R: Read>(reader: R) -> Result<Node, Error> {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment