I have a wrapper around a &str to maintain an invariant. When I try to return a new wrapper that basically wraps the same data wrapped by the argument to a function
(playground), I'm getting "cannot return value referencing function parameter s"
struct MyStrWrapper<'a> {
raw: &'a str,
}
impl AsRef<str> for MyStrWrapper<'_> {
fn as_ref(&self) -> &str {
&self.raw
}
}
fn my_function<'inp>(s: MyStrWrapper<'inp>) -> MyStrWrapper<'inp> {
MyStrWrapper {
raw: &s.as_ref()[1..],
}
}
The indexing is unrelated to the error, but it works when I directly access the member with s.raw instead of via as_ref(). Is there a good way around this that doesn't make raw visible to my_function?