Skip to content

Commit 529ca56

Browse files
elmarcosdroege
authored andcommitted
glib: add 'futures' feature
This reduces the amount of extra dependencies for projects who don't need or use futures, such as QEMU atm. Signed-off-by: Marc-André Lureau <[email protected]>
1 parent c792e52 commit 529ca56

File tree

5 files changed

+24
-10
lines changed

5 files changed

+24
-10
lines changed

glib/Cargo.toml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ name = "glib"
1919
[dependencies]
2020
libc.workspace = true
2121
bitflags.workspace = true
22-
futures-core = { version = "0.3", default-features = false }
23-
futures-task = { version = "0.3", default-features = false }
24-
futures-executor = "0.3"
25-
futures-channel = "0.3"
26-
futures-util = "0.3"
22+
futures-core = { version = "0.3", default-features = false, optional = true }
23+
futures-task = { version = "0.3", default-features = false, optional = true }
24+
futures-executor = { version = "0.3", optional = true }
25+
futures-channel = { version = "0.3", optional = true }
26+
futures-util = { version = "0.3", optional = true }
2727
glib-sys.workspace = true
2828
gobject-sys.workspace = true
2929
glib-macros = { workspace = true, default-features = false }
@@ -39,7 +39,7 @@ trybuild2 = "1"
3939
criterion = "0.7.0"
4040

4141
[features]
42-
default = ["gio"]
42+
default = ["gio", "futures"]
4343
v2_58 = ["glib-sys/v2_58", "gobject-sys/v2_58"]
4444
v2_60 = ["v2_58", "glib-sys/v2_60"]
4545
v2_62 = ["v2_60", "glib-sys/v2_62", "gobject-sys/v2_62"]
@@ -59,7 +59,8 @@ log = ["rs-log"]
5959
log_kv = ["log", "rs-log/kv"]
6060
log_macros = ["log"]
6161
compiletests = []
62-
gio = ["gio-sys"]
62+
gio = ["gio-sys", "futures"]
63+
futures = ["futures-core", "futures-task", "futures-executor", "futures-channel", "futures-util"]
6364

6465
[[test]]
6566
name = "subclass_compiletest"

glib/src/functions.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ pub fn file_open_tmp(
283283
///
284284
/// This can be called from any thread and will execute the future from the thread
285285
/// where main context is running, e.g. via a `MainLoop`.
286+
#[cfg(feature = "futures")]
286287
pub fn spawn_future<R: Send + 'static, F: std::future::Future<Output = R> + Send + 'static>(
287288
f: F,
288289
) -> crate::JoinHandle<R> {
@@ -298,6 +299,7 @@ pub fn spawn_future<R: Send + 'static, F: std::future::Future<Output = R> + Send
298299
/// This can be called only from the thread where the main context is running, e.g.
299300
/// from any other `Future` that is executed on this main context, or after calling
300301
/// `with_thread_default` or `acquire` on the main context.
302+
#[cfg(feature = "futures")]
301303
pub fn spawn_future_local<R: 'static, F: std::future::Future<Output = R> + 'static>(
302304
f: F,
303305
) -> crate::JoinHandle<R> {

glib/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,18 @@ pub use self::bridged_logging::{
231231
#[macro_use]
232232
pub mod subclass;
233233

234+
#[cfg(feature = "futures")]
234235
mod main_context_futures;
236+
#[cfg(feature = "futures")]
235237
pub use main_context_futures::{JoinError, JoinHandle, SpawnWithinJoinHandle};
238+
#[cfg(feature = "futures")]
236239
mod source_futures;
240+
#[cfg(feature = "futures")]
237241
pub use self::source_futures::*;
238242

243+
#[cfg(feature = "futures")]
239244
mod future_with_timeout;
245+
#[cfg(feature = "futures")]
240246
pub use self::future_with_timeout::*;
241247

242248
mod thread_pool;

glib/src/thread_pool.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3-
use std::{future::Future, panic, ptr};
3+
use std::{panic, ptr};
44

5+
#[cfg(feature = "futures")]
56
use futures_channel::oneshot;
7+
#[cfg(feature = "futures")]
8+
use std::future::Future;
69

710
use crate::{ffi, translate::*};
811

@@ -104,6 +107,7 @@ impl ThreadPool {
104107
}
105108
}
106109

110+
#[cfg(feature = "futures")]
107111
pub fn push_future<T: Send + 'static, F: FnOnce() -> T + Send + 'static>(
108112
&self,
109113
func: F,
@@ -241,6 +245,7 @@ mod tests {
241245
assert_eq!(receiver.recv(), Ok(true));
242246
}
243247

248+
#[cfg(feature = "futures")]
244249
#[test]
245250
fn test_push_future() {
246251
let c = crate::MainContext::new();

glib/tests/clone.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::{
99
thread,
1010
};
1111

12-
use futures_executor::block_on;
1312
use glib::clone;
1413

1514
struct State {
@@ -1053,11 +1052,12 @@ fn test_clone_macro_body() {
10531052
assert_eq!(10, *v.lock().expect("failed to lock"));
10541053
}
10551054

1055+
#[cfg(feature = "futures")]
10561056
#[test]
10571057
fn test_clone_macro_async_kinds() {
10581058
let v = Rc::new(RefCell::new(1));
10591059

1060-
block_on(clone!(
1060+
futures_executor::block_on(clone!(
10611061
#[weak]
10621062
v,
10631063
async move {

0 commit comments

Comments
 (0)