Vec-Like containers interleaved in memory

Hello. This is more of a "concept review" than a code review, since I haven't written the code yet.

I find myself in need of a crate, but I figured I'd check to see if it already exists before writing it, or to see if there is a better idea that I'm not seeing.

It seems like a generally useful thing, so if I do end up writing it, I'll polish it up and make it available on crates.io

What I have in mind is multiple vectors that can be borrowed independently, but are interleaved in memory. So you might use it like this:

#[derive(Debug, Clone)]
struct BigStruct{
    #[point_list(group = "one")]
    a : i32,
    #[point_list(group = "two")]
    b : i32,
    c : i32
}

fn main() {
    let mut point_list = PointList::with_capacity(1000);
    point_list.resize(1000, BigStruct{a : 1, b : 2, c : 3}); //Perhaps this buffer already exists and we just want to treat it as a PointList
    println!("{:?}", point_list[500]);

    let (mut group_one_list, group_two_list) = point_list.borrow_subs_mut();
    for mut group_one in group_one_list.iter_mut() {
        group_one.a = group_two_list[200].b + group_two_list[700].c;
    }
}

Internally, the vector-like objects created by borrow_subs_mut both have the same base_pointer, but because they're accessing differnt fields, it should be safe. (i.e. the implementation will be full of unsafe but the interface should be totally safe)

To implement it, I was imagining that the macros would create separate structs with sizes that lined up with BigStruct, and then my implementation of index trait would do the right pointer arithmetic and transmute. Just like the bad old days of C & C++.

Has this already been done somewhere? Is there a better idea that I'm missing?

Thank you.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.