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

Commit

Permalink
Update manual futures code
Browse files Browse the repository at this point in the history
  • Loading branch information
sdroege committed Nov 11, 2019
1 parent 16bccd7 commit b9da1af
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 111 deletions.
22 changes: 11 additions & 11 deletions src/file.rs
Expand Up @@ -8,14 +8,12 @@ use glib::object::IsA;
use glib::translate::*;
use glib_sys;
use gobject_sys;
use std::pin::Pin;
use std::ptr;
use Cancellable;
use File;
use FileCreateFlags;

#[cfg(any(feature = "futures", feature = "dox"))]
use futures::future;

pub trait FileExtManual: Sized {
fn replace_contents_async<
B: AsRef<[u8]> + Send + 'static,
Expand All @@ -31,16 +29,17 @@ pub trait FileExtManual: Sized {
callback: R,
);

#[cfg(any(feature = "futures", feature = "dox"))]
fn replace_contents_async_future<'a, B: AsRef<[u8]> + Send + 'static>(
&self,
contents: B,
etag: Option<&str>,
make_backup: bool,
flags: FileCreateFlags,
) -> Box<
dyn future::Future<Output = Result<(B, glib::GString), (B, glib::Error)>>
+ std::marker::Unpin,
) -> Pin<
Box<
dyn std::future::Future<Output = Result<(B, glib::GString), (B, glib::Error)>>
+ 'static,
>,
>;
}

Expand Down Expand Up @@ -110,16 +109,17 @@ impl<O: IsA<File>> FileExtManual for O {
}
}

#[cfg(any(feature = "futures", feature = "dox"))]
fn replace_contents_async_future<B: AsRef<[u8]> + Send + 'static>(
&self,
contents: B,
etag: Option<&str>,
make_backup: bool,
flags: FileCreateFlags,
) -> Box<
dyn future::Future<Output = Result<(B, glib::GString), (B, glib::Error)>>
+ std::marker::Unpin,
) -> Pin<
Box<
dyn std::future::Future<Output = Result<(B, glib::GString), (B, glib::Error)>>
+ 'static,
>,
> {
use fragile::Fragile;
use GioFuture;
Expand Down
30 changes: 15 additions & 15 deletions src/input_stream.rs
Expand Up @@ -11,13 +11,11 @@ use glib_sys;
use gobject_sys;
use std::io;
use std::mem;
use std::pin::Pin;
use std::ptr;
use Cancellable;
use InputStream;

#[cfg(any(feature = "futures", feature = "dox"))]
use futures::future;

pub trait InputStreamExtManual: Sized {
fn read<B: AsMut<[u8]>, C: IsA<Cancellable>>(
&self,
Expand Down Expand Up @@ -56,23 +54,24 @@ pub trait InputStreamExtManual: Sized {
callback: Q,
);

#[cfg(any(feature = "futures", feature = "dox"))]
#[cfg(any(feature = "v2_44", feature = "dox"))]
fn read_all_async_future<'a, B: AsMut<[u8]> + Send + 'static>(
&self,
buffer: B,
io_priority: Priority,
) -> Box<
dyn future::Future<Output = Result<(B, usize, Option<glib::Error>), (B, glib::Error)>>
+ std::marker::Unpin,
) -> Pin<
Box<
dyn std::future::Future<
Output = Result<(B, usize, Option<glib::Error>), (B, glib::Error)>,
> + 'static,
>,
>;

#[cfg(any(feature = "futures", feature = "dox"))]
fn read_async_future<'a, B: AsMut<[u8]> + Send + 'static>(
&self,
buffer: B,
io_priority: Priority,
) -> Box<dyn future::Future<Output = Result<(B, usize), (B, glib::Error)>> + std::marker::Unpin>;
) -> Pin<Box<dyn std::future::Future<Output = Result<(B, usize), (B, glib::Error)>> + 'static>>;

fn into_read(self) -> InputStreamRead<Self> {
InputStreamRead(self)
Expand Down Expand Up @@ -263,15 +262,17 @@ impl<O: IsA<InputStream>> InputStreamExtManual for O {
}
}

#[cfg(any(feature = "futures", feature = "dox"))]
#[cfg(any(feature = "v2_44", feature = "dox"))]
fn read_all_async_future<'a, B: AsMut<[u8]> + Send + 'static>(
&self,
buffer: B,
io_priority: Priority,
) -> Box<
dyn future::Future<Output = Result<(B, usize, Option<glib::Error>), (B, glib::Error)>>
+ std::marker::Unpin,
) -> Pin<
Box<
dyn std::future::Future<
Output = Result<(B, usize, Option<glib::Error>), (B, glib::Error)>,
> + 'static,
>,
> {
use GioFuture;

Expand All @@ -288,12 +289,11 @@ impl<O: IsA<InputStream>> InputStreamExtManual for O {
})
}

#[cfg(any(feature = "futures", feature = "dox"))]
fn read_async_future<'a, B: AsMut<[u8]> + Send + 'static>(
&self,
buffer: B,
io_priority: Priority,
) -> Box<dyn future::Future<Output = Result<(B, usize), (B, glib::Error)>> + std::marker::Unpin>
) -> Pin<Box<dyn std::future::Future<Output = Result<(B, usize), (B, glib::Error)>> + 'static>>
{
use GioFuture;

Expand Down
3 changes: 1 addition & 2 deletions src/memory_input_stream.rs
Expand Up @@ -51,9 +51,8 @@ mod tests {
}

#[test]
#[cfg(feature = "futures")]
fn read_async_future() {
use futures::prelude::*;
use futures_util::future::TryFutureExt;

let c = glib::MainContext::new();

Expand Down
30 changes: 15 additions & 15 deletions src/output_stream.rs
Expand Up @@ -11,14 +11,12 @@ use glib_sys;
use gobject_sys;
use std::io;
use std::mem;
use std::pin::Pin;
use std::ptr;
use Cancellable;
use OutputStream;
use OutputStreamExt;

#[cfg(any(feature = "futures", feature = "dox"))]
use futures::future;

pub trait OutputStreamExtManual: Sized + OutputStreamExt {
fn write_async<
B: AsRef<[u8]> + Send + 'static,
Expand Down Expand Up @@ -51,22 +49,23 @@ pub trait OutputStreamExtManual: Sized + OutputStreamExt {
callback: Q,
);

#[cfg(any(feature = "futures", feature = "dox"))]
fn write_async_future<'a, B: AsRef<[u8]> + Send + 'static>(
&self,
buffer: B,
io_priority: Priority,
) -> Box<dyn future::Future<Output = Result<(B, usize), (B, glib::Error)>> + std::marker::Unpin>;
) -> Pin<Box<dyn std::future::Future<Output = Result<(B, usize), (B, glib::Error)>> + 'static>>;

#[cfg(any(feature = "futures", feature = "dox"))]
#[cfg(any(feature = "v2_44", feature = "dox"))]
fn write_all_async_future<'a, B: AsRef<[u8]> + Send + 'static>(
&self,
buffer: B,
io_priority: Priority,
) -> Box<
dyn future::Future<Output = Result<(B, usize, Option<glib::Error>), (B, glib::Error)>>
+ std::marker::Unpin,
) -> Pin<
Box<
dyn std::future::Future<
Output = Result<(B, usize, Option<glib::Error>), (B, glib::Error)>,
> + 'static,
>,
>;

fn into_write(self) -> OutputStreamWrite<Self> {
Expand Down Expand Up @@ -225,12 +224,11 @@ impl<O: IsA<OutputStream>> OutputStreamExtManual for O {
}
}

#[cfg(any(feature = "futures", feature = "dox"))]
fn write_async_future<'a, B: AsRef<[u8]> + Send + 'static>(
&self,
buffer: B,
io_priority: Priority,
) -> Box<dyn future::Future<Output = Result<(B, usize), (B, glib::Error)>> + std::marker::Unpin>
) -> Pin<Box<dyn std::future::Future<Output = Result<(B, usize), (B, glib::Error)>> + 'static>>
{
use GioFuture;

Expand All @@ -247,15 +245,17 @@ impl<O: IsA<OutputStream>> OutputStreamExtManual for O {
})
}

#[cfg(any(feature = "futures", feature = "dox"))]
#[cfg(any(feature = "v2_44", feature = "dox"))]
fn write_all_async_future<'a, B: AsRef<[u8]> + Send + 'static>(
&self,
buffer: B,
io_priority: Priority,
) -> Box<
dyn future::Future<Output = Result<(B, usize, Option<glib::Error>), (B, glib::Error)>>
+ std::marker::Unpin,
) -> Pin<
Box<
dyn std::future::Future<
Output = Result<(B, usize, Option<glib::Error>), (B, glib::Error)>,
> + 'static,
>,
> {
use GioFuture;

Expand Down
22 changes: 8 additions & 14 deletions src/pollable_input_stream.rs
Expand Up @@ -14,10 +14,8 @@ use std::ptr;
use Cancellable;
use PollableInputStream;

#[cfg(any(feature = "futures", feature = "dox"))]
use futures::future::Future;
#[cfg(any(feature = "futures", feature = "dox"))]
use futures::stream::Stream;
use futures_core::stream::Stream;
use std::pin::Pin;

pub trait PollableInputStreamExtManual: Sized {
fn create_source<F, C>(
Expand All @@ -31,19 +29,17 @@ pub trait PollableInputStreamExtManual: Sized {
F: FnMut(&Self) -> glib::Continue + 'static,
C: IsA<Cancellable>;

#[cfg(any(feature = "futures", feature = "dox"))]
fn create_source_future<C: IsA<Cancellable>>(
&self,
cancellable: Option<&C>,
priority: glib::Priority,
) -> Box<dyn Future<Output = ()> + std::marker::Unpin>;
) -> Pin<Box<dyn std::future::Future<Output = ()> + 'static>>;

#[cfg(any(feature = "futures", feature = "dox"))]
fn create_source_stream<C: IsA<Cancellable>>(
&self,
cancellable: Option<&C>,
priority: glib::Priority,
) -> Box<dyn Stream<Item = ()> + std::marker::Unpin>;
) -> Pin<Box<dyn Stream<Item = ()> + 'static>>;

fn read_nonblocking<C: IsA<Cancellable>>(
&self,
Expand Down Expand Up @@ -130,16 +126,15 @@ impl<O: IsA<PollableInputStream>> PollableInputStreamExtManual for O {
}
}

#[cfg(any(feature = "futures", feature = "dox"))]
fn create_source_future<C: IsA<Cancellable>>(
&self,
cancellable: Option<&C>,
priority: glib::Priority,
) -> Box<dyn Future<Output = ()> + std::marker::Unpin> {
) -> Pin<Box<dyn std::future::Future<Output = ()> + 'static>> {
let cancellable: Option<Cancellable> = cancellable.map(|c| c.as_ref()).cloned();

let obj = Fragile::new(self.clone());
Box::new(glib::SourceFuture::new(move |send| {
Box::pin(glib::SourceFuture::new(move |send| {
let mut send = Some(send);
obj.get()
.create_source(cancellable.as_ref(), None, priority, move |_| {
Expand All @@ -149,16 +144,15 @@ impl<O: IsA<PollableInputStream>> PollableInputStreamExtManual for O {
}))
}

#[cfg(any(feature = "futures", feature = "dox"))]
fn create_source_stream<C: IsA<Cancellable>>(
&self,
cancellable: Option<&C>,
priority: glib::Priority,
) -> Box<dyn Stream<Item = ()> + std::marker::Unpin> {
) -> Pin<Box<dyn Stream<Item = ()> + 'static>> {
let cancellable: Option<Cancellable> = cancellable.map(|c| c.as_ref()).cloned();

let obj = Fragile::new(self.clone());
Box::new(glib::SourceStream::new(move |send| {
Box::pin(glib::SourceStream::new(move |send| {
obj.get()
.create_source(cancellable.as_ref(), None, priority, move |_| {
if send.unbounded_send(()).is_err() {
Expand Down
22 changes: 8 additions & 14 deletions src/pollable_output_stream.rs
Expand Up @@ -10,13 +10,11 @@ use glib::translate::*;
use glib_sys;
use std::cell::RefCell;
use std::mem::transmute;
use std::pin::Pin;
use Cancellable;
use PollableOutputStream;

#[cfg(feature = "futures")]
use futures::future::Future;
#[cfg(feature = "futures")]
use futures::stream::Stream;
use futures_core::stream::Stream;

pub trait PollableOutputStreamExtManual {
fn create_source<F, C>(
Expand All @@ -30,19 +28,17 @@ pub trait PollableOutputStreamExtManual {
F: FnMut(&Self) -> glib::Continue + 'static,
C: IsA<Cancellable>;

#[cfg(feature = "futures")]
fn create_source_future<C: IsA<Cancellable>>(
&self,
cancellable: Option<&C>,
priority: glib::Priority,
) -> Box<dyn Future<Output = ()> + std::marker::Unpin>;
) -> Pin<Box<dyn std::future::Future<Output = ()> + 'static>>;

#[cfg(feature = "futures")]
fn create_source_stream<C: IsA<Cancellable>>(
&self,
cancellable: Option<&C>,
priority: glib::Priority,
) -> Box<dyn Stream<Item = ()> + std::marker::Unpin>;
) -> Pin<Box<dyn Stream<Item = ()> + 'static>>;
}

impl<O: IsA<PollableOutputStream>> PollableOutputStreamExtManual for O {
Expand Down Expand Up @@ -98,16 +94,15 @@ impl<O: IsA<PollableOutputStream>> PollableOutputStreamExtManual for O {
}
}

#[cfg(feature = "futures")]
fn create_source_future<C: IsA<Cancellable>>(
&self,
cancellable: Option<&C>,
priority: glib::Priority,
) -> Box<dyn Future<Output = ()> + std::marker::Unpin> {
) -> Pin<Box<dyn std::future::Future<Output = ()> + 'static>> {
let cancellable: Option<Cancellable> = cancellable.map(|c| c.as_ref()).cloned();

let obj = Fragile::new(self.clone());
Box::new(glib::SourceFuture::new(move |send| {
Box::pin(glib::SourceFuture::new(move |send| {
let mut send = Some(send);
obj.get()
.create_source(cancellable.as_ref(), None, priority, move |_| {
Expand All @@ -117,16 +112,15 @@ impl<O: IsA<PollableOutputStream>> PollableOutputStreamExtManual for O {
}))
}

#[cfg(feature = "futures")]
fn create_source_stream<C: IsA<Cancellable>>(
&self,
cancellable: Option<&C>,
priority: glib::Priority,
) -> Box<dyn Stream<Item = ()> + std::marker::Unpin> {
) -> Pin<Box<dyn Stream<Item = ()> + 'static>> {
let cancellable: Option<Cancellable> = cancellable.map(|c| c.as_ref()).cloned();

let obj = Fragile::new(self.clone());
Box::new(glib::SourceStream::new(move |send| {
Box::pin(glib::SourceStream::new(move |send| {
let send = Some(send);
obj.get()
.create_source(cancellable.as_ref(), None, priority, move |_| {
Expand Down

0 comments on commit b9da1af

Please sign in to comment.