I tried to use the Rust crate tray-icon to create a tray icon on Windows 10. I wanted to use the function tray_icon::menu::MenuEvent::set_event_handler() to set an event handler for actions on the tray menu which appears after clicking on the tray icon with the right mouse button.
The function has the signature
pub fn set_event_handler<F: Fn(MenuEvent) + Send + Sync + 'static>(f: Option<F>)
When I try to use that function with the argument None, I get the following error message from Rust analyser:
type annotations needed
multipleimpl
s satisfying_: Fn(MenuEvent)
found in the following crates:alloc
,core
:
- impl<A, F> std::ops::Fn<A> for &F
where A: Tuple, F: std::ops::Fn<A>, F: ?Sized;- impl<Args, F, A> std::ops::Fn<Args> for Box<F, A>
where Args: Tuple, F: std::ops::Fn<Args>, A: Allocator, F: ?Sized;rustcClick for full compiler diagnostic
main.rs(54, 34): type must be known at this point
lib.rs(384, 33): required by a bound inMenuEvent::set_event_handler
main.rs(54, 33): consider specifying the generic argument:::<F>
muda::MenuEvent
pub fn set_event_handler<F>(f: Option<F>)
where
F: Fn(MenuEvent) + Send + Sync + ‘static,
Set a handler to be called for new events. Useful for implementing custom event sender.
I followed the suggestion of Rust analyser and “added the generic argument: ::<F>
“:
MenuEvent::set_event_handler::<F>(None);
Now, Rust analyser cannot find the type F (which I expected), but how can I pass None to that function? Looking at the implementation of that function, one can see, that None is an accepted value:
pub fn set_event_handler<F: Fn(MenuEvent) + Send + Sync + 'static>(f: Option<F>) {
if let Some(f) = f {
let _ = MENU_EVENT_HANDLER.set(Some(Box::new(f)));
} else {
let _ = MENU_EVENT_HANDLER.set(None);
}
}