I'm creating a recursive function. Each call to the function modifies a single element in the array, then calls itself to modify the subsequent elements in the array. Once it gets to the end of the array, the entire array is processed not just the small segment that is in the current context.
I was thinking of going with something like:
use std::ops::{Index, IndexMut};
struct ShrinkWrap<'a, T : 'a>
{
offset : usize,
data : &'a mut Vec<T>
}
impl<'a, T> ShrinkWrap<'a, T>
{
pub fn new(
data : &'a mut Vec<T>
) -> ShrinkWrap<T>
{
ShrinkWrap {
offset : 0,
data : data
}
}
pub fn len(&self) -> usize
{
self.data.len() - self.offset
}
pub fn remaining(
&'a self
) -> ShrinkWrap<T>
{
ShrinkWrap {
offset : self.offset + 1,
data : self.data
}
}
pub fn full(&self) -> &Vec<T>
{
&self.data
}
}
impl<'a, T> Index<usize> for ShrinkWrap<'a, T>
{
type Output = T;
fn index<'b>(&'b self, index : usize) -> &'b T
{
&self.data[self.offset + index]
}
}
impl<'a, T> IndexMut<usize> for ShrinkWrap<'a, T>
{
fn index_mut<'b>(&'b mut self, index : usize) -> &'b mut T
{
&mut self.data[self.offset + index]
}
}
fn main() {
let mut vec = vec!(1, 2, 3, 4, 5);
let shrink = ShrinkWrap::new(&mut vec);
let shrunk = shrink.remaining();
println!("{}", shrunk[0]);
}
So that my function looks clean, just modifying index 0, and calling itself with remaining() until len() is 1.
However the remaining() function is wrong. First I'm getting an error E0389 which I can't actually find defined anywhere. Second I feel like I should be specifying a lifetime on the object that is returned so the compiler knows that this element cannot outlive the parent.
Anyone see a way to make the above class actually work?
Maybe I just need to stuff this in a class with the vectors as member variables and pass slices from those variables into the methods.