@@ -8,7 +8,8 @@ use dbus_tokio::{
8
8
tree:: { AFactory , ATree , ATreeServer } ,
9
9
AConnection ,
10
10
} ;
11
- use futures:: { sync:: oneshot, Async , Future , Poll , Stream } ;
11
+ use futures:: { self , Future , FutureExt } ;
12
+ use futures:: channel:: oneshot;
12
13
use librespot:: {
13
14
connect:: spirc:: Spirc ,
14
15
core:: {
@@ -24,15 +25,15 @@ use rspotify::spotify::{
24
25
util:: datetime_to_timestamp,
25
26
} ;
26
27
use std:: { collections:: HashMap , env, rc:: Rc , thread} ;
27
- use tokio_core:: reactor:: Handle ;
28
+ use std:: pin:: Pin ;
29
+ use futures:: task:: { Context , Poll } ;
28
30
29
31
pub struct DbusServer {
30
32
session : Session ,
31
- handle : Handle ,
32
33
spirc : Rc < Spirc > ,
33
34
api_token : RspotifyToken ,
34
- token_request : Option < Box < dyn Future < Item = LibrespotToken , Error = MercuryError > > > ,
35
- dbus_future : Option < Box < dyn Future < Item = ( ) , Error = ( ) > > > ,
35
+ token_request : Option < Pin < Box < dyn Future < Output = Result < LibrespotToken , MercuryError > > > > > ,
36
+ dbus_future : Option < Pin < Box < dyn Future < Output = ( ) > > > > ,
36
37
device_name : String ,
37
38
}
38
39
@@ -47,13 +48,11 @@ const SCOPE: &str = "user-read-playback-state,user-read-private,\
47
48
impl DbusServer {
48
49
pub fn new (
49
50
session : Session ,
50
- handle : Handle ,
51
51
spirc : Rc < Spirc > ,
52
52
device_name : String ,
53
53
) -> DbusServer {
54
54
DbusServer {
55
55
session,
56
- handle,
57
56
spirc,
58
57
api_token : RspotifyToken :: default ( ) ,
59
58
token_request : None ,
@@ -72,20 +71,18 @@ impl DbusServer {
72
71
}
73
72
74
73
impl Future for DbusServer {
75
- type Error = ( ) ;
76
- type Item = ( ) ;
74
+ type Output = ( ) ;
77
75
78
- fn poll ( & mut self ) -> Poll < ( ) , ( ) > {
76
+ fn poll ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < ( ) > {
79
77
let mut got_new_token = false ;
80
78
if self . is_token_expired ( ) {
81
79
if let Some ( ref mut fut) = self . token_request {
82
- if let Async :: Ready ( token) = fut. poll ( ) . unwrap ( ) {
80
+ if let Poll :: Ready ( Ok ( token) ) = fut. as_mut ( ) . poll ( cx ) {
83
81
self . api_token = RspotifyToken :: default ( )
84
82
. access_token ( & token. access_token )
85
83
. expires_in ( token. expires_in )
86
84
. expires_at ( datetime_to_timestamp ( token. expires_in ) ) ;
87
85
self . dbus_future = Some ( create_dbus_server (
88
- self . handle . clone ( ) ,
89
86
self . api_token . clone ( ) ,
90
87
self . spirc . clone ( ) ,
91
88
self . device_name . clone ( ) ,
@@ -96,17 +93,17 @@ impl Future for DbusServer {
96
93
// This is more meant as a fast hotfix than anything else!
97
94
let client_id =
98
95
env:: var ( "SPOTIFYD_CLIENT_ID" ) . unwrap_or_else ( |_| CLIENT_ID . to_string ( ) ) ;
99
- self . token_request = Some ( get_token ( & self . session , & client_id, SCOPE ) ) ;
96
+ self . token_request = Some ( Box :: pin ( get_token ( & self . session . clone ( ) , & client_id, SCOPE ) ) ) ;
100
97
}
101
98
} else if let Some ( ref mut fut) = self . dbus_future {
102
- return fut. poll ( ) ;
99
+ return fut. as_mut ( ) . poll ( cx ) ;
103
100
}
104
101
105
102
if got_new_token {
106
103
self . token_request = None ;
107
104
}
108
105
109
- Ok ( Async :: NotReady )
106
+ Poll :: Pending
110
107
}
111
108
}
112
109
@@ -115,29 +112,31 @@ fn create_spotify_api(token: &RspotifyToken) -> Spotify {
115
112
}
116
113
117
114
fn create_dbus_server (
118
- handle : Handle ,
119
115
api_token : RspotifyToken ,
120
116
spirc : Rc < Spirc > ,
121
117
device_name : String ,
122
- ) -> Box < dyn Future < Item = ( ) , Error = ( ) > > {
118
+ ) -> Pin < Box < dyn Future < Output = ( ) > > > {
123
119
macro_rules! spotify_api_method {
124
120
( [ $sp: ident, $device: ident $( , $m: ident: $t: ty) * ] $f: expr) => {
125
121
{
126
122
let device_name = utf8_percent_encode( & device_name, NON_ALPHANUMERIC ) . to_string( ) ;
127
123
let token = api_token. clone( ) ;
128
124
move |m| {
129
- let ( p , c ) = oneshot:: channel( ) ;
125
+ let ( tx , rx ) = oneshot:: channel( ) ;
130
126
let token = token. clone( ) ;
131
127
let device_name = device_name. clone( ) ;
132
128
$( let $m: Result <$t, _> = m. msg. read1( ) ; ) *
133
129
thread:: spawn( move || {
134
130
let $sp = create_spotify_api( & token) ;
135
131
let $device = Some ( device_name) ;
136
132
let _ = $f;
137
- let _ = p . send( ( ) ) ;
133
+ let _ = tx . send( ( ) ) ;
138
134
} ) ;
139
135
let mret = m. msg. method_return( ) ;
140
- c. map_err( |e| MethodErr :: failed( & e) ) . map( |_| vec![ mret] )
136
+ rx. map( |val| match val {
137
+ Ok ( _) => Ok ( vec![ mret] ) ,
138
+ Err ( e) => Err ( MethodErr :: failed( & e) ) ,
139
+ } ) . into( )
141
140
}
142
141
}
143
142
}
@@ -638,6 +637,8 @@ fn create_dbus_server(
638
637
tree. set_registered ( & connection, true )
639
638
. expect ( "Failed to register tree" ) ;
640
639
640
+ //let handle = tokio_compat::runtime::Handle::current();
641
+
641
642
let async_connection = AConnection :: new ( connection. clone ( ) , handle)
642
643
. expect ( "Failed to create async dbus connection" ) ;
643
644
@@ -649,7 +650,7 @@ fn create_dbus_server(
649
650
. expect ( "Failed to unwrap async messages" ) ,
650
651
) ;
651
652
652
- Box :: new ( server. for_each ( |message| {
653
+ Box :: pin ( server. for_each ( |message| {
653
654
warn ! ( "Unhandled DBus message: {:?}" , message) ;
654
655
Ok ( ( ) )
655
656
} ) )
0 commit comments