Convert closures into Option<Callback> automatcially (#2538)

* convert from a closure to Option<Callback> in props

* add a test for optional callback conversion
This commit is contained in:
Evan Almloff 2024-06-19 01:39:35 +02:00 committed by GitHub
parent a09548d80e
commit 6320e00056
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -243,13 +243,34 @@ impl<'a> SuperFrom<Arguments<'a>, OptionArgumentsFromMarker> for Option<String>
}
#[doc(hidden)]
pub struct OptionHandlerMarker;
pub struct OptionCallbackMarker<T>(std::marker::PhantomData<T>);
impl<G: 'static, F: FnMut(G) + 'static> SuperFrom<F, OptionHandlerMarker>
for Option<EventHandler<G>>
// Closure can be created from FnMut -> async { anything } or FnMut -> Ret
impl<
Function: FnMut(Args) -> Spawn + 'static,
Args: 'static,
Spawn: SpawnIfAsync<Marker, Ret> + 'static,
Ret: 'static,
Marker,
> SuperFrom<Function, OptionCallbackMarker<Marker>> for Option<Callback<Args, Ret>>
{
fn super_from(input: F) -> Self {
Some(EventHandler::new(input))
fn super_from(input: Function) -> Self {
Some(Callback::new(input))
}
}
#[test]
#[allow(unused)]
fn optional_callback_compiles() {
fn compiles() {
// Converting from closures (without type hints in the closure works)
let callback: Callback<i32, i32> = (|num| num * num).super_into();
let callback: Callback<i32, ()> = (|num| async move { println!("{num}") }).super_into();
// Converting from closures to optional callbacks works
let optional: Option<Callback<i32, i32>> = (|num| num * num).super_into();
let optional: Option<Callback<i32, ()>> =
(|num| async move { println!("{num}") }).super_into();
}
}