Accept closures of any lifetime

The following code works:

pub trait Listener<Args> {
fn call(&self, a: &usize, b: bool);
}
impl<F> Listener<(&usize,)> for F
where
F: Fn(&usize),
{
fn call(&self, a: &usize, b: bool) {
self(a);
}
}
impl<F> Listener<(&usize, bool)> for F
where
F: Fn(&usize, bool),
{
fn call(&self, a: &usize, b: bool) {
self(a, b);
}
}
fn trigger(f: impl Fn(&usize, bool)) {}
fn listener<Args>(l: impl Listener<Args>) -> impl for<'b> Fn(&'b usize, bool) {
move |a, b| l.call(a, b)
}
fn test() {
trigger(listener(|a: &usize| {}));
trigger(listener(|a:&usize, b: bool| {}));
}

but when I change fn test() to:
fn test() {
trigger(listener(|a:| {}));
trigger(listener(|a, b: bool| {}));
}
The closures don't implement Listener. I suspect it is because of the lifetimes.
Does anyone know how to fix this?

Posted Mar 8, 2026, 5:55 PM UTC

110
Loading comments…