I have this struct:
struct Test {
data: Vec<u8>,
}
impl Test {
fn new() -> Self {
return Test { data: Vec::new() };
}
}
let a = Test::new();
// populate something into a...
I would like to implement slice for this type, when doing e.g. &a[2..5], it returns a slice pointing to 2..5 of its internal data. Is it possible to do this in Rust?
You can do that by implementing the Index trait, and bounding the index to SliceIndex:
struct Test {
data: Vec<u8>,
}
impl Test {
fn new(data: Vec<u8>) -> Self {
Test { data }
}
}
impl<Idx> std::ops::Index<Idx> for Test
where
Idx: std::slice::SliceIndex<[u8]>,
{
type Output = Idx::Output;
fn index(&self, index: Idx) -> &Self::Output {
&self.data[index]
}
}
fn main() {
let test = Test::new(vec![1, 2, 3]);
let slice = &test[1..];
assert_eq!(slice, [2, 3]);
let first = &test[0];
assert_eq!(first, &1);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With