I have a struct that wraps some functionality around a slice:
use std::fmt::Debug;
struct SliceWrapper<'a, T: Debug + Copy + 'a> {
slice: &'a [T],
pos: usize,
}
I want to implement the From
trait for each element that supports AsRef<T: Debug + Copy + 'a>
like this:
impl<'a, T: Debug + Copy + 'a, R: AsRef<[T]> + 'a> From<R> for SliceWrapper<'a, T> {
fn from(slice: R) -> Self {
Self {
slice: slice.as_ref(),
pos: 0,
}
}
}
I get the error:
error[E0597]: `slice` does not live long enough
--> src/lib.rs:11:20
|
11 | slice: slice.as_ref(),
| ^^^^^ borrowed value does not live long enough
...
14 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the impl at 8:6...
--> src/lib.rs:8:6
|
8 | impl<'a, T: Debug + Copy + 'a, R: AsRef<[T]> + 'a> From<R> for SliceWrapper<'a, T> {
| ^^
I don't understand this because I say that R
(slice
) must live as long as my SliceWrapper
– and as far as I understand it, AsRef<_>
inherits the lifetime from it's self
(slice
)...