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

IsA<Cancellable> generic instead of concrete Option<&Cancellable> #242

Merged
merged 9 commits into from Sep 27, 2019
Merged
11 changes: 7 additions & 4 deletions src/file.rs
Expand Up @@ -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<Cancellable>,
>(
&self,
contents: B,
etag: Option<&str>,
make_backup: bool,
flags: FileCreateFlags,
cancellable: Option<&Cancellable>,
cancellable: Option<&C>,
callback: R,
);

Expand All @@ -45,17 +46,19 @@ impl<O: IsA<File>> FileExtManual for O {
fn replace_contents_async<
B: AsRef<[u8]> + Send + 'static,
R: FnOnce(Result<(B, glib::GString), (B, Error)>) + Send + 'static,
C: IsA<Cancellable>,
>(
&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<Option<(R, B)>> = 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) = {
Expand Down Expand Up @@ -98,7 +101,7 @@ impl<O: IsA<File>> 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 _,
);
Expand Down
3 changes: 1 addition & 2 deletions src/file_enumerator.rs
@@ -1,5 +1,4 @@
use std::iter::Iterator;
use Cancellable;
use Error;
use FileEnumerator;
use FileEnumeratorExt;
Expand All @@ -9,7 +8,7 @@ impl Iterator for FileEnumerator {
type Item = Result<FileInfo, Error>;

fn next(&mut self) -> Option<Result<FileInfo, Error>> {
match self.next_file(None::<&Cancellable>) {
match self.next_file(::NONE_CANCELLABLE) {
Err(err) => Some(Err(err)),
Ok(file_info) => file_info.map(Ok),
}
Expand Down
58 changes: 33 additions & 25 deletions src/input_stream.rs
Expand Up @@ -20,38 +20,40 @@ use InputStream;
use futures::future;

pub trait InputStreamExtManual: Sized {
fn read<B: AsMut<[u8]>>(
fn read<B: AsMut<[u8]>, C: IsA<Cancellable>>(
&self,
buffer: B,
cancellable: Option<&Cancellable>,
cancellable: Option<&C>,
) -> Result<usize, Error>;

fn read_all<B: AsMut<[u8]>>(
fn read_all<B: AsMut<[u8]>, C: IsA<Cancellable>>(
&self,
buffer: B,
cancellable: Option<&Cancellable>,
cancellable: Option<&C>,
) -> Result<(usize, Option<Error>), Error>;

#[cfg(any(feature = "v2_44", feature = "dox"))]
fn read_all_async<
B: AsMut<[u8]> + Send + 'static,
Q: FnOnce(Result<(B, usize, Option<Error>), (B, Error)>) + Send + 'static,
C: IsA<Cancellable>,
>(
&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<Cancellable>,
>(
&self,
buffer: B,
io_priority: Priority,
cancellable: Option<&Cancellable>,
cancellable: Option<&C>,
callback: Q,
);

Expand Down Expand Up @@ -79,12 +81,13 @@ pub trait InputStreamExtManual: Sized {
}

impl<O: IsA<InputStream>> InputStreamExtManual for O {
fn read<B: AsMut<[u8]>>(
fn read<B: AsMut<[u8]>, C: IsA<Cancellable>>(
&self,
mut buffer: B,
cancellable: Option<&Cancellable>,
cancellable: Option<&C>,
) -> Result<usize, 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();
Expand All @@ -94,7 +97,7 @@ impl<O: IsA<InputStream>> InputStreamExtManual for O {
self.as_ref().to_glib_none().0,
buffer_ptr,
count,
cancellable.0,
gcancellable.0,
&mut error,
);
if error.is_null() {
Expand All @@ -105,12 +108,13 @@ impl<O: IsA<InputStream>> InputStreamExtManual for O {
}
}

fn read_all<B: AsMut<[u8]>>(
fn read_all<B: AsMut<[u8]>, C: IsA<Cancellable>>(
&self,
mut buffer: B,
cancellable: Option<&Cancellable>,
cancellable: Option<&C>,
) -> Result<(usize, Option<Error>), 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();
Expand All @@ -122,7 +126,7 @@ impl<O: IsA<InputStream>> InputStreamExtManual for O {
buffer_ptr,
count,
bytes_read.as_mut_ptr(),
cancellable.0,
gcancellable.0,
&mut error,
);

Expand All @@ -141,14 +145,16 @@ impl<O: IsA<InputStream>> InputStreamExtManual for O {
fn read_all_async<
B: AsMut<[u8]> + Send + 'static,
Q: FnOnce(Result<(B, usize, Option<Error>), (B, Error)>) + Send + 'static,
C: IsA<Cancellable>,
>(
&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<Option<(Q, B)>> = 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) = {
Expand Down Expand Up @@ -194,7 +200,7 @@ impl<O: IsA<InputStream>> InputStreamExtManual for O {
buffer_ptr,
count,
io_priority.to_glib(),
cancellable.0,
gcancellable.0,
Some(callback),
Box::into_raw(user_data) as *mut _,
);
Expand All @@ -204,14 +210,16 @@ impl<O: IsA<InputStream>> InputStreamExtManual for O {
fn read_async<
B: AsMut<[u8]> + Send + 'static,
Q: FnOnce(Result<(B, usize), (B, Error)>) + Send + 'static,
C: IsA<Cancellable>,
>(
&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<Option<(Q, B)>> = 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) = {
Expand Down Expand Up @@ -249,7 +257,7 @@ impl<O: IsA<InputStream>> InputStreamExtManual for O {
buffer_ptr,
count,
io_priority.to_glib(),
cancellable.0,
gcancellable.0,
Some(callback),
Box::into_raw(user_data) as *mut _,
);
Expand Down Expand Up @@ -318,7 +326,7 @@ impl<T: InputStreamExtManual> InputStreamRead<T> {

impl<T: InputStreamExtManual> io::Read for InputStreamRead<T> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let gio_result = self.0.read(buf, None);
let gio_result = self.0.read(buf, ::NONE_CANCELLABLE);
to_std_io_result(gio_result)
}
}
Expand All @@ -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();
});
Expand All @@ -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());
Expand All @@ -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);
Expand All @@ -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();
});
Expand Down
35 changes: 21 additions & 14 deletions src/output_stream.rs
Expand Up @@ -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<Cancellable>,
>(
&self,
buffer: B,
io_priority: Priority,
cancellable: Option<&Cancellable>,
cancellable: Option<&C>,
callback: Q,
);

fn write_all(
fn write_all<C: IsA<Cancellable>>(
&self,
buffer: &[u8],
cancellable: Option<&Cancellable>,
cancellable: Option<&C>,
) -> Result<(usize, Option<Error>), Error>;

#[cfg(any(feature = "v2_44", feature = "dox"))]
fn write_all_async<
B: AsRef<[u8]> + Send + 'static,
Q: FnOnce(Result<(B, usize, Option<Error>), (B, Error)>) + Send + 'static,
C: IsA<Cancellable>,
>(
&self,
buffer: B,
io_priority: Priority,
cancellable: Option<&Cancellable>,
cancellable: Option<&C>,
callback: Q,
);

Expand Down Expand Up @@ -77,14 +79,16 @@ impl<O: IsA<OutputStream>> OutputStreamExtManual for O {
fn write_async<
B: AsRef<[u8]> + Send + 'static,
Q: FnOnce(Result<(B, usize), (B, Error)>) + Send + 'static,
C: IsA<Cancellable>,
>(
&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<Option<(Q, B)>> = 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) = {
Expand Down Expand Up @@ -120,19 +124,20 @@ impl<O: IsA<OutputStream>> 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<C: IsA<Cancellable>>(
&self,
buffer: &[u8],
cancellable: Option<&Cancellable>,
cancellable: Option<&C>,
) -> Result<(usize, Option<Error>), 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();
Expand All @@ -142,7 +147,7 @@ impl<O: IsA<OutputStream>> OutputStreamExtManual for O {
buffer.to_glib_none().0,
count,
bytes_written.as_mut_ptr(),
cancellable.0,
gcancellable.0,
&mut error,
);

Expand All @@ -162,14 +167,16 @@ impl<O: IsA<OutputStream>> OutputStreamExtManual for O {
'a,
B: AsRef<[u8]> + Send + 'static,
Q: FnOnce(Result<(B, usize, Option<Error>), (B, Error)>) + Send + 'static,
C: IsA<Cancellable>,
>(
&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<Option<(Q, B)>> = 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) = {
Expand Down Expand Up @@ -213,7 +220,7 @@ impl<O: IsA<OutputStream>> 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 _,
);
Expand Down