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

Conversation

@sdroege
Copy link
Member

@sdroege sdroege commented Jul 11, 2017

///
/// `func` will be called repeatedly until it returns `Continue(false)`.
pub fn idle_source_new<'a, N: Into<Option<&'a str>>, F>(name: N, priority: Priority, func: F) -> Source
where F: FnMut() -> Continue + Send + 'static {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question here is mostly whether we want single functions for creating Sources, or create something with a builder pattern here. Single functions seems more simple to me

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either way seems fine to me so do whatever seems better to you.

@sdroege
Copy link
Member Author

sdroege commented Jul 11, 2017

For MainContext::invoke() this could in theory be this, but I'm not sure how to handle FnOnce inside a Box<_>. What am I missing?
This fails with error[E0161]: cannot move a value of type std::ops::FnOnce() + std::marker::Send: the size of std::ops::FnOnce() + std::marker::Send cannot be statically determined

impl MainContext {
    pub fn invoke<F>(&self, func: F)
    where F: FnOnce() + Send + 'static {
        unsafe {
            glib_ffi::g_main_context_invoke_full(self.to_glib_none().0, glib_ffi::G_PRIORITY_DEFAULT_IDLE, Some(trampoline),
                into_raw(func), Some(destroy_closure))
        }
    }

    pub fn invoke_with_priority<F>(&self, priority: Priority, func: F)
    where F: FnOnce() + Send + 'static {
        unsafe {
            glib_ffi::g_main_context_invoke_full(self.to_glib_none().0, priority.to_glib(), Some(trampoline),
                into_raw(func), Some(destroy_closure))
        }
    }
}

unsafe extern "C" fn trampoline(func: gpointer) -> gboolean {
    let _guard = CallbackGuard::new();
    let func: &RefCell<Option<Box<FnOnce() + Send + 'static>>> = transmute(func);

    if let Some(func) = func.borrow_mut().take() {
        func();
    }

    glib_ffi::G_SOURCE_REMOVE
}

unsafe extern "C" fn destroy_closure(ptr: gpointer) {
    let _guard = CallbackGuard::new();
    Box::<RefCell<Option<Box<FnOnce() + Send + 'static>>>>::from_raw(ptr as *mut _);
}

fn into_raw<F: FnOnce() + Send + 'static>(func: F) -> gpointer {
    let func: Box<RefCell<Option<Box<FnOnce() + Send + 'static>>>> =
        Box::new(RefCell::new(Some(Box::new(func))));
    Box::into_raw(func) as gpointer
}

@sdroege
Copy link
Member Author

sdroege commented Jul 11, 2017

Problem is that we need something like FnBox https://doc.rust-lang.org/alloc/boxed/trait.FnBox.html . So let's add a FIXME comment there for the time when this stabilizes...

@sdroege
Copy link
Member Author

sdroege commented Jul 11, 2017

Good for review then IMHO, @GuillaumeGomez @EPashkin :) No further additions planned to this pull request

glib_ffi::g_main_context_invoke_full(self.to_glib_none().0, glib_ffi::G_PRIORITY_DEFAULT_IDLE, Some(trampoline),
into_raw(func), Some(destroy_closure))
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We always pass to FFI boxed closure. And destroy_closure working with boxed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. Missed that you added your version destory_closure

pub mod prelude;
pub mod signal;
mod source;
pub mod source;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part of source reexported, while part is not.
Not better do pub use source::*; ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your call :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No preference for me so do whatever you want.

@EPashkin
Copy link
Member

Code looks good to me, but PR missing update Gir submodule and regen to correct version.

// unsafe { TODO: call ffi::g_idle_remove_by_data() }
//}

//pub fn idle_source_new() -> /*Ignored*/Option<Source> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we bound Source, why is this ignored?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah nevermind...

@GuillaumeGomez
Copy link
Member

Looks good to me as well. Fix @EPashkin's issues and let's merge!

@sdroege
Copy link
Member Author

sdroege commented Jul 11, 2017

@EPashkin What do you want to have fixed? Should source::* be re-exported at the crate level?

@EPashkin
Copy link
Member

@sdroege Yes + gir submodule update + regen

@sdroege
Copy link
Member Author

sdroege commented Jul 11, 2017

OK, will do in an hour or two

@sdroege
Copy link
Member Author

sdroege commented Jul 11, 2017

Done :)

@sdroege
Copy link
Member Author

sdroege commented Jul 11, 2017

Ah I guess that's the reason for not re-exporting source::*. source::Id should be re-exported as SourceId. What do you suggest? Rename Id to SourceId?

@sdroege
Copy link
Member Author

sdroege commented Jul 11, 2017

Let's try that...

@EPashkin
Copy link
Member

👍
Thanks @sdroege

@GuillaumeGomez
Copy link
Member

The "changes PR" is getting really huge. Next release will be certainly a worldwide ovation. People will throw money at us. Gods will sing our names and we'll be taken as examples by future generations. Can't wait to be at the release. 😭

@sdroege
Copy link
Member Author

sdroege commented Jul 11, 2017

I'd like to get some more things into the next release though, most importantly removing generic impls for the various conversions traits (and move them into the macros) and implement them for enums/flags/boxed/etc types (which is not possible right now. so we can have those in GValues, for example)

@GuillaumeGomez
Copy link
Member

GuillaumeGomez commented Jul 11, 2017

I don't intend to make the release before at least another month. I still need to work on gtk-rs/release anyway. (my comment was super ironic: it'll be hellish for me to make this release unless I end my automatic releaser)

@EPashkin
Copy link
Member

CI passed

@sdroege
Copy link
Member Author

sdroege commented Jul 11, 2017

🎉

@GuillaumeGomez
Copy link
Member

Thanks a lot!

@GuillaumeGomez GuillaumeGomez merged commit aaad196 into gtk-rs:master Jul 11, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants