From e7cba2cee5be79a7fff9d8bfca5a2d59d705db45 Mon Sep 17 00:00:00 2001 From: Federico Mena Quintero Date: Mon, 23 Sep 2019 18:10:51 -0500 Subject: [PATCH 1/9] InputStreamExtManual: use an IsA generic throughout This should make life easier for callers that pass subclasses of Cancellable. --- src/input_stream.rs | 58 ++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/src/input_stream.rs b/src/input_stream.rs index e5409b06..069f9e13 100644 --- a/src/input_stream.rs +++ b/src/input_stream.rs @@ -20,38 +20,40 @@ use InputStream; use futures::future; pub trait InputStreamExtManual: Sized { - fn read>( + fn read, C: IsA>( &self, buffer: B, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, ) -> Result; - fn read_all>( + fn read_all, C: IsA>( &self, buffer: B, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, ) -> Result<(usize, Option), Error>; #[cfg(any(feature = "v2_44", feature = "dox"))] fn read_all_async< B: AsMut<[u8]> + Send + 'static, Q: FnOnce(Result<(B, usize, Option), (B, Error)>) + Send + 'static, + C: IsA, >( &self, buffer: B, io_priority: Priority, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, callback: Q, ); fn read_async< B: AsMut<[u8]> + Send + 'static, Q: FnOnce(Result<(B, usize), (B, Error)>) + Send + 'static, + C: IsA, >( &self, buffer: B, io_priority: Priority, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, callback: Q, ); @@ -79,12 +81,13 @@ pub trait InputStreamExtManual: Sized { } impl> InputStreamExtManual for O { - fn read>( + fn read, C: IsA>( &self, mut buffer: B, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, ) -> Result { - let cancellable = cancellable.to_glib_none(); + let cancellable = cancellable.map(|c| c.as_ref()); + let gcancellable = cancellable.to_glib_none(); let buffer = buffer.as_mut(); let buffer_ptr = buffer.as_mut_ptr(); let count = buffer.len(); @@ -94,7 +97,7 @@ impl> InputStreamExtManual for O { self.as_ref().to_glib_none().0, buffer_ptr, count, - cancellable.0, + gcancellable.0, &mut error, ); if error.is_null() { @@ -105,12 +108,13 @@ impl> InputStreamExtManual for O { } } - fn read_all>( + fn read_all, C: IsA>( &self, mut buffer: B, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, ) -> Result<(usize, Option), Error> { - let cancellable = cancellable.to_glib_none(); + let cancellable = cancellable.map(|c| c.as_ref()); + let gcancellable = cancellable.to_glib_none(); let buffer = buffer.as_mut(); let buffer_ptr = buffer.as_mut_ptr(); let count = buffer.len(); @@ -122,7 +126,7 @@ impl> InputStreamExtManual for O { buffer_ptr, count, bytes_read.as_mut_ptr(), - cancellable.0, + gcancellable.0, &mut error, ); @@ -141,14 +145,16 @@ impl> InputStreamExtManual for O { fn read_all_async< B: AsMut<[u8]> + Send + 'static, Q: FnOnce(Result<(B, usize, Option), (B, Error)>) + Send + 'static, + C: IsA, >( &self, buffer: B, io_priority: Priority, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, callback: Q, ) { - let cancellable = cancellable.to_glib_none(); + let cancellable = cancellable.map(|c| c.as_ref()); + let gcancellable = cancellable.to_glib_none(); let mut user_data: Box> = Box::new(Some((callback, buffer))); // Need to do this after boxing as the contents pointer might change by moving into the box let (count, buffer_ptr) = { @@ -194,7 +200,7 @@ impl> InputStreamExtManual for O { buffer_ptr, count, io_priority.to_glib(), - cancellable.0, + gcancellable.0, Some(callback), Box::into_raw(user_data) as *mut _, ); @@ -204,14 +210,16 @@ impl> InputStreamExtManual for O { fn read_async< B: AsMut<[u8]> + Send + 'static, Q: FnOnce(Result<(B, usize), (B, Error)>) + Send + 'static, + C: IsA, >( &self, buffer: B, io_priority: Priority, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, callback: Q, ) { - let cancellable = cancellable.to_glib_none(); + let cancellable = cancellable.map(|c| c.as_ref()); + let gcancellable = cancellable.to_glib_none(); let mut user_data: Box> = Box::new(Some((callback, buffer))); // Need to do this after boxing as the contents pointer might change by moving into the box let (count, buffer_ptr) = { @@ -249,7 +257,7 @@ impl> InputStreamExtManual for O { buffer_ptr, count, io_priority.to_glib(), - cancellable.0, + gcancellable.0, Some(callback), Box::into_raw(user_data) as *mut _, ); @@ -318,7 +326,7 @@ impl InputStreamRead { impl io::Read for InputStreamRead { fn read(&mut self, buf: &mut [u8]) -> io::Result { - let gio_result = self.0.read(buf, None); + let gio_result = self.0.read(buf, None::<&Cancellable>); to_std_io_result(gio_result) } } @@ -338,7 +346,7 @@ mod tests { let strm = MemoryInputStream::new_from_bytes(&b); let buf = vec![0; 10]; - strm.read_all_async(buf, PRIORITY_DEFAULT_IDLE, None, move |ret| { + strm.read_all_async(buf, PRIORITY_DEFAULT_IDLE, ::NONE_CANCELLABLE, move |ret| { tx.send(ret).unwrap(); l.quit(); }); @@ -358,7 +366,7 @@ mod tests { let strm = MemoryInputStream::new_from_bytes(&b); let mut buf = vec![0; 10]; - let ret = strm.read_all(&mut buf, None).unwrap(); + let ret = strm.read_all(&mut buf, ::NONE_CANCELLABLE).unwrap(); assert_eq!(ret.0, 3); assert!(ret.1.is_none()); @@ -373,7 +381,7 @@ mod tests { let strm = MemoryInputStream::new_from_bytes(&b); let mut buf = vec![0; 10]; - let ret = strm.read(&mut buf, None); + let ret = strm.read(&mut buf, ::NONE_CANCELLABLE); assert_eq!(ret.unwrap(), 3); assert_eq!(buf[0], 1); @@ -388,7 +396,7 @@ mod tests { let strm = MemoryInputStream::new_from_bytes(&b); let buf = vec![0; 10]; - strm.read_async(buf, PRIORITY_DEFAULT_IDLE, None, move |ret| { + strm.read_async(buf, PRIORITY_DEFAULT_IDLE, ::NONE_CANCELLABLE, move |ret| { tx.send(ret).unwrap(); l.quit(); }); From f73b9a89c74bda902070711623350159726ff17e Mon Sep 17 00:00:00 2001 From: Federico Mena Quintero Date: Mon, 23 Sep 2019 18:13:59 -0500 Subject: [PATCH 2/9] Subprocess: use an IsA generic throughout --- src/subprocess.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/subprocess.rs b/src/subprocess.rs index 871dec6c..02f3a296 100644 --- a/src/subprocess.rs +++ b/src/subprocess.rs @@ -1,6 +1,7 @@ #[cfg(any(feature = "futures", feature = "dox"))] use futures::future; use gio_sys; +use glib::object::IsA; use glib::translate::*; use glib::GString; use glib_sys; @@ -12,14 +13,18 @@ use Error; use Subprocess; impl Subprocess { - pub fn communicate_utf8_async) + Send + 'static>( + pub fn communicate_utf8_async< + R: FnOnce(Result<(GString, GString), Error>) + Send + 'static, + C: IsA, + >( &self, stdin_buf: Option, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, callback: R, ) { let stdin_buf = stdin_buf.to_glib_full(); - let cancellable = cancellable.to_glib_none(); + let cancellable = cancellable.map(|c| c.as_ref()); + let gcancellable = cancellable.to_glib_none(); let user_data: Box<(R, *mut c_char)> = Box::new((callback, stdin_buf)); unsafe extern "C" fn communicate_utf8_async_trampoline< R: FnOnce(Result<(GString, GString), Error>) + Send + 'static, @@ -51,7 +56,7 @@ impl Subprocess { gio_sys::g_subprocess_communicate_utf8_async( self.to_glib_none().0, stdin_buf, - cancellable.0, + gcancellable.0, Some(communicate_utf8_async_trampoline::), Box::into_raw(user_data) as *mut _, ); From 554a93a654c28f8bcad89b4e7544cd626a6cee4c Mon Sep 17 00:00:00 2001 From: Federico Mena Quintero Date: Mon, 23 Sep 2019 18:16:40 -0500 Subject: [PATCH 3/9] SocketListenerExtManual: use an IsA generic throughout --- src/socket_listener.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/socket_listener.rs b/src/socket_listener.rs index 2bf2a508..9dcdc4db 100644 --- a/src/socket_listener.rs +++ b/src/socket_listener.rs @@ -23,9 +23,10 @@ use futures::future; pub trait SocketListenerExtManual: Sized { fn accept_socket_async< Q: FnOnce(Result<(Socket, Option), Error>) + Send + 'static, + C: IsA, >( &self, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, callback: Q, ); @@ -38,11 +39,11 @@ pub trait SocketListenerExtManual: Sized { >; fn accept_async< - P: IsA, + C: IsA, Q: FnOnce(Result<(SocketConnection, Option), Error>) + Send + 'static, >( &self, - cancellable: Option<&P>, + cancellable: Option<&C>, callback: Q, ); @@ -58,12 +59,14 @@ pub trait SocketListenerExtManual: Sized { impl> SocketListenerExtManual for O { fn accept_socket_async< Q: FnOnce(Result<(Socket, Option), Error>) + Send + 'static, + C: IsA, >( &self, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, callback: Q, ) { - let cancellable = cancellable.to_glib_none(); + let cancellable = cancellable.map(|c| c.as_ref()); + let gcancellable = cancellable.to_glib_none(); let user_data: Box = Box::new(callback); unsafe extern "C" fn accept_socket_async_trampoline< Q: FnOnce(Result<(Socket, Option), Error>) + Send + 'static, @@ -92,7 +95,7 @@ impl> SocketListenerExtManual for O { unsafe { gio_sys::g_socket_listener_accept_socket_async( self.as_ref().to_glib_none().0, - cancellable.0, + gcancellable.0, Some(callback), Box::into_raw(user_data) as *mut _, ); @@ -122,13 +125,15 @@ impl> SocketListenerExtManual for O { } fn accept_async< - P: IsA, + C: IsA, Q: FnOnce(Result<(SocketConnection, Option), Error>) + Send + 'static, >( &self, - cancellable: Option<&P>, + cancellable: Option<&C>, callback: Q, ) { + let cancellable = cancellable.map(|c| c.as_ref()); + let gcancellable = cancellable.to_glib_none(); let user_data: Box = Box::new(callback); unsafe extern "C" fn accept_async_trampoline< Q: FnOnce(Result<(SocketConnection, Option), Error>) + Send + 'static, @@ -157,7 +162,7 @@ impl> SocketListenerExtManual for O { unsafe { gio_sys::g_socket_listener_accept_async( self.as_ref().to_glib_none().0, - cancellable.map(|p| p.as_ref()).to_glib_none().0, + gcancellable.0, Some(callback), Box::into_raw(user_data) as *mut _, ); From 2edd60720b644f1296f3baa8cc7328ed39fd5454 Mon Sep 17 00:00:00 2001 From: Federico Mena Quintero Date: Mon, 23 Sep 2019 18:48:02 -0500 Subject: [PATCH 4/9] SocketExtManual: use an IsA generic throughout --- src/socket.rs | 115 +++++++++++++++++++++++++++----------------------- 1 file changed, 62 insertions(+), 53 deletions(-) diff --git a/src/socket.rs b/src/socket.rs index 9b274b1c..eb437551 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -71,39 +71,39 @@ impl AsRawSocket for Socket { } pub trait SocketExtManual: Sized { - fn receive>( + fn receive, C: IsA>( &self, buffer: B, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, ) -> Result; - fn receive_from>( + fn receive_from, C: IsA>( &self, buffer: B, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, ) -> Result<(usize, SocketAddress), Error>; - fn receive_with_blocking>( + fn receive_with_blocking, C: IsA>( &self, buffer: B, blocking: bool, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, ) -> Result; - fn send>( + fn send, C: IsA>( &self, buffer: B, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, ) -> Result; - fn send_to, P: IsA>( + fn send_to, P: IsA, C: IsA>( &self, address: Option<&P>, buffer: B, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, ) -> Result; - fn send_with_blocking>( + fn send_with_blocking, C: IsA>( &self, buffer: B, blocking: bool, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, ) -> Result; #[cfg(any(unix, feature = "dox"))] @@ -112,41 +112,43 @@ pub trait SocketExtManual: Sized { #[cfg(any(windows, feature = "dox"))] fn get_socket(&self) -> T; - fn create_source( + fn create_source( &self, condition: glib::IOCondition, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, name: Option<&str>, priority: glib::Priority, func: F, ) -> glib::Source where - F: FnMut(&Self, glib::IOCondition) -> glib::Continue + 'static; + F: FnMut(&Self, glib::IOCondition) -> glib::Continue + 'static, + C: IsA; #[cfg(any(feature = "futures", feature = "dox"))] - fn create_source_future( + fn create_source_future>( &self, condition: glib::IOCondition, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, priority: glib::Priority, ) -> Box + std::marker::Unpin>; #[cfg(any(feature = "futures", feature = "dox"))] - fn create_source_stream( + fn create_source_stream>( &self, condition: glib::IOCondition, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, priority: glib::Priority, ) -> Box + std::marker::Unpin>; } impl> SocketExtManual for O { - fn receive>( + fn receive, C: IsA>( &self, mut buffer: B, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, ) -> Result { - let cancellable = cancellable.to_glib_none(); + let cancellable = cancellable.map(|c| c.as_ref()); + let gcancellable = cancellable.to_glib_none(); let buffer = buffer.as_mut(); let buffer_ptr = buffer.as_mut_ptr(); let count = buffer.len(); @@ -156,7 +158,7 @@ impl> SocketExtManual for O { self.as_ref().to_glib_none().0, buffer_ptr, count, - cancellable.0, + gcancellable.0, &mut error, ); if error.is_null() { @@ -167,12 +169,13 @@ impl> SocketExtManual for O { } } - fn receive_from>( + fn receive_from, C: IsA>( &self, mut buffer: B, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, ) -> Result<(usize, SocketAddress), Error> { - let cancellable = cancellable.to_glib_none(); + let cancellable = cancellable.map(|c| c.as_ref()); + let gcancellable = cancellable.to_glib_none(); let buffer = buffer.as_mut(); let buffer_ptr = buffer.as_mut_ptr(); let count = buffer.len(); @@ -185,7 +188,7 @@ impl> SocketExtManual for O { &mut addr_ptr, buffer_ptr, count, - cancellable.0, + gcancellable.0, &mut error, ); if error.is_null() { @@ -196,13 +199,14 @@ impl> SocketExtManual for O { } } - fn receive_with_blocking>( + fn receive_with_blocking, C: IsA>( &self, mut buffer: B, blocking: bool, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, ) -> Result { - let cancellable = cancellable.to_glib_none(); + let cancellable = cancellable.map(|c| c.as_ref()); + let gcancellable = cancellable.to_glib_none(); let buffer = buffer.as_mut(); let buffer_ptr = buffer.as_mut_ptr(); let count = buffer.len(); @@ -213,7 +217,7 @@ impl> SocketExtManual for O { buffer_ptr, count, blocking.to_glib(), - cancellable.0, + gcancellable.0, &mut error, ); if error.is_null() { @@ -224,12 +228,13 @@ impl> SocketExtManual for O { } } - fn send>( + fn send, C: IsA>( &self, buffer: B, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, ) -> Result { - let cancellable = cancellable.to_glib_none(); + let cancellable = cancellable.map(|c| c.as_ref()); + let gcancellable = cancellable.to_glib_none(); let (count, buffer_ptr) = { let slice = buffer.as_ref(); (slice.len(), slice.as_ptr()) @@ -240,7 +245,7 @@ impl> SocketExtManual for O { self.as_ref().to_glib_none().0, mut_override(buffer_ptr), count, - cancellable.0, + gcancellable.0, &mut error, ); if error.is_null() { @@ -251,13 +256,14 @@ impl> SocketExtManual for O { } } - fn send_to, P: IsA>( + fn send_to, P: IsA, C: IsA>( &self, address: Option<&P>, buffer: B, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, ) -> Result { - let cancellable = cancellable.to_glib_none(); + let cancellable = cancellable.map(|c| c.as_ref()); + let gcancellable = cancellable.to_glib_none(); let (count, buffer_ptr) = { let slice = buffer.as_ref(); (slice.len(), slice.as_ptr()) @@ -270,7 +276,7 @@ impl> SocketExtManual for O { address.map(|p| p.as_ref()).to_glib_none().0, mut_override(buffer_ptr), count, - cancellable.0, + gcancellable.0, &mut error, ); if error.is_null() { @@ -281,13 +287,14 @@ impl> SocketExtManual for O { } } - fn send_with_blocking>( + fn send_with_blocking, C: IsA>( &self, buffer: B, blocking: bool, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, ) -> Result { - let cancellable = cancellable.to_glib_none(); + let cancellable = cancellable.map(|c| c.as_ref()); + let gcancellable = cancellable.to_glib_none(); let (count, buffer_ptr) = { let slice = buffer.as_ref(); (slice.len(), slice.as_ptr()) @@ -299,7 +306,7 @@ impl> SocketExtManual for O { mut_override(buffer_ptr), count, blocking.to_glib(), - cancellable.0, + gcancellable.0, &mut error, ); if error.is_null() { @@ -324,16 +331,17 @@ impl> SocketExtManual for O { } } - fn create_source( + fn create_source( &self, condition: glib::IOCondition, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, name: Option<&str>, priority: glib::Priority, func: F, ) -> glib::Source where F: FnMut(&Self, glib::IOCondition) -> glib::Continue + 'static, + C: IsA, { #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] unsafe extern "C" fn trampoline>( @@ -357,12 +365,13 @@ impl> SocketExtManual for O { Fragile glib::Continue + 'static>>>, >::from_raw(ptr as *mut _); } - let cancellable = cancellable.to_glib_none(); + let cancellable = cancellable.map(|c| c.as_ref()); + let gcancellable = cancellable.to_glib_none(); unsafe { let source = gio_sys::g_socket_create_source( self.as_ref().to_glib_none().0, condition.to_glib(), - cancellable.0, + gcancellable.0, ); let trampoline = trampoline:: as glib_sys::gpointer; glib_sys::g_source_set_callback( @@ -382,13 +391,13 @@ impl> SocketExtManual for O { } #[cfg(any(feature = "futures", feature = "dox"))] - fn create_source_future( + fn create_source_future>( &self, condition: glib::IOCondition, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, priority: glib::Priority, ) -> Box + std::marker::Unpin> { - let cancellable: Option = cancellable.cloned(); + let cancellable: Option = cancellable.map(|c| c.as_ref()).cloned(); let obj = Fragile::new(self.clone()); Box::new(glib::SourceFuture::new(move |send| { @@ -407,13 +416,13 @@ impl> SocketExtManual for O { } #[cfg(any(feature = "futures", feature = "dox"))] - fn create_source_stream( + fn create_source_stream>( &self, condition: glib::IOCondition, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, priority: glib::Priority, ) -> Box + std::marker::Unpin> { - let cancellable: Option = cancellable.cloned(); + let cancellable: Option = cancellable.map(|c| c.as_ref()).cloned(); let obj = Fragile::new(self.clone()); Box::new(glib::SourceStream::new(move |send| { From 96da3a5a3ef70fc08fa6b54a933466f9c8ff4328 Mon Sep 17 00:00:00 2001 From: Federico Mena Quintero Date: Mon, 23 Sep 2019 18:57:55 -0500 Subject: [PATCH 5/9] PollableOutputStreamExtManual: use an IsA generic throughout --- src/pollable_output_stream.rs | 37 +++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/pollable_output_stream.rs b/src/pollable_output_stream.rs index 7ca142d1..dcf8e57f 100644 --- a/src/pollable_output_stream.rs +++ b/src/pollable_output_stream.rs @@ -19,41 +19,43 @@ use futures::future::Future; use futures::stream::Stream; pub trait PollableOutputStreamExtManual { - fn create_source( + fn create_source( &self, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, name: Option<&str>, priority: glib::Priority, func: F, ) -> glib::Source where - F: FnMut(&Self) -> glib::Continue + 'static; + F: FnMut(&Self) -> glib::Continue + 'static, + C: IsA; #[cfg(feature = "futures")] - fn create_source_future( + fn create_source_future>( &self, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, priority: glib::Priority, ) -> Box + std::marker::Unpin>; #[cfg(feature = "futures")] - fn create_source_stream( + fn create_source_stream>( &self, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, priority: glib::Priority, ) -> Box + std::marker::Unpin>; } impl> PollableOutputStreamExtManual for O { - fn create_source( + fn create_source( &self, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, name: Option<&str>, priority: glib::Priority, func: F, ) -> glib::Source where F: FnMut(&Self) -> glib::Continue + 'static, + C: IsA, { #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] unsafe extern "C" fn trampoline>( @@ -71,11 +73,12 @@ impl> PollableOutputStreamExtManual for O { ptr as *mut _, ); } - let cancellable = cancellable.to_glib_none(); + let cancellable = cancellable.map(|c| c.as_ref()); + let gcancellable = cancellable.to_glib_none(); unsafe { let source = gio_sys::g_pollable_output_stream_create_source( self.as_ref().to_glib_none().0, - cancellable.0, + gcancellable.0, ); let trampoline = trampoline:: as glib_sys::gpointer; @@ -96,12 +99,12 @@ impl> PollableOutputStreamExtManual for O { } #[cfg(feature = "futures")] - fn create_source_future( + fn create_source_future>( &self, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, priority: glib::Priority, ) -> Box + std::marker::Unpin> { - let cancellable: Option = cancellable.cloned(); + let cancellable: Option = cancellable.map(|c| c.as_ref()).cloned(); let obj = Fragile::new(self.clone()); Box::new(glib::SourceFuture::new(move |send| { @@ -115,12 +118,12 @@ impl> PollableOutputStreamExtManual for O { } #[cfg(feature = "futures")] - fn create_source_stream( + fn create_source_stream>( &self, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, priority: glib::Priority, ) -> Box + std::marker::Unpin> { - let cancellable: Option = cancellable.cloned(); + let cancellable: Option = cancellable.map(|c| c.as_ref()).cloned(); let obj = Fragile::new(self.clone()); Box::new(glib::SourceStream::new(move |send| { From 3ece0eb5242d47f5fcc363121ddda64f5c7bd8ef Mon Sep 17 00:00:00 2001 From: Federico Mena Quintero Date: Mon, 23 Sep 2019 19:01:03 -0500 Subject: [PATCH 6/9] OutputStreamExtManual: use an IsA generic throughout --- src/output_stream.rs | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/output_stream.rs b/src/output_stream.rs index 4e165167..0b4307e4 100644 --- a/src/output_stream.rs +++ b/src/output_stream.rs @@ -24,29 +24,31 @@ pub trait OutputStreamExtManual: Sized + OutputStreamExt { fn write_async< B: AsRef<[u8]> + Send + 'static, Q: FnOnce(Result<(B, usize), (B, Error)>) + Send + 'static, + C: IsA, >( &self, buffer: B, io_priority: Priority, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, callback: Q, ); - fn write_all( + fn write_all>( &self, buffer: &[u8], - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, ) -> Result<(usize, Option), Error>; #[cfg(any(feature = "v2_44", feature = "dox"))] fn write_all_async< B: AsRef<[u8]> + Send + 'static, Q: FnOnce(Result<(B, usize, Option), (B, Error)>) + Send + 'static, + C: IsA, >( &self, buffer: B, io_priority: Priority, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, callback: Q, ); @@ -77,14 +79,16 @@ impl> OutputStreamExtManual for O { fn write_async< B: AsRef<[u8]> + Send + 'static, Q: FnOnce(Result<(B, usize), (B, Error)>) + Send + 'static, + C: IsA, >( &self, buffer: B, io_priority: Priority, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, callback: Q, ) { - let cancellable = cancellable.to_glib_none(); + let cancellable = cancellable.map(|c| c.as_ref()); + let gcancellable = cancellable.to_glib_none(); let user_data: Box> = Box::new(Some((callback, buffer))); // Need to do this after boxing as the contents pointer might change by moving into the box let (count, buffer_ptr) = { @@ -120,19 +124,20 @@ impl> OutputStreamExtManual for O { mut_override(buffer_ptr), count, io_priority.to_glib(), - cancellable.0, + gcancellable.0, Some(callback), Box::into_raw(user_data) as *mut _, ); } } - fn write_all( + fn write_all>( &self, buffer: &[u8], - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, ) -> Result<(usize, Option), Error> { - let cancellable = cancellable.to_glib_none(); + let cancellable = cancellable.map(|c| c.as_ref()); + let gcancellable = cancellable.to_glib_none(); let count = buffer.len() as usize; unsafe { let mut bytes_written = mem::MaybeUninit::uninit(); @@ -142,7 +147,7 @@ impl> OutputStreamExtManual for O { buffer.to_glib_none().0, count, bytes_written.as_mut_ptr(), - cancellable.0, + gcancellable.0, &mut error, ); @@ -162,14 +167,16 @@ impl> OutputStreamExtManual for O { 'a, B: AsRef<[u8]> + Send + 'static, Q: FnOnce(Result<(B, usize, Option), (B, Error)>) + Send + 'static, + C: IsA, >( &self, buffer: B, io_priority: Priority, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, callback: Q, ) { - let cancellable = cancellable.to_glib_none(); + let cancellable = cancellable.map(|c| c.as_ref()); + let gcancellable = cancellable.to_glib_none(); let user_data: Box> = Box::new(Some((callback, buffer))); // Need to do this after boxing as the contents pointer might change by moving into the box let (count, buffer_ptr) = { @@ -213,7 +220,7 @@ impl> OutputStreamExtManual for O { mut_override(buffer_ptr), count, io_priority.to_glib(), - cancellable.0, + gcancellable.0, Some(callback), Box::into_raw(user_data) as *mut _, ); From c156bbafcca7eb811c2f9c03bc9e81309e1a2995 Mon Sep 17 00:00:00 2001 From: Federico Mena Quintero Date: Mon, 23 Sep 2019 19:13:35 -0500 Subject: [PATCH 7/9] PollableInputStreamExtManual: use an IsA generic throughout --- src/pollable_input_stream.rs | 50 +++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/src/pollable_input_stream.rs b/src/pollable_input_stream.rs index 7793f762..90b8558a 100644 --- a/src/pollable_input_stream.rs +++ b/src/pollable_input_stream.rs @@ -21,47 +21,49 @@ use futures::future::Future; use futures::stream::Stream; pub trait PollableInputStreamExtManual: Sized { - fn create_source( + fn create_source( &self, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, name: Option<&str>, priority: glib::Priority, func: F, ) -> glib::Source where - F: FnMut(&Self) -> glib::Continue + 'static; + F: FnMut(&Self) -> glib::Continue + 'static, + C: IsA; #[cfg(any(feature = "futures", feature = "dox"))] - fn create_source_future( + fn create_source_future>( &self, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, priority: glib::Priority, ) -> Box + std::marker::Unpin>; #[cfg(any(feature = "futures", feature = "dox"))] - fn create_source_stream( + fn create_source_stream>( &self, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, priority: glib::Priority, ) -> Box + std::marker::Unpin>; - fn read_nonblocking( + fn read_nonblocking>( &self, buffer: &mut [u8], - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, ) -> Result; } impl> PollableInputStreamExtManual for O { - fn create_source( + fn create_source( &self, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, name: Option<&str>, priority: glib::Priority, func: F, ) -> glib::Source where F: FnMut(&Self) -> glib::Continue + 'static, + C: IsA, { #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] unsafe extern "C" fn trampoline>( @@ -79,11 +81,12 @@ impl> PollableInputStreamExtManual for O { ptr as *mut _, ); } - let cancellable = cancellable.to_glib_none(); + let cancellable = cancellable.map(|c| c.as_ref()); + let gcancellable = cancellable.to_glib_none(); unsafe { let source = gio_sys::g_pollable_input_stream_create_source( self.as_ref().to_glib_none().0, - cancellable.0, + gcancellable.0, ); let trampoline = trampoline:: as glib_sys::gpointer; @@ -103,12 +106,13 @@ impl> PollableInputStreamExtManual for O { } } - fn read_nonblocking( + fn read_nonblocking>( &self, buffer: &mut [u8], - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, ) -> Result { - let cancellable = cancellable.to_glib_none(); + let cancellable = cancellable.map(|c| c.as_ref()); + let gcancellable = cancellable.to_glib_none(); let count = buffer.len() as usize; unsafe { let mut error = ptr::null_mut(); @@ -116,7 +120,7 @@ impl> PollableInputStreamExtManual for O { self.as_ref().to_glib_none().0, buffer.to_glib_none().0, count, - cancellable.0, + gcancellable.0, &mut error, ); if error.is_null() { @@ -128,12 +132,12 @@ impl> PollableInputStreamExtManual for O { } #[cfg(any(feature = "futures", feature = "dox"))] - fn create_source_future( + fn create_source_future>( &self, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, priority: glib::Priority, ) -> Box + std::marker::Unpin> { - let cancellable: Option = cancellable.cloned(); + let cancellable: Option = cancellable.map(|c| c.as_ref()).cloned(); let obj = Fragile::new(self.clone()); Box::new(glib::SourceFuture::new(move |send| { @@ -147,12 +151,12 @@ impl> PollableInputStreamExtManual for O { } #[cfg(any(feature = "futures", feature = "dox"))] - fn create_source_stream( + fn create_source_stream>( &self, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, priority: glib::Priority, ) -> Box + std::marker::Unpin> { - let cancellable: Option = cancellable.cloned(); + let cancellable: Option = cancellable.map(|c| c.as_ref()).cloned(); let obj = Fragile::new(self.clone()); Box::new(glib::SourceStream::new(move |send| { From 2346a64215628336cb40b6c66a0ddd0ed1ee6fda Mon Sep 17 00:00:00 2001 From: Federico Mena Quintero Date: Mon, 23 Sep 2019 19:17:06 -0500 Subject: [PATCH 8/9] FileExtManual: use an IsA generic throughout --- src/file.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/file.rs b/src/file.rs index 99cc7ad1..0af247ab 100644 --- a/src/file.rs +++ b/src/file.rs @@ -21,13 +21,14 @@ pub trait FileExtManual: Sized { fn replace_contents_async< B: AsRef<[u8]> + Send + 'static, R: FnOnce(Result<(B, glib::GString), (B, Error)>) + Send + 'static, + C: IsA, >( &self, contents: B, etag: Option<&str>, make_backup: bool, flags: FileCreateFlags, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, callback: R, ); @@ -45,17 +46,19 @@ impl> FileExtManual for O { fn replace_contents_async< B: AsRef<[u8]> + Send + 'static, R: FnOnce(Result<(B, glib::GString), (B, Error)>) + Send + 'static, + C: IsA, >( &self, contents: B, etag: Option<&str>, make_backup: bool, flags: FileCreateFlags, - cancellable: Option<&Cancellable>, + cancellable: Option<&C>, callback: R, ) { let etag = etag.to_glib_none(); - let cancellable = cancellable.to_glib_none(); + let cancellable = cancellable.map(|c| c.as_ref()); + let gcancellable = cancellable.to_glib_none(); let user_data: Box> = Box::new(Some((callback, contents))); // Need to do this after boxing as the contents pointer might change by moving into the box let (count, contents_ptr) = { @@ -98,7 +101,7 @@ impl> FileExtManual for O { etag.0, make_backup.to_glib(), flags.to_glib(), - cancellable.0, + gcancellable.0, Some(callback), Box::into_raw(user_data) as *mut _, ); From 8df11edb1261dbe4dd3a473854039ad7ae84398d Mon Sep 17 00:00:00 2001 From: Federico Mena Quintero Date: Tue, 24 Sep 2019 17:43:30 -0500 Subject: [PATCH 9/9] Use NONE_CANCELLABLE in a couple of places instead of a turbofish. --- src/file_enumerator.rs | 3 +-- src/input_stream.rs | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/file_enumerator.rs b/src/file_enumerator.rs index 54d88ce9..8dc9c98a 100644 --- a/src/file_enumerator.rs +++ b/src/file_enumerator.rs @@ -1,5 +1,4 @@ use std::iter::Iterator; -use Cancellable; use Error; use FileEnumerator; use FileEnumeratorExt; @@ -9,7 +8,7 @@ impl Iterator for FileEnumerator { type Item = Result; fn next(&mut self) -> Option> { - match self.next_file(None::<&Cancellable>) { + match self.next_file(::NONE_CANCELLABLE) { Err(err) => Some(Err(err)), Ok(file_info) => file_info.map(Ok), } diff --git a/src/input_stream.rs b/src/input_stream.rs index 069f9e13..da1664af 100644 --- a/src/input_stream.rs +++ b/src/input_stream.rs @@ -326,7 +326,7 @@ impl InputStreamRead { impl io::Read for InputStreamRead { fn read(&mut self, buf: &mut [u8]) -> io::Result { - let gio_result = self.0.read(buf, None::<&Cancellable>); + let gio_result = self.0.read(buf, ::NONE_CANCELLABLE); to_std_io_result(gio_result) } }