I have a Rust struct T<'a> with Cow<'a, X> fields. I want to create .to_owned version of the struct and embed in another struct with a different lifetime. Lifetime of new struct could be shorter or longer. How do I do that?
-
Show this thread
-
Replying to @indygreg2 replies 0 retweets 0 likes
-
Replying to @sgrif
Oh, I thought the ToOwned trait would conflict if I tried to implement to_owned() on a trait-less impl! I guess it doesn't. Thanks!
2 replies 0 retweets 1 like -
Replying to @indygreg
If you want to use the `ToOwned` trait you should probably create an owned version of your struct with no lifetime instead of `Cow`
1 reply 0 retweets 0 likes -
Replying to @sgrif
Yeah, I was hoping to abuse `Cow` to allow me to only have to define a single struct type. That's much simpler, as once you have 2 types, you need to express everything in terms of traits. Adds coding overhead. (Although it is arguably better.)
1 reply 0 retweets 0 likes -
Replying to @indygreg
You really don't though. You can express everything in terms of `Cow<'a, Borrowed>` or `T: AsRef<Borrowed>`. It's no different than `String` or `str`.
1 reply 0 retweets 1 like -
Replying to @sgrif
Oooh - I haven't learned the `T: AsRef<Borrowed>` trick yet!
1 reply 0 retweets 1 like
You might have issues depending on whether you can convert a reference of one into a reference of the other, really just depends on what other fields exist.
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.