#[repr(transparent)]pub struct Closure { /* private fields */ }
Implementations
sourceimpl Closure
impl Closure
sourcepub fn new<F: Fn(&[Value]) -> Option<Value> + Send + Sync + 'static>(
callback: F
) -> Self
pub fn new<F: Fn(&[Value]) -> Option<Value> + Send + Sync + 'static>(
callback: F
) -> Self
Creates a new closure around a Rust closure.
Note that RustClosure
provides more convenient and non-unsafe API for invoking
closures. This type mostly exists for FFI interop.
Panics
Invoking the closure with wrong argument types or returning the wrong return value type will panic.
Example
use glib::prelude::*;
let closure = glib::Closure::new(|values| {
let x = values[0].get::<i32>().unwrap();
Some((x + 1).to_value())
});
// Invoking non-Rust closures is unsafe because of possibly missing
// argument and return value type checks.
let res = unsafe {
closure
.invoke_with_values(glib::Type::I32, &[1i32.to_value()])
.and_then(|v| v.get::<i32>().ok())
.expect("Invalid return value")
};
assert_eq!(res, 2);
sourcepub fn new_local<F: Fn(&[Value]) -> Option<Value> + 'static>(
callback: F
) -> Self
pub fn new_local<F: Fn(&[Value]) -> Option<Value> + 'static>(
callback: F
) -> Self
Creates a new closure around a Rust closure.
Note that RustClosure
provides more convenient and non-unsafe API for invoking
closures. This type mostly exists for FFI interop.
Panics
Invoking the closure with wrong argument types or returning the wrong return value type will panic.
Invoking the closure from a different thread than this one will panic.
sourcepub unsafe fn new_unsafe<F: Fn(&[Value]) -> Option<Value>>(callback: F) -> Self
pub unsafe fn new_unsafe<F: Fn(&[Value]) -> Option<Value>>(callback: F) -> Self
Creates a new closure around a Rust closure.
Safety
The captured variables of the closure must stay valid as long as the return value of this constructor does, and it must be valid to call the closure from any thread that is used by callers.
sourcepub unsafe fn invoke_with_values(
&self,
return_type: Type,
values: &[Value]
) -> Option<Value>
pub unsafe fn invoke_with_values(
&self,
return_type: Type,
values: &[Value]
) -> Option<Value>
Invokes the closure with the given arguments.
For invalidated closures this returns the “default” value of the return type.
Safety
The argument types and return value type must match the ones expected by the closure or otherwise the behaviour is undefined.
Closures created from Rust via e.g. Closure::new
will panic on type mismatches but
this is not guaranteed for closures created from other languages.
sourcepub fn invalidate(&self)
pub fn invalidate(&self)
Invalidates the closure.
Invoking an invalidated closure has no effect.
Trait Implementations
sourceimpl AsRef<Closure> for RustClosure
impl AsRef<Closure> for RustClosure
sourceimpl From<RustClosure> for Closure
impl From<RustClosure> for Closure
sourcefn from(c: RustClosure) -> Self
fn from(c: RustClosure) -> Self
Converts to this type from the input type.
sourceimpl Ord for Closure
impl Ord for Closure
sourceimpl PartialOrd<Closure> for Closure
impl PartialOrd<Closure> for Closure
sourcefn partial_cmp(&self, other: &Closure) -> Option<Ordering>
fn partial_cmp(&self, other: &Closure) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl StaticType for Closure
impl StaticType for Closure
sourcefn static_type() -> Type
fn static_type() -> Type
Returns the type identifier of Self
.
impl Eq for Closure
impl Send for Closure
impl StructuralEq for Closure
impl StructuralPartialEq for Closure
impl Sync for Closure
Auto Trait Implementations
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> StaticTypeExt for T where
T: StaticType,
impl<T> StaticTypeExt for T where
T: StaticType,
sourcefn ensure_type()
fn ensure_type()
Ensures that the type has been registered with the type system.
sourceimpl<T> ToClosureReturnValue for T where
T: ToValue,
impl<T> ToClosureReturnValue for T where
T: ToValue,
fn to_closure_return_value(&self) -> Option<Value>
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
sourceimpl<T> ToSendValue for T where
T: Send + ToValue + ?Sized,
impl<T> ToSendValue for T where
T: Send + ToValue + ?Sized,
sourcefn to_send_value(&self) -> SendValue
fn to_send_value(&self) -> SendValue
Returns a SendValue
clone of self
.