I couldn't find a good post title, but what I want to do is actually quite simple (I think).
I have one object that represents a ZIP file that is to be written, with multiple files in it. It should allow writing data to those inner files in arbitrary order. Because the files in a ZIP file have to be contiguous, my object needs to store the data until it's ready write. (Later I want to add functionality to stream data into one file at a time.)
I could easily implement an API like this:
let mut zip = Archive::new();
zip.add_data("file1", "some data");
zip.add_data("file2", "some data");
zip.add_data("file1", "some data");
But I would prefer to have an API like this (note that the file names are given first):
let zip = Archive::new();
let file1 = zip.add_file("file1");
let file2 = zip.add_file("file2");
file1.add_data("some_data");
file2.add_data("some_data");
file1.add_data("some_data");
I couldn't come up with an implementation that satisfies the borrow checker. Is such an API possible (and idiomatic) in Rust?