mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-26 14:10:20 +00:00
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:
parent
a09548d80e
commit
6320e00056
1 changed files with 26 additions and 5 deletions
|
@ -243,13 +243,34 @@ impl<'a> SuperFrom<Arguments<'a>, OptionArgumentsFromMarker> for Option<String>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub struct OptionHandlerMarker;
|
pub struct OptionCallbackMarker<T>(std::marker::PhantomData<T>);
|
||||||
|
|
||||||
impl<G: 'static, F: FnMut(G) + 'static> SuperFrom<F, OptionHandlerMarker>
|
// Closure can be created from FnMut -> async { anything } or FnMut -> Ret
|
||||||
for Option<EventHandler<G>>
|
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 {
|
fn super_from(input: Function) -> Self {
|
||||||
Some(EventHandler::new(input))
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue