Rewrite handwritten futures functions

This commit is contained in:
Felix Krull 2019-12-15 15:00:53 +01:00 committed by Colin Walters
parent 3e70feb75f
commit 3290d5c2d1
4 changed files with 35 additions and 60 deletions

View File

@ -1,22 +1,15 @@
#[cfg(any(feature = "v2017_13", feature = "dox"))]
use crate::ChecksumFlags;
use crate::{Checksum, ObjectType};
#[cfg(feature = "futures")]
use futures::future;
use glib::prelude::*;
use glib::translate::*;
use glib::{prelude::*, translate::*};
use glib_sys::GFALSE;
#[cfg(feature = "futures")]
use std::boxed::Box as Box_;
use std::error;
use std::mem::MaybeUninit;
use std::ptr;
use std::{future::Future, mem::MaybeUninit, pin::Pin, ptr};
pub fn checksum_file<P: IsA<gio::File>, Q: IsA<gio::Cancellable>>(
f: &P,
objtype: ObjectType,
cancellable: Option<&Q>,
) -> Result<Checksum, Box<dyn error::Error>> {
) -> Result<Checksum, Box<dyn std::error::Error>> {
unsafe {
let mut out_csum = ptr::null_mut();
let mut error = ptr::null_mut();
@ -34,7 +27,7 @@ pub fn checksum_file<P: IsA<gio::File>, Q: IsA<gio::Cancellable>>(
pub fn checksum_file_async<
P: IsA<gio::File>,
Q: IsA<gio::Cancellable>,
R: FnOnce(Result<Checksum, Box<dyn error::Error>>) + Send + 'static,
R: FnOnce(Result<Checksum, Box<dyn std::error::Error>>) + Send + 'static,
>(
f: &P,
objtype: ObjectType,
@ -44,7 +37,7 @@ pub fn checksum_file_async<
) {
let user_data: Box<R> = Box::new(callback);
unsafe extern "C" fn checksum_file_async_trampoline<
R: FnOnce(Result<Checksum, Box<dyn error::Error>>) + Send + 'static,
R: FnOnce(Result<Checksum, Box<dyn std::error::Error>>) + Send + 'static,
>(
_source_object: *mut gobject_sys::GObject,
res: *mut gio_sys::GAsyncResult,
@ -76,25 +69,20 @@ pub fn checksum_file_async<
}
}
#[cfg(feature = "futures")]
#[allow(clippy::type_complexity)]
pub fn checksum_file_async_future<P: IsA<gio::File> + Clone + 'static>(
f: &P,
objtype: ObjectType,
io_priority: i32,
) -> Box_<dyn future::Future<Output = Result<Checksum, Box<dyn error::Error>>> + std::marker::Unpin>
{
use fragile::Fragile;
use gio::GioFuture;
) -> Pin<Box<dyn Future<Output = Result<Checksum, Box<dyn std::error::Error>>> + 'static>> {
let f = f.clone();
GioFuture::new(&f, move |f, send| {
Box::pin(gio::GioFuture::new(&f, move |f, send| {
let cancellable = gio::Cancellable::new();
let send = Fragile::new(send);
checksum_file_async(f, objtype, io_priority, Some(&cancellable), move |res| {
let _ = send.into_inner().send(res);
send.resolve(res);
});
cancellable
})
}))
}
pub fn checksum_file_from_input<P: IsA<gio::InputStream>, Q: IsA<gio::Cancellable>>(
@ -103,7 +91,7 @@ pub fn checksum_file_from_input<P: IsA<gio::InputStream>, Q: IsA<gio::Cancellabl
in_: Option<&P>,
objtype: ObjectType,
cancellable: Option<&Q>,
) -> Result<Checksum, Box<dyn error::Error>> {
) -> Result<Checksum, Box<dyn std::error::Error>> {
unsafe {
let mut out_csum = ptr::null_mut();
let mut error = ptr::null_mut();
@ -156,7 +144,7 @@ unsafe fn checksum_file_error(
out_csum: *mut [*mut u8; 32],
error: *mut glib_sys::GError,
ret: i32,
) -> Result<Checksum, Box<dyn error::Error>> {
) -> Result<Checksum, Box<dyn std::error::Error>> {
if !error.is_null() {
Err(Box::<glib::Error>::new(from_glib_full(error)))
} else if ret == GFALSE {

View File

@ -9,7 +9,6 @@ use ostree_sys;
use ostree_sys::OstreeKernelArgs;
use std::fmt;
use std::ptr;
use Error;
glib_wrapper! {
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
@ -54,7 +53,7 @@ impl KernelArgs {
pub fn append_proc_cmdline<P: IsA<gio::Cancellable>>(
&mut self,
cancellable: Option<&P>,
) -> Result<(), Error> {
) -> Result<(), glib::Error> {
unsafe {
let mut error = ptr::null_mut();
let _ = ostree_sys::ostree_kernel_args_append_proc_cmdline(
@ -70,7 +69,7 @@ impl KernelArgs {
}
}
pub fn delete(&mut self, arg: &str) -> Result<(), Error> {
pub fn delete(&mut self, arg: &str) -> Result<(), glib::Error> {
unsafe {
let mut error = ptr::null_mut();
let _ = ostree_sys::ostree_kernel_args_delete(
@ -87,7 +86,7 @@ impl KernelArgs {
}
#[cfg(any(feature = "v2019_3", feature = "dox"))]
pub fn delete_key_entry(&mut self, key: &str) -> Result<(), Error> {
pub fn delete_key_entry(&mut self, key: &str) -> Result<(), glib::Error> {
unsafe {
let mut error = ptr::null_mut();
let _ = ostree_sys::ostree_kernel_args_delete_key_entry(
@ -114,7 +113,7 @@ impl KernelArgs {
}
#[cfg(any(feature = "v2019_3", feature = "dox"))]
pub fn new_replace(&mut self, arg: &str) -> Result<(), Error> {
pub fn new_replace(&mut self, arg: &str) -> Result<(), glib::Error> {
unsafe {
let mut error = ptr::null_mut();
let _ = ostree_sys::ostree_kernel_args_new_replace(

View File

@ -1,17 +1,18 @@
#[cfg(any(feature = "v2016_4", feature = "dox"))]
use crate::RepoListRefsExtFlags;
use crate::{Checksum, ObjectName, ObjectType, Repo};
use gio;
use gio_sys;
use glib;
use glib::translate::*;
use glib::Error;
use glib::IsA;
use glib::{self, translate::*, Error, IsA};
use glib_sys;
use ostree_sys;
use std::collections::{HashMap, HashSet};
use std::path::Path;
use std::{mem::MaybeUninit, ptr};
use std::{
collections::{HashMap, HashSet},
future::Future,
mem::MaybeUninit,
path::Path,
pin::Pin,
ptr,
};
unsafe extern "C" fn read_variant_table(
_key: glib_sys::gpointer,
@ -226,15 +227,11 @@ impl Repo {
expected_checksum: Option<&str>,
object: &P,
length: u64,
) -> Box_<dyn future::Future<Output = Result<Checksum, Error>> + std::marker::Unpin> {
use fragile::Fragile;
use gio::GioFuture;
) -> Pin<Box<dyn Future<Output = Result<Checksum, Error>> + 'static>> {
let expected_checksum = expected_checksum.map(ToOwned::to_owned);
let object = object.clone();
GioFuture::new(self, move |obj, send| {
Box::pin(gio::GioFuture::new(self, move |obj, send| {
let cancellable = gio::Cancellable::new();
let send = Fragile::new(send);
obj.write_content_async(
expected_checksum
.as_ref()
@ -243,12 +240,11 @@ impl Repo {
length,
Some(&cancellable),
move |res| {
let _ = send.into_inner().send(res);
send.resolve(res);
},
);
cancellable
})
}))
}
pub fn write_metadata_async<
@ -306,15 +302,11 @@ impl Repo {
objtype: ObjectType,
expected_checksum: Option<&str>,
object: &glib::Variant,
) -> Box_<dyn future::Future<Output = Result<Checksum, Error>> + std::marker::Unpin> {
use fragile::Fragile;
use gio::GioFuture;
) -> Pin<Box<dyn Future<Output = Result<Checksum, Error>> + 'static>> {
let expected_checksum = expected_checksum.map(ToOwned::to_owned);
let object = object.clone();
GioFuture::new(self, move |obj, send| {
Box::pin(gio::GioFuture::new(self, move |obj, send| {
let cancellable = gio::Cancellable::new();
let send = Fragile::new(send);
obj.write_metadata_async(
objtype,
expected_checksum
@ -323,11 +315,10 @@ impl Repo {
&object,
Some(&cancellable),
move |res| {
let _ = send.into_inner().send(res);
send.resolve(res);
},
);
cancellable
})
}))
}
}

View File

@ -1,9 +1,6 @@
use crate::util::*;
use gio::prelude::*;
use gio::NONE_CANCELLABLE;
use glib::prelude::*;
use ostree::ObjectType;
use ostree::*;
use gio::{prelude::*, NONE_CANCELLABLE};
use ostree::{ObjectType, *};
#[cfg(feature = "v2016_8")]
mod checkout_at;