I'm trying to pass a constructor function as an argument to another function. The function creates a struct with an associated lifetime. I need to create a struct from this pointer after I have created some other objects that this struct can then reference. The example below seems to work:
struct Bar<'a> {
number: Option<&'a usize>,
}
impl<'a> Bar<'a> {
pub fn new() -> Bar<'a> {
Bar { number: None }
}
}
fn foo<'a, F>(func: &F)
where
F: Fn() -> Bar<'a>,
{
let number = 42;
let mut bar = (func)();
bar.number = Some(&number);
}
fn main() {
foo(&Bar::new);
}
When I add a Cell
for interior mutability then it does not compile:
use std::cell::Cell;
struct Bar<'a> {
number: Cell<Option<&'a usize>>,
}
impl<'a> Bar<'a> {
pub fn new() -> Bar<'a> {
Bar {
number: Cell::new(None),
}
}
}
fn foo<'a, F>(func: &F)
where
F: Fn() -> Bar<'a>,
{
let number = 42;
let bar = (func)();
bar.number.set(Some(&number));
}
fn main() {
foo(&Bar::new);
}
Giving me the following error:
error[E0597]: `number` does not live long enough
--> src/main.rs:21:26
|
21 | bar.number.set(Some(&number));
| ^^^^^^ borrowed value does not live long enough
22 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 15:1...
--> src/main.rs:15:1
|
15 | / fn foo<'a, F>(func: &F)
16 | | where
17 | | F: Fn() -> Bar<'a>,
18 | | {
... |
21 | | bar.number.set(Some(&number));
22 | | }
| |_^
Why did the first example work and not the second? Is there a way to specify a lifetime that exists for the scope let mut bar
until the end of the function, rather than 'a
which encompasses the entire function? Is this not possible without Non Lexical Lifetimes or Higher Kind Type Constructors etc?
Cell
by the compiler. See this playground snippet where I've replaced thestd::cell::Cell
with a custom type having exactly the same constraints, but it doesn't exhibit the problem. – Peter Hallfn(...) -> ...
. You just have generic types that implement theFn*
traits (which don't have a succinct name AFAIK). You also don't need to take a reference to it, just pass it by value. – Shepmaster