I've got a large list of serializable items, but I'm only given an iterator over them.
Is there a way to serialize them into Json with serde?
The only working way I've found is first collecting the items into a vector first:
pub fn write_as_json<'a, I, P>(&mut self, groups: I) -> io::Result<()>
where
I: Iterator<Item = &'a FileGroup<P>>,
P: Serialize + 'a,
{
let groups: Vec<&FileGroup<P>> = groups.collect();
serde_json::to_writer_pretty(&mut self.out, &groups)?
}
Unfortunately, I can't pass an iterator to to_writer_pretty
directly, because it expects a non-mutable reference:
pub fn write_as_json<'a, I, P>(&mut self, groups: I) -> io::Result<()>
where
I: Iterator<Item = &'a FileGroup<P>>,
P: Serialize + 'a,
{
serde_json::to_writer_pretty(&mut self.out, groups)? // expected &T but got I instead
}