From b9da1af43ac0096e041c1fe7fd4b549f2955ad88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Mon, 11 Nov 2019 10:33:23 +0100 Subject: [PATCH] Update manual futures code --- src/file.rs | 22 +++++++++--------- src/input_stream.rs | 30 ++++++++++++------------ src/memory_input_stream.rs | 3 +-- src/output_stream.rs | 30 ++++++++++++------------ src/pollable_input_stream.rs | 22 +++++++----------- src/pollable_output_stream.rs | 22 +++++++----------- src/socket.rs | 22 +++++++----------- src/socket_listener.rs | 43 +++++++++++++++++++---------------- src/subprocess.rs | 9 +++----- 9 files changed, 92 insertions(+), 111 deletions(-) diff --git a/src/file.rs b/src/file.rs index ba69a853..03c76729 100644 --- a/src/file.rs +++ b/src/file.rs @@ -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, @@ -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> - + std::marker::Unpin, + ) -> Pin< + Box< + dyn std::future::Future> + + 'static, + >, >; } @@ -110,16 +109,17 @@ impl> FileExtManual for O { } } - #[cfg(any(feature = "futures", feature = "dox"))] fn replace_contents_async_future + Send + 'static>( &self, contents: B, etag: Option<&str>, make_backup: bool, flags: FileCreateFlags, - ) -> Box< - dyn future::Future> - + std::marker::Unpin, + ) -> Pin< + Box< + dyn std::future::Future> + + 'static, + >, > { use fragile::Fragile; use GioFuture; diff --git a/src/input_stream.rs b/src/input_stream.rs index 35340cb7..aecae4c2 100644 --- a/src/input_stream.rs +++ b/src/input_stream.rs @@ -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, C: IsA>( &self, @@ -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), (B, glib::Error)>> - + std::marker::Unpin, + ) -> Pin< + Box< + dyn std::future::Future< + Output = Result<(B, usize, Option), (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> + std::marker::Unpin>; + ) -> Pin> + 'static>>; fn into_read(self) -> InputStreamRead { InputStreamRead(self) @@ -263,15 +262,17 @@ impl> 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), (B, glib::Error)>> - + std::marker::Unpin, + ) -> Pin< + Box< + dyn std::future::Future< + Output = Result<(B, usize, Option), (B, glib::Error)>, + > + 'static, + >, > { use GioFuture; @@ -288,12 +289,11 @@ impl> 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> + std::marker::Unpin> + ) -> Pin> + 'static>> { use GioFuture; diff --git a/src/memory_input_stream.rs b/src/memory_input_stream.rs index 9c0da2f8..a5e7d16a 100644 --- a/src/memory_input_stream.rs +++ b/src/memory_input_stream.rs @@ -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(); diff --git a/src/output_stream.rs b/src/output_stream.rs index 55129945..030149d1 100644 --- a/src/output_stream.rs +++ b/src/output_stream.rs @@ -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, @@ -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> + std::marker::Unpin>; + ) -> Pin> + '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), (B, glib::Error)>> - + std::marker::Unpin, + ) -> Pin< + Box< + dyn std::future::Future< + Output = Result<(B, usize, Option), (B, glib::Error)>, + > + 'static, + >, >; fn into_write(self) -> OutputStreamWrite { @@ -225,12 +224,11 @@ impl> 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> + std::marker::Unpin> + ) -> Pin> + 'static>> { use GioFuture; @@ -247,15 +245,17 @@ impl> 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), (B, glib::Error)>> - + std::marker::Unpin, + ) -> Pin< + Box< + dyn std::future::Future< + Output = Result<(B, usize, Option), (B, glib::Error)>, + > + 'static, + >, > { use GioFuture; diff --git a/src/pollable_input_stream.rs b/src/pollable_input_stream.rs index 855274fd..b19acc97 100644 --- a/src/pollable_input_stream.rs +++ b/src/pollable_input_stream.rs @@ -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( @@ -31,19 +29,17 @@ pub trait PollableInputStreamExtManual: Sized { F: FnMut(&Self) -> glib::Continue + 'static, C: IsA; - #[cfg(any(feature = "futures", feature = "dox"))] fn create_source_future>( &self, cancellable: Option<&C>, priority: glib::Priority, - ) -> Box + std::marker::Unpin>; + ) -> Pin + 'static>>; - #[cfg(any(feature = "futures", feature = "dox"))] fn create_source_stream>( &self, cancellable: Option<&C>, priority: glib::Priority, - ) -> Box + std::marker::Unpin>; + ) -> Pin + 'static>>; fn read_nonblocking>( &self, @@ -130,16 +126,15 @@ impl> PollableInputStreamExtManual for O { } } - #[cfg(any(feature = "futures", feature = "dox"))] fn create_source_future>( &self, cancellable: Option<&C>, priority: glib::Priority, - ) -> Box + std::marker::Unpin> { + ) -> Pin + 'static>> { let cancellable: Option = 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 |_| { @@ -149,16 +144,15 @@ impl> PollableInputStreamExtManual for O { })) } - #[cfg(any(feature = "futures", feature = "dox"))] fn create_source_stream>( &self, cancellable: Option<&C>, priority: glib::Priority, - ) -> Box + std::marker::Unpin> { + ) -> Pin + 'static>> { let cancellable: Option = 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() { diff --git a/src/pollable_output_stream.rs b/src/pollable_output_stream.rs index 22a1d4af..7222d1f8 100644 --- a/src/pollable_output_stream.rs +++ b/src/pollable_output_stream.rs @@ -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( @@ -30,19 +28,17 @@ pub trait PollableOutputStreamExtManual { F: FnMut(&Self) -> glib::Continue + 'static, C: IsA; - #[cfg(feature = "futures")] fn create_source_future>( &self, cancellable: Option<&C>, priority: glib::Priority, - ) -> Box + std::marker::Unpin>; + ) -> Pin + 'static>>; - #[cfg(feature = "futures")] fn create_source_stream>( &self, cancellable: Option<&C>, priority: glib::Priority, - ) -> Box + std::marker::Unpin>; + ) -> Pin + 'static>>; } impl> PollableOutputStreamExtManual for O { @@ -98,16 +94,15 @@ impl> PollableOutputStreamExtManual for O { } } - #[cfg(feature = "futures")] fn create_source_future>( &self, cancellable: Option<&C>, priority: glib::Priority, - ) -> Box + std::marker::Unpin> { + ) -> Pin + 'static>> { let cancellable: Option = 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 |_| { @@ -117,16 +112,15 @@ impl> PollableOutputStreamExtManual for O { })) } - #[cfg(feature = "futures")] fn create_source_stream>( &self, cancellable: Option<&C>, priority: glib::Priority, - ) -> Box + std::marker::Unpin> { + ) -> Pin + 'static>> { let cancellable: Option = 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 |_| { diff --git a/src/socket.rs b/src/socket.rs index c5df7e99..50c9d368 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -14,15 +14,13 @@ use std::mem::transmute; use std::os::raw::c_int; #[cfg(all(not(windows), feature = "dox"))] use std::os::raw::c_void; +use std::pin::Pin; use std::ptr; use Cancellable; use Socket; use SocketAddress; -#[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; #[cfg(unix)] use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; @@ -123,21 +121,19 @@ pub trait SocketExtManual: Sized { F: FnMut(&Self, glib::IOCondition) -> glib::Continue + 'static, C: IsA; - #[cfg(any(feature = "futures", feature = "dox"))] fn create_source_future>( &self, condition: glib::IOCondition, cancellable: Option<&C>, priority: glib::Priority, - ) -> Box + std::marker::Unpin>; + ) -> Pin + 'static>>; - #[cfg(any(feature = "futures", feature = "dox"))] fn create_source_stream>( &self, condition: glib::IOCondition, cancellable: Option<&C>, priority: glib::Priority, - ) -> Box + std::marker::Unpin>; + ) -> Pin + 'static>>; } impl> SocketExtManual for O { @@ -389,17 +385,16 @@ impl> SocketExtManual for O { } } - #[cfg(any(feature = "futures", feature = "dox"))] fn create_source_future>( &self, condition: glib::IOCondition, cancellable: Option<&C>, priority: glib::Priority, - ) -> Box + std::marker::Unpin> { + ) -> Pin + 'static>> { let cancellable: Option = 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( condition, @@ -414,17 +409,16 @@ impl> SocketExtManual for O { })) } - #[cfg(any(feature = "futures", feature = "dox"))] fn create_source_stream>( &self, condition: glib::IOCondition, cancellable: Option<&C>, priority: glib::Priority, - ) -> Box + std::marker::Unpin> { + ) -> Pin + 'static>> { let cancellable: Option = 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( condition, diff --git a/src/socket_listener.rs b/src/socket_listener.rs index 00ec5597..7ab00620 100644 --- a/src/socket_listener.rs +++ b/src/socket_listener.rs @@ -8,17 +8,14 @@ use glib::object::IsA; use glib::translate::*; use glib_sys; use gobject_sys; -#[cfg(any(feature = "futures", feature = "dox"))] use std::boxed::Box as Box_; +use std::pin::Pin; use std::ptr; use Cancellable; use Socket; use SocketConnection; use SocketListener; -#[cfg(any(feature = "futures", feature = "dox"))] -use futures::future; - pub trait SocketListenerExtManual: Sized { fn accept_socket_async< Q: FnOnce(Result<(Socket, Option), glib::Error>) + Send + 'static, @@ -29,12 +26,13 @@ pub trait SocketListenerExtManual: Sized { callback: Q, ); - #[cfg(any(feature = "futures", feature = "dox"))] fn accept_socket_async_future( &self, - ) -> Box< - dyn future::Future), glib::Error>> - + std::marker::Unpin, + ) -> Pin< + Box< + dyn std::future::Future), glib::Error>> + + 'static, + >, >; fn accept_async< @@ -46,12 +44,14 @@ pub trait SocketListenerExtManual: Sized { callback: Q, ); - #[cfg(any(feature = "futures", feature = "dox"))] fn accept_async_future( &self, - ) -> Box_< - dyn future::Future), glib::Error>> - + std::marker::Unpin, + ) -> Pin< + Box_< + dyn std::future::Future< + Output = Result<(SocketConnection, Option), glib::Error>, + > + 'static, + >, >; } @@ -101,12 +101,13 @@ impl> SocketListenerExtManual for O { } } - #[cfg(any(feature = "futures", feature = "dox"))] fn accept_socket_async_future( &self, - ) -> Box< - dyn future::Future), glib::Error>> - + std::marker::Unpin, + ) -> Pin< + Box< + dyn std::future::Future), glib::Error>> + + 'static, + >, > { use GioFuture; @@ -168,12 +169,14 @@ impl> SocketListenerExtManual for O { } } - #[cfg(any(feature = "futures", feature = "dox"))] fn accept_async_future( &self, - ) -> Box_< - dyn future::Future), glib::Error>> - + std::marker::Unpin, + ) -> Pin< + Box_< + dyn std::future::Future< + Output = Result<(SocketConnection, Option), glib::Error>, + > + 'static, + >, > { use fragile::Fragile; use GioFuture; diff --git a/src/subprocess.rs b/src/subprocess.rs index 1584e0bd..be22e628 100644 --- a/src/subprocess.rs +++ b/src/subprocess.rs @@ -1,5 +1,3 @@ -#[cfg(any(feature = "futures", feature = "dox"))] -use futures::future; use gio_sys; use glib::object::IsA; use glib::translate::*; @@ -7,6 +5,7 @@ use glib::GString; use glib_sys; use gobject_sys; use libc::c_char; +use std::pin::Pin; use std::ptr; use Cancellable; use Subprocess; @@ -62,13 +61,11 @@ impl Subprocess { } } - #[cfg(any(feature = "futures", feature = "dox"))] pub fn communicate_utf8_async_future( &self, stdin_buf: Option, - ) -> Box< - dyn future::Future> + std::marker::Unpin, - > { + ) -> Pin> + 'static>> + { use fragile::Fragile; use GioFuture;