I want to overload the indexing operator for this struct :
struct Ram<'a> {
ram: &'a mut [u8]
}
impl<'a> Ram<'a> {
pub fn new( bytes: &mut[u8]) -> Ram {
Ram {
ram: bytes
}
}
}
... which basically is a "controller" over a byte array. I do it like that because I want to reuse it for byte arrays of different sizes. I understand that the lifetime is here to make sure the "ram" reference is valid throughout the execution. Here's my current index code:
use std::ops::{Index};
impl<'a> Index<usize> for Ram<'a> {
type Output = u8;
fn index(&self, i: usize) -> &'a u8 {
&self.ram[i]
}
}
This doesn't compile. Rust says there's an anonymous lifetime definition that conflicts with 'a at the index(...) definition : "error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements".
How am I supposed to implement this? Or am I just plain wrong with my assumptions about lifetimes? Thanks.