Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Update to futures 0.3 #531

Merged
merged 2 commits into from Nov 11, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 10 additions & 10 deletions .travis.yml
Expand Up @@ -4,21 +4,21 @@ matrix:
include:
- os: linux
rust: nightly
env: GTK=3.14 FEATURES=futures
env: GTK=3.14 FEATURES=
- os: linux
rust: nightly
env: GTK=3.24 FEATURES=v2_48,futures
env: GTK=3.24 FEATURES=v2_48
- os: linux
rust: beta
env: GTK=3.14 FEATURES=
- os: linux
rust: beta
env: GTK=3.24 FEATURES=v2_48
- os: linux
rust: 1.36.0
rust: 1.39.0
env: GTK=3.14 FEATURES=
- os: linux
rust: 1.36.0
rust: 1.39.0
env: GTK=3.24 FEATURES=v2_48
- os: linux
rust: stable
Expand All @@ -28,21 +28,21 @@ matrix:
env: GTK=3.24 FEATURES=v2_48
- os: osx
rust: nightly
env: GTK=3.14 FEATURES=futures
env: GTK=3.14 FEATURES=
# - os: osx
# rust: nightly
# env: GTK=3.24 FEATURES=v2_48,futures
# env: GTK=3.24 FEATURES=v2_48
- os: osx
rust: beta
env: GTK=3.14 FEATURES=
# - os: osx
# rust: beta
# env: GTK=3.24 FEATURES=v2_48
- os: osx
rust: 1.36.0
rust: 1.39.0
env: GTK=3.14 FEATURES=
# - os: osx
# rust: 1.36.0
# rust: 1.39.0
# env: GTK=3.24 FEATURES=v2_48
- os: osx
rust: stable
Expand All @@ -52,10 +52,10 @@ matrix:
# env: GTK=3.24 FEATURES=v2_48
- os: linux
rust: nightly
env: GTK=3.14 FEATURES=futures ARM=1 OTHER_TARGET="--target armv7-unknown-linux-gnueabihf"
env: GTK=3.14 FEATURES= ARM=1 OTHER_TARGET="--target armv7-unknown-linux-gnueabihf"
- os: linux
rust: nightly
env: GTK=3.24 FEATURES=v2_48,futures ARM=1 OTHER_TARGET="--target armv7-unknown-linux-gnueabihf"
env: GTK=3.24 FEATURES=v2_48 ARM=1 OTHER_TARGET="--target armv7-unknown-linux-gnueabihf"
addons:
apt:
packages:
Expand Down
9 changes: 6 additions & 3 deletions Cargo.toml
Expand Up @@ -24,7 +24,11 @@ name = "glib"
lazy_static = "1.2"
libc = "0.2"
bitflags = "1.0"
futures-preview = { version = "0.3.0-alpha", optional = true }
futures-core = "0.3"
futures-task = "0.3"
futures-executor = "0.3"
futures-util = "0.3"
futures-channel = "0.3"
glib-sys = { git = "https://github.com/gtk-rs/sys" }
gobject-sys = { git = "https://github.com/gtk-rs/sys" }

Expand All @@ -41,8 +45,7 @@ v2_54 = ["v2_52", "glib-sys/v2_54", "gobject-sys/v2_54"]
v2_56 = ["v2_54", "glib-sys/v2_56"]
v2_58 = ["v2_56", "glib-sys/v2_58"]
v2_60 = ["v2_58", "glib-sys/v2_60"]
futures = ["futures-preview"]
dox = ["glib-sys/dox", "gobject-sys/dox", "futures"]
dox = ["glib-sys/dox", "gobject-sys/dox"]

[package.metadata.docs.rs]
features = ["dox"]
10 changes: 5 additions & 5 deletions src/lib.rs
Expand Up @@ -88,8 +88,11 @@ pub extern crate glib_sys;
#[doc(hidden)]
pub extern crate gobject_sys;

#[cfg(any(feature = "futures", feature = "dox"))]
pub extern crate futures;
extern crate futures_channel;
extern crate futures_core;
extern crate futures_executor;
extern crate futures_task;
extern crate futures_util;

pub use byte_array::ByteArray;
pub use bytes::Bytes;
Expand Down Expand Up @@ -179,11 +182,8 @@ pub use send_unique::{SendUnique, SendUniqueCell};
#[macro_use]
pub mod subclass;

#[cfg(any(feature = "futures", feature = "dox"))]
mod main_context_futures;
#[cfg(any(feature = "futures", feature = "dox"))]
mod source_futures;
#[cfg(any(feature = "futures", feature = "dox"))]
pub use source_futures::*;

// Actual thread IDs can be reused by the OS once the old thread finished.
Expand Down
39 changes: 14 additions & 25 deletions src/main_context_futures.rs
Expand Up @@ -2,12 +2,10 @@
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

use futures;
use futures::future::{FutureObj, LocalFutureObj};
use futures::prelude::*;
use futures::task::{
Context, LocalSpawn, Poll, RawWaker, RawWakerVTable, Spawn, SpawnError, Waker,
};
use futures_core::future::Future;
use futures_core::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
use futures_task::{FutureObj, LocalFutureObj, LocalSpawn, Spawn, SpawnError};
use futures_util::future::FutureExt;
use get_thread_id;
use glib_sys;
use std::mem;
Expand Down Expand Up @@ -215,7 +213,7 @@ impl TaskSource {
// Clone that we store in the task local data so that
// it can be retrieved as needed
let res = executor.with_thread_default(|| {
let enter = futures::executor::enter().unwrap();
let enter = futures_executor::enter().unwrap();
let mut context = Context::from_waker(&waker);

let res = future.poll_unpin(&mut context);
Expand Down Expand Up @@ -311,7 +309,7 @@ impl MainContext {
let f = f.then(|r| {
res = Some(r);
l_clone.quit();
future::ready(())
futures_util::future::ready(())
});

// Super-unsafe: We transmute here to get rid of the 'static lifetime
Expand All @@ -332,25 +330,15 @@ impl MainContext {
}

impl Spawn for MainContext {
fn spawn_obj(&mut self, f: FutureObj<'static, ()>) -> Result<(), SpawnError> {
(&*self).spawn_obj(f)
}
}

/// Implementing `Spawn` for a _reference_ of `MainContext` allows to convert it to a futures-0.1 `Executor`,
/// using the futures-0.1 compatibility layer.
///
/// See https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.16/src/futures_util/compat/executor.rs.html#85
impl Spawn for &MainContext {
fn spawn_obj(&mut self, f: FutureObj<'static, ()>) -> Result<(), SpawnError> {
fn spawn_obj(&self, f: FutureObj<'static, ()>) -> Result<(), SpawnError> {
let source = TaskSource::new(::PRIORITY_DEFAULT, None, f);
source.attach(Some(&*self));
Ok(())
}
}

impl LocalSpawn for MainContext {
fn spawn_local_obj(&mut self, f: LocalFutureObj<'static, ()>) -> Result<(), SpawnError> {
fn spawn_local_obj(&self, f: LocalFutureObj<'static, ()>) -> Result<(), SpawnError> {
let source = TaskSource::new(::PRIORITY_DEFAULT, Some(get_thread_id()), unsafe {
f.into_future_obj()
});
Expand All @@ -362,7 +350,8 @@ impl LocalSpawn for MainContext {
#[cfg(test)]
mod tests {
use super::*;
use futures::channel::oneshot;
use futures_channel::oneshot;
use futures_util::future::TryFutureExt;
use std::sync::mpsc;
use std::thread;

Expand All @@ -381,9 +370,9 @@ mod tests {
sender.send(()).unwrap();
l_clone.quit();

future::ok(())
futures_util::future::ok(())
})
.then(|res| future::ready(res.unwrap())),
.then(|res| futures_util::future::ready(res.unwrap())),
);

thread::spawn(move || {
Expand All @@ -402,7 +391,7 @@ mod tests {

c.push_thread_default();
let l_clone = l.clone();
c.spawn_local(future::lazy(move |_ctx| {
c.spawn_local(futures_util::future::lazy(move |_ctx| {
l_clone.quit();
}));

Expand All @@ -419,7 +408,7 @@ mod tests {
{
let v = &mut v;

let future = future::lazy(|_ctx| {
let future = futures_util::future::lazy(|_ctx| {
*v = Some(123);
Ok::<i32, ()>(123)
});
Expand Down