frankmcsherry / recycler

A small Rust library for recycling types with owned memory
MIT License
33 stars 2 forks source link

Lifetimes in Recyclable types #3

Open frankmcsherry opened 9 years ago

frankmcsherry commented 9 years ago

It would be nice to be able to recycle types with lifetimes in them. Right now this is an issue, because maintaining e.g. a pool of Vec<&'a T> for longer than 'a is a problem, even though we know that the vectors are all empty, and so contain no references to invalid memory. Perhaps this is as easy as making VecRecycler<R> not have a Vec<Vec<R::Item>> field but rather one of type Vec<UntypedVec>, which doesn't reveal lifetimes of R::Item. This would surely require some unsafe, so ... maybe let's ponder that for a bit.

koivunej commented 4 years ago

I have used this crate in my private database project a ~while~ some years back and sometimes I get back to this issue, as it was the one thing I really needed to recycle: a buffer of log entries needed to be written/flushed to a log. The difficulty was that these log entries held references to data blobs which lived on a Bytes from bytes crate.

If anyone is looking to recycle a Vec<LogEntry<'a>> or similar, I do not know if you can put the recycler behind a nice API, but taking the vec apart into raw values and rebuilding it once again might work wonderfully, as hinted in the original issue description. And as long as the vec is cleared before taking it apart, I think this use of unsafe might be perfectly safe :)