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

feat!: drop async_trait usage #7

Merged
merged 4 commits into from
Mar 19, 2024
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ edition = "2021"

[dependencies]
async-channel = "2.2"
async-trait = "0.1"
futures-channel = "0.3"
serde = "1.0"
trait-variant = "0.1.2"
zbus = "4.0"

[dev-dependencies]
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@ If you want to have more control, it is recommended to manually create your own
use std::future;

use mpris_server::{
async_trait,
zbus::{fdo, Result},
Metadata, PlayerInterface, Property, RootInterface, Server, Signal, Time, Volume,
};

pub struct MyPlayer;

#[async_trait]
impl RootInterface for MyPlayer {
async fn identity(&self) -> fdo::Result<String> {
Ok("MyPlayer".into())
Expand All @@ -54,7 +52,6 @@ impl RootInterface for MyPlayer {
// Other methods...
}

#[async_trait]
impl PlayerInterface for MyPlayer {
async fn set_volume(&self, volume: Volume) -> Result<()> {
self.volume.set(volume);
Expand Down
5 changes: 0 additions & 5 deletions examples/local_server.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::future;

use mpris_server::{
async_trait,
zbus::{fdo, Result},
LocalPlayerInterface, LocalPlaylistsInterface, LocalRootInterface, LocalServer,
LocalTrackListInterface, LoopStatus, Metadata, PlaybackRate, PlaybackStatus, Playlist,
Expand All @@ -10,7 +9,6 @@ use mpris_server::{

pub struct Player;

#[async_trait(?Send)]
impl LocalRootInterface for Player {
async fn raise(&self) -> fdo::Result<()> {
println!("Raise");
Expand Down Expand Up @@ -73,7 +71,6 @@ impl LocalRootInterface for Player {
}
}

#[async_trait(?Send)]
impl LocalPlayerInterface for Player {
async fn next(&self) -> fdo::Result<()> {
println!("Next");
Expand Down Expand Up @@ -216,7 +213,6 @@ impl LocalPlayerInterface for Player {
}
}

#[async_trait(?Send)]
impl LocalTrackListInterface for Player {
async fn get_tracks_metadata(&self, track_ids: Vec<TrackId>) -> fdo::Result<Vec<Metadata>> {
println!("GetTracksMetadata({:?})", track_ids);
Expand Down Expand Up @@ -254,7 +250,6 @@ impl LocalTrackListInterface for Player {
}
}

#[async_trait(?Send)]
impl LocalPlaylistsInterface for Player {
async fn activate_playlist(&self, playlist_id: PlaylistId) -> fdo::Result<()> {
println!("ActivatePlaylist({})", playlist_id);
Expand Down
5 changes: 0 additions & 5 deletions examples/server.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::future;

use mpris_server::{
async_trait,
zbus::{fdo, Result},
LoopStatus, Metadata, PlaybackRate, PlaybackStatus, PlayerInterface, Playlist, PlaylistId,
PlaylistOrdering, PlaylistsInterface, Property, RootInterface, Server, Signal, Time, TrackId,
Expand All @@ -10,7 +9,6 @@ use mpris_server::{

pub struct Player;

#[async_trait]
impl RootInterface for Player {
async fn raise(&self) -> fdo::Result<()> {
println!("Raise");
Expand Down Expand Up @@ -73,7 +71,6 @@ impl RootInterface for Player {
}
}

#[async_trait]
impl PlayerInterface for Player {
async fn next(&self) -> fdo::Result<()> {
println!("Next");
Expand Down Expand Up @@ -216,7 +213,6 @@ impl PlayerInterface for Player {
}
}

#[async_trait]
impl TrackListInterface for Player {
async fn get_tracks_metadata(&self, track_ids: Vec<TrackId>) -> fdo::Result<Vec<Metadata>> {
println!("GetTracksMetadata({:?})", track_ids);
Expand Down Expand Up @@ -254,7 +250,6 @@ impl TrackListInterface for Player {
}
}

#[async_trait]
impl PlaylistsInterface for Player {
async fn activate_playlist(&self, playlist_id: PlaylistId) -> fdo::Result<()> {
println!("ActivatePlaylist({})", playlist_id);
Expand Down
49 changes: 10 additions & 39 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,6 @@ pub mod builder {
pub use crate::{metadata::MetadataBuilder, player::PlayerBuilder};
}

/// Retrofits support for `async fn` in trait impls and declarations.
///
/// Any trait declaration or trait `impl` decorated with `#[async_trait]` or
/// `#[async_trait(?Send)]` is retrofitted with support for `async fn`s:
///
/// # Examples
///
/// ```ignore
/// use mpris_server::{async_trait, LocalRootInterface, RootInterface};
/// use zbus::fdo;
///
/// struct MyPlayer;
///
/// #[async_trait]
/// impl RootInterface for MyPlayer {
/// async fn identity(&self) -> fdo::Result<String> {
/// Ok("MyPlayer".into())
/// }
///
/// // Other methods...
/// }
///
/// struct MyLocalPlayer;
///
/// #[async_trait(?Send)]
/// impl LocalRootInterface for MyLocalPlayer {
/// async fn identity(&self) -> fdo::Result<String> {
/// Ok("MyLocalPlayer".into())
/// }
///
/// // Other methods...
/// }
/// ```
pub use async_trait::async_trait;
pub use zbus;
use zbus::{fdo, zvariant::OwnedObjectPath, Result};

Expand All @@ -87,7 +53,7 @@ pub use crate::{

macro_rules! define_iface {
(#[$attr:meta],
$root_iface_ident:ident$(: $bound:tt $(+ $other_bounds:tt)* )? extra_docs $extra_root_docs:literal,
$root_iface_ident:ident extra_docs $extra_root_docs:literal,
$player_iface_ident:ident extra_docs $extra_player_docs:literal,
$track_list_iface_ident:ident extra_docs $extra_track_list_docs:literal,
$playlists_iface_ident:ident extra_docs $extra_playlists_docs:literal) => {
Expand All @@ -97,7 +63,7 @@ macro_rules! define_iface {
///
/// [org.mpris.MediaPlayer2]: https://specifications.freedesktop.org/mpris-spec/2.2/Media_Player.html
#[$attr]
pub trait $root_iface_ident$(: $bound $(+ $other_bounds)* )? {
pub trait $root_iface_ident {
/// Brings the media player's user interface to the front using any
/// appropriate mechanism available.
///
Expand Down Expand Up @@ -1167,15 +1133,15 @@ macro_rules! define_iface {
}

define_iface!(
#[async_trait],
RootInterface: Send + Sync extra_docs "",
#[trait_variant::make(Send + Sync)],
RootInterface extra_docs "",
PlayerInterface extra_docs "",
TrackListInterface extra_docs "",
PlaylistsInterface extra_docs ""
);

define_iface!(
#[async_trait(?Send)],
#[allow(async_fn_in_trait)],
LocalRootInterface extra_docs "Local version of [`RootInterface`] to be used with [`LocalServer`].",
LocalPlayerInterface extra_docs "Local version of [`PlayerInterface`] to be used with [`LocalServer`].",
LocalTrackListInterface extra_docs "Local version of [`TrackListInterface`] to be used with [`LocalServer`].",
Expand Down Expand Up @@ -1226,6 +1192,11 @@ mod tests {

use super::*;

assert_trait_sub_all!(RootInterface: Send, Sync);
assert_trait_sub_all!(PlayerInterface: Send, Sync);
assert_trait_sub_all!(TrackListInterface: Send, Sync);
assert_trait_sub_all!(PlaylistsInterface: Send, Sync);

assert_trait_sub_all!(PlayerInterface: RootInterface);
assert_trait_sub_all!(TrackListInterface: PlayerInterface, RootInterface);
assert_trait_sub_all!(PlaylistsInterface: PlayerInterface, RootInterface);
Expand Down
5 changes: 0 additions & 5 deletions src/local_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use std::{
};

use async_channel::{Receiver, Sender};
use async_trait::async_trait;
use futures_channel::oneshot;
use zbus::{fdo, Connection, Result};

Expand Down Expand Up @@ -137,7 +136,6 @@ impl<T> InnerImp<T> {
}
}

#[async_trait]
impl<T> RootInterface for InnerImp<T> {
async fn raise(&self) -> fdo::Result<()> {
let (tx, rx) = oneshot::channel();
Expand Down Expand Up @@ -213,7 +211,6 @@ impl<T> RootInterface for InnerImp<T> {
}
}

#[async_trait]
impl<T> PlayerInterface for InnerImp<T> {
async fn next(&self) -> fdo::Result<()> {
let (tx, rx) = oneshot::channel();
Expand Down Expand Up @@ -387,7 +384,6 @@ impl<T> PlayerInterface for InnerImp<T> {
}
}

#[async_trait]
impl<T> TrackListInterface for InnerImp<T>
where
T: LocalTrackListInterface,
Expand Down Expand Up @@ -444,7 +440,6 @@ where
}
}

#[async_trait]
impl<T> PlaylistsInterface for InnerImp<T>
where
T: LocalPlaylistsInterface,
Expand Down
3 changes: 0 additions & 3 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::{
rc::{Rc, Weak},
};

use async_trait::async_trait;
use zbus::{fdo, Result};

use crate::{
Expand Down Expand Up @@ -87,7 +86,6 @@ impl State {
}
}

#[async_trait(?Send)]
impl LocalRootInterface for State {
async fn raise(&self) -> fdo::Result<()> {
let player = self.player();
Expand Down Expand Up @@ -150,7 +148,6 @@ impl LocalRootInterface for State {
}
}

#[async_trait(?Send)]
impl LocalPlayerInterface for State {
async fn next(&self) -> fdo::Result<()> {
let player = self.player();
Expand Down
Loading