Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix callbacks #761

Merged
merged 4 commits into from May 12, 2019

Conversation

Projects
None yet
3 participants
@GuillaumeGomez
Copy link
Member

commented May 11, 2019

@sdroege

This comment has been minimized.

Copy link
Member

commented May 11, 2019

Does it change anything in any of our crates?

@GuillaumeGomez

This comment has been minimized.

Copy link
Member Author

commented May 11, 2019

It changes the version files yes.

@sdroege

This comment has been minimized.

Copy link
Member

commented May 11, 2019

Ok, fine then ;)

@GuillaumeGomez

This comment has been minimized.

Copy link
Member Author

commented May 11, 2019

The if condition isn't generated apparently. Needs to be fixed as well.

@EPashkin

This comment has been minimized.

Copy link
Member

commented May 11, 2019

PR adds many functions like threads_add_idle to gdk,
SubprocessLauncher::set_child_setup to gio,
result compiles on Windows.

pub fn threads_add_idle<P: Fn() -> bool + Send + Sync + 'static>(function: P) -> u32 {
    assert_initialized_main_thread!();
    let function_data: Box_<P> = Box::new(function);
    unsafe extern "C" fn function_func<P: Fn() -> bool + Send + Sync + 'static>(user_data: glib_sys::gpointer) -> glib_sys::gboolean {
        let callback: &P = &*(user_data as *mut _);
        let res = (*callback)();
        res.to_glib()
    }
    let function = Some(function_func::<P> as _);
    let super_callback0: Box_<P> = function_data;
    unsafe {
        gdk_sys::gdk_threads_add_idle(function, Box::into_raw(super_callback0) as *mut _)
    }
}

    #[cfg(any(unix, feature = "dox"))]
    pub fn set_child_setup<P: Fn() + 'static>(&self, child_setup: P) {
        let child_setup_data: Box_<P> = Box::new(child_setup);
        unsafe extern "C" fn child_setup_func<P: Fn() + 'static>(user_data: glib_sys::gpointer) {
            let callback: &P = &*(user_data as *mut _);
            (*callback)();
        }
        let child_setup = Some(child_setup_func::<P> as _);
        unsafe extern "C" fn destroy_notify_func<P: Fn() + 'static>(data: glib_sys::gpointer) {
            let _callback: Box_<P> = Box_::from_raw(data as *mut _);
        }
        let destroy_call3 = Some(destroy_notify_func::<P> as _);
        let super_callback0: Box_<P> = child_setup_data;
        unsafe {
            gio_sys::g_subprocess_launcher_set_child_setup(self.to_glib_none().0, child_setup, Box::into_raw(super_callback0) as *mut _, destroy_call3);
        }
    }
pos)));
if let Some(pos) = pos {
format!(".{}", pos)
} else {

This comment has been minimized.

Copy link
@EPashkin

EPashkin May 11, 2019

Member

unnecessary tab before if let

This comment has been minimized.

Copy link
@GuillaumeGomez

GuillaumeGomez May 11, 2019

Author Member

The other way around actually: missing tab for this line, the one before and the one after. :p

@EPashkin

This comment has been minimized.

Copy link
Member

commented May 11, 2019

@GuillaumeGomez Thanks, LGFM

@GuillaumeGomez

This comment has been minimized.

Copy link
Member Author

commented May 11, 2019

No no! It's not ready yet!

@GuillaumeGomez GuillaumeGomez force-pushed the GuillaumeGomez:fix-callbacks branch from ad88c4f to b6a00ed May 11, 2019

@GuillaumeGomez

This comment has been minimized.

Copy link
Member Author

commented May 11, 2019

That *should* be good now. Just waiting for @elmarco confirmation.

@GuillaumeGomez

This comment has been minimized.

Copy link
Member Author

commented May 11, 2019

Ah hold on, new error spotted!

@GuillaumeGomez GuillaumeGomez force-pushed the GuillaumeGomez:fix-callbacks branch from f9be7f6 to ea78a8b May 12, 2019

@GuillaumeGomez

This comment has been minimized.

Copy link
Member Author

commented May 12, 2019

Finally ready!

@GuillaumeGomez

This comment has been minimized.

Copy link
Member Author

commented May 12, 2019

It generates code like this:

fn extract<P: IsA<gio::File>, Q: IsA<gio::Cancellable>>(&self, path: Option<&P>, file_callback: Option<&mut dyn (FnMut(&File) -> bool)>, progress_callback: Option<&mut dyn (FnMut(i64, i64))>, cancellable: Option<&Q>) -> Result<(), Error> {
    let file_callback_data: &Option<&mut dyn (FnMut(&File) -> bool)> = &file_callback;
    unsafe extern "C" fn file_callback_func<P: IsA<gio::File>, Q: IsA<gio::Cancellable>>(file: *mut gcab_sys::GCabFile, user_data: glib_sys::gpointer) -> glib_sys::gboolean {
        let file = from_glib_borrow(file);
        let callback: &mut (&mut Option<&mut dyn (FnMut(&File) -> bool)>, &mut Option<&mut dyn (FnMut(i64, i64))>) = &mut *(user_data as *mut _);
        let res = if let Some(ref mut callback) = (*callback).0 {
            callback(&file)
        } else {
            panic!("cannot get closure...")
        };
        res.to_glib()
    }
    let file_callback = if file_callback_data.is_some() { Some(file_callback_func::<P, Q> as _) } else { None };
    let progress_callback_data: &Option<&mut dyn (FnMut(i64, i64))> = &progress_callback;
    unsafe extern "C" fn progress_callback_func<P: IsA<gio::File>, Q: IsA<gio::Cancellable>>(current_num_bytes: i64, total_num_bytes: i64, user_data: glib_sys::gpointer) {
        let callback: &mut (&mut Option<&mut dyn (FnMut(&File) -> bool)>, &mut Option<&mut dyn (FnMut(i64, i64))>) = &mut *(user_data as *mut _);
        if let Some(ref mut callback) = (*callback).1 {
            callback(current_num_bytes, total_num_bytes)
        } else {
            panic!("cannot get closure...")
        };
    }
    let progress_callback = if progress_callback_data.is_some() { Some(progress_callback_func::<P, Q> as _) } else { None };
    let super_callback0: &(&Option<&mut dyn (FnMut(&File) -> bool)>, &Option<&mut dyn (FnMut(i64, i64))>) = &(file_callback_data, progress_callback_data);
    unsafe {
        let mut error = ptr::null_mut();
        let _ = gcab_sys::gcab_cabinet_extract(self.as_ref().to_glib_none().0, path.map(|p| p.as_ref()).to_glib_none().0, file_callback, progress_callback, super_callback0 as *const _ as usize as *mut _, cancellable.map(|p| p.as_ref()).to_glib_none().0, &mut error);
        if error.is_null() { Ok(()) } else { Err(from_glib_full(error)) }
    }
}
@EPashkin

This comment has been minimized.

Copy link
Member

commented May 12, 2019

@GuillaumeGomez Thanks, looks good, and changes in gdk/gio still same
You want me merge after CI, or still waiting for @elmarco confirmation?

@GuillaumeGomez

This comment has been minimized.

Copy link
Member Author

commented May 12, 2019

@elmarco confirmed (he's sitting next to me currently, that helps ;) )

@GuillaumeGomez

This comment has been minimized.

Copy link
Member Author

commented May 12, 2019

But let's wait for CI in any case!

@GuillaumeGomez

This comment has been minimized.

Copy link
Member Author

commented May 12, 2019

All green.

@EPashkin

This comment has been minimized.

Copy link
Member

commented May 12, 2019

Thanks again

@EPashkin EPashkin merged commit 3114b7c into gtk-rs:master May 12, 2019

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@GuillaumeGomez GuillaumeGomez deleted the GuillaumeGomez:fix-callbacks branch May 12, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.