diff --git a/packages/core/src/properties.rs b/packages/core/src/properties.rs index be979f58d..0c8ac0415 100644 --- a/packages/core/src/properties.rs +++ b/packages/core/src/properties.rs @@ -243,13 +243,34 @@ impl<'a> SuperFrom, OptionArgumentsFromMarker> for Option } #[doc(hidden)] -pub struct OptionHandlerMarker; +pub struct OptionCallbackMarker(std::marker::PhantomData); -impl SuperFrom - for Option> +// Closure can be created from FnMut -> async { anything } or FnMut -> Ret +impl< + Function: FnMut(Args) -> Spawn + 'static, + Args: 'static, + Spawn: SpawnIfAsync + 'static, + Ret: 'static, + Marker, + > SuperFrom> for Option> { - 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 = (|num| num * num).super_into(); + let callback: Callback = (|num| async move { println!("{num}") }).super_into(); + + // Converting from closures to optional callbacks works + let optional: Option> = (|num| num * num).super_into(); + let optional: Option> = + (|num| async move { println!("{num}") }).super_into(); } }