What I am proposing here does not use any extra memory. It just reuses the storage of the Vec of aiocbs that you had initially.
Also, note that Rust does not support immovable types yet, and if someone has even just an &mut to an aiocb, it is still possible to swap it out of its current location… I’m not sure if it would be safe to give your clients anything more than an & reference to them.
EDIT: You may also want to explore this kind of API, which consumes self at the end of the user-specified aiocb processing routine. It’s a common trick to handle “consume a value after letting a user play with references to it” scenarios.
struct MyStruct {
cbs: Vec<aiocb>,
}
impl MyStruct {
fn consume<F: FnOnce(&[aiocb])>(self, processing: F) {
processing(&self.cbs);
}
}
// ... user code ...
my_struct.consume(|cbs| {
// Can use the inner aiocbs however I like here: iteration, slicing...
});
// my_struct does not exist anymore here, and aiocbs cannot be leaked out
EDIT 2: Thinking about this some more, I wonder if you should not rethink your use of a vector of aiocbs to begin with:
- Any operation that modifies the vec (insertions, deletions, capacity changes…) while async IO associated with one of the aiocbs is in progress is unsafe.
- Accidentally giving your client mutable access to the aiocbs while async IO is in progress is unsafe.
- Even just dropping the struct hosting the vector of aiocbs while async IO is in progress (which can occur unpredictably as a result of a panic) is unsafe.
Basically, if I understand the way your code currently works, this vector of aiocbs is a major unsafe footgun.