// struct A<'a>{
// r:&'a mut &'a i32
// }
// impl<'a> A<'a>{
// fn call<'b:'a>(& mut self, v:&'b i32){
// *self.r = v;
// }
// }
trait MyFun<'a>{
type Output;
fn call(& mut self,v:&'a i32)->Self::Output;
}
impl<'a,F> MyFun<'a> for F
where F:FnMut(&'a i32){
type Output = ();
fn call(& mut self,v:&'a i32)->Self::Output{
self(v);
}
}
fn test<F>(f:F)
where F:for<'b> MyFun<'b>{
}
fn main(){
let mut r = &1;
test(|v|{ // #1
r = v;
});
// let mut a = A{r:&mut r};
// let ii = 0;
// a.call(&ii);
}
use std::marker::PhantomData;
#1
causes a compiled error since the compiler does not know whether the lifetime in the parameter type outlives the captured one. I tried to use MyFun
to workaround the issue, however, in the current rust, there is no way to express 'a:F
, is there some way to achieve this purpose?
Incidentally, if write the lambda with
let f = |v|{r = v};
f
itself is ok, however, this would make the lifetime of v
a specific one.