Hello,
I am trying to implement Index and IndexMut on a container, but return a smart pointer struct instead of a normal reference, but it seems these two things just aren't meant to fit together ![]()
First is the issue of the lifetime I need inside the pointer struct. Index wants the Output to be declared as part of the impl declaration, but then I don't have access to the lifetime of the container object; I need to wait until the function signature for that...
use core::ops::{Index};
struct MyContainer {
records : Vec<String>
}
struct MyElementPointer<'a> {
record : &'a String
}
impl <'a>Index<usize> for MyContainer {
type Output = MyElementPointer<'a>;
fn index(&'a self, idx: usize) -> &MyElementPointer<'a> {
MyElementPointer{record : self.records.get(idx).unwrap()}
}
}
fn main() {
let my_container = MyContainer{records : vec!["one".to_string(), "two".to_string(), "three".to_string()]};
println!("{}", my_container[1].record);
}
And if I could somehow work around the lifetime issue, (just for illustration, I made the "smart pointer" just clone the data so it can have a 'static lifetime....
use core::ops::{Index};
struct MyContainer {
records : Vec<String>,
}
struct MyElementPointer {
record : String
}
impl Index<usize> for MyContainer {
type Output = MyElementPointer;
fn index(&self, idx: usize) -> MyElementPointer {
MyElementPointer{record : self.records.get(idx).unwrap().clone()}
}
}
fn main() {
let my_container = MyContainer{records : vec!["one".to_string(), "two".to_string(), "three".to_string()]};
println!("{}", my_container[1].record);
}
Then I'd have my pick of errors between method index has an incompatible type for trait and cannot return reference to temporary value errors.
Moving on to IndexMut, it relies on the Output type being the same as Index, but in the case of smart pointers, I have a struct MyElementPointerMut that is a compliment to struct MyElementPointer, in the same way that std::cell::Ref and std::cell::RefMut are different types.
Is there another trait that allows access to the square-bracket slice syntax?
Has anyone succeeded at something like this?
Thank you.