I am getting a lifetime error in the following code and I am unable to decipher the issue.
Please note that the Text
trait is a simplified version from the graphql-parser library. Other types are similarly modeled after types in that library.
pub trait Text<'a>: 'a {
type Value: 'a;
}
impl<'a> Text<'a> for String {
type Value = String;
}
pub struct Address<'a, T: Text<'a>> {
pub street: T::Value,
}
pub struct Person<'person> {
pub address: &'person Address<'person, String>,
}
pub fn process<'p>() {
let address: Address<'p, String> = Address{street: "a street".to_string()};
Person {
address: &address
};
}
The error is:
error[E0597]: `address` does not live long enough
--> src/problem.rs:20:18
|
17 | pub fn process<'p>() {
| -- lifetime `'p` defined here
18 | let address: Address<'p, String> = Address{street: "a street".to_string()};
| ------------------- type annotation requires that `address` is borrowed for `'p`
19 | Person {
20 | address: &address
| ^^^^^^^^ borrowed value does not live long enough
21 | };
22 | }
| - `address` dropped here while still borrowed
If I change type of street
in Address
to String
(and make attendant changes elsewhere), the error goes away. But in my real code, I can't do that (the equivalent types come from the graphql-parser library).
Please let me know any pointers to deal with this error.