Consider the following code:
use std::ops;
struct Wrap<T>(T);
impl<T> Wrap<T> {
fn new(element: T) -> Self {
Wrap(element)
}
}
// implementation of other methods that makes the wrapper necessary ;-)
impl ops::Index<ops::Range<usize>> for Wrap<Vec<i8>> {
type Output = Wrap<&[i8]>;
fn index(&self, range: ops::Range<usize>) -> &Self::Output {
&Wrap::<&[i8]>::new(&self.0[range])
}
}
impl ops::Index<ops::Range<usize>> for Wrap<&[i8]> {
type Output = Wrap<&[i8]>;
fn index(&self, range: ops::Range<usize>) -> &Self::Output {
&Wrap::<&[i8]>::new(&self.0[range])
}
}
The compiler states:
error[E0106]: missing lifetime specifier
--> src/lib.rs:14:24
|
14 | type Output = Wrap<&[i8]>;
| ^ expected lifetime parameter
error[E0106]: missing lifetime specifier
--> src/lib.rs:21:45
|
21 | impl ops::Index<ops::Range<usize>> for Wrap<&[i8]> {
| ^ expected lifetime parameter
error[E0106]: missing lifetime specifier
--> src/lib.rs:22:24
|
22 | type Output = Wrap<&[i8]>;
| ^ expected lifetime parameter
How should I set the lifetimes here? I want Wrap
to work for owned Vec
s as well as borrowed slices. What would be the best solution?