//! e2: iterate over segments, call method that mutates slices

use fieldwork_borrow::with_;

struct Slices;

impl Slices {
    fn some_code(&mut self) {}
}

struct Segment;

pub struct FileBuilder {
    segments: Vec<Segment>,
    slices: Slices,
}

impl FileBuilder {
    // Original problem code (won't compile):
    // fn problem(&mut self) {
    //     for (i, s) in self.segments.iter().enumerate() {
    //         self.append_slice(some_f2(s), i);
    //     }
    // }

    fn problem(&mut self) {
        with_!(self, {
            methods {
                append_slice(mut slices, _val: u64, _idx: usize) {
                    slices.some_code();
                }
            }
            fields {
                segments
            }
            body {
                for (_i, _s) in segments.iter().enumerate() {
                    self.append_slice(some_f2(s), _i);
                }
            }
        });
    }

    // Original method for reference
    fn _append_slice(&mut self, _: u64, _: usize) {
        self.slices.some_code();
    }
}

#[test]
fn test_e2() {
    let mut builder = FileBuilder {
        segments: vec![Segment, Segment, Segment],
        slices: Slices,
    };
    builder.problem();
}
