Hi All,
This is simplified version of code I am working on:
use std::cell::RefCell;
use std::rc::Rc;
struct Inner<'a> {
pub val: u32,
pub callback: Box<dyn FnMut(u32) + 'a>
}
struct A<'a> {
inner: Rc<RefCell<Inner<'a>>>,
}
impl<'a> A<'a> {
pub fn new<F: 'a>(val: u32, callback: F) -> A<'a>
where F: FnMut(u32)
{
A{inner: Rc::new(RefCell::new(Inner{val, callback: Box::new(callback)})),}
}
pub fn create<'b>(&'a self) -> B<'b> {
B{inner: self.inner.clone()}
}
}
struct B<'b> {
inner: Rc<RefCell<Inner<'b>>>,
}
fn main() {
let val: u32 = 128;
let obj = A::new(0xaa, |v: u32| { println!("--> v {}, val {}", v, val)});
{
let h = obj.create();
//let b = B{inner: obj.inner.clone()}; // works
println!("--> {:?}",h.inner.borrow().val);
(h.inner.borrow_mut().callback)(55);
}
}
This is the playground
The problem is with 'create' function: "cannot infer an appropriate lifetime for lifetime parameter 'b
due to conflicting requirements".
Lifetime of B should be less than A, so I have tried to do something like this:
pub fn create<'b>(&'a self) -> B<'b>
where 'a: 'b
{
B{inner: self.inner.clone()}
}
But this did not help.
Can anybody explain what lifetime configuration should be for 'create' function?
Thank you in advance.