5

currently I have some code similar to the following:

// Lifetime of child is enforced to be valid as far as Parent is valid
struct Child<'a> {
    some_data : i32,
    phantom_lifetime: PhantomData<&'a Parent>
}

struct Parent {

}

#[wasm_bindgen]
pub struct Foo<'a> {
    parent: Parent,
    children: Vec<Child<'a>>
}

The problem now is I have to get rid of the lifetime of Foo because wasm_bindgen will not allow it.

Intuitively the lifetime should be enforced just by the creation of the struct Foo. However the compiler asks me to provide the lifetime anyway.

Anyway I have no idea how to accomplish that. Can somebody help me?

milck
  • 592
  • 3
  • 12
  • I don't know wasm_bindgen if it lets you do this but have you tried declaring `Foo` with : `children: Vec>` – Ömer Erden Nov 11 '19 at 14:21
  • 1
    thanks, `'static` lifetime is working. However I would prefer a way to indicate the relationship between `Foo.parent` and `Foo.children` without it, as `'static` will make any struct `Foo` created make its content live forever? – milck Nov 11 '19 at 14:32
  • 1
    No it just points that your lifetime argument will be `'static`, this means; `Child` has a lifetime constraint `'a`, anything you bind with `'a` lifetime must live in `'a`. We are telling that `'a` equals to `'static`. In your case lifetime has just omitted, that is why it works. As a side effect if you borrow anything inside Child in `'a` lifetime, it should be live in `'static`. so if you create or move some object into `Child` you cannot borrow that object in a `'a` lifetime in `Child`. – Ömer Erden Nov 11 '19 at 16:38
  • 1
    It looks like you have a self referential struct. Maybe [`rental`](https://crates.io/crates) will help you? – rodrigo Nov 11 '19 at 18:36
  • Thanks for the input, I guess my understanding of the `'static` lifetime is a little bit vague, however it is a working solution for now. `rental` looks interesting, I'll take a look into it. – milck Nov 12 '19 at 08:20

0 Answers0