Compiling playground v0.0.1 (/playground)
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> src/main.rs:17:22
|
17 | asd: lol.clone()
| ^^^^^
|
note: first, the lifetime cannot outlive the lifetime 'method as defined on the method body at 15:12...
--> src/main.rs:15:12
|
15 | fn add<'method>(&mut self, lol: &'method str) -> Arc<dyn Tr + 'x> {
| ^^^^^^^
= note: ...so that the types are compatible:
expected &&str
found &&'method str
note: but, the lifetime must be valid for the lifetime 'x as defined on the impl at 14:6...
--> src/main.rs:14:6
|
14 | impl<'x> Store<'x> {
| ^^
= note: ...so that the expression is assignable:
expected std::sync::Arc<(dyn Tr + 'x)>
found std::sync::Arc<dyn Tr>
error: aborting due to previous error
error: could not compile `playground`.
To learn more, run the command again with --verbose.
Sure, but the type will be different. If you need an owned string, use a String. There's also the Cow type, which could allow you to have a string that can be either an owned string, or a borrow of some other string.
Fundamentally a &str is a reference to data stored somewhere else. Where do you expect the data you just copied to be stored? Rust is not garbage collected, so you need to tell it where to put it — the existence of a reference is not enough to keep it alive.
I would expected to be tied to struct St and be dropped when Arc counter reach zero.
Unfortunately i cannot change the type since in the ""real code"" where the issue originated, it's not a simple &str but a structure<'a> from an external crate..
has no field that allows you to store the data of a string. The reference requires the data to be somewhere else. In fact St has no destructor, because it owns no data that needs cleaning up.
I mean you could try to keep the lifetime around for the lifetime of the Store. Alternatively you could just make a vector from the buffer using field.table.buf.to_vec()