Closed bIgBV closed 4 years ago
I got iterators working. Took some persuasion to convince the compiler (and a lot of begging honestly) that the function pointer being passed was of the right type, but once that was done, it fit into place with existing iterator code perfectly.
I also updated the public API for Pool
. There are now two main methods to get a new object, create
and create_with
.
create
: I chose the default create method to require passing a closure which instantiates the newly initialized slot. I chose to make this the default because just getting a empty object which you can't really modify without wrapping in a lock seemed like a bad API.create_with
: a helper method where you just want to initialize an object with a direct value, without having to write a closure. (Side note: I just realized that I can reduce code duplication between the two methods).One thing I want to address before getting started on tests is safety. We are calling the passed callback inside an unsafe block:
If the closure causes a panic, then we might be left in an inconsistent state. I think we need to use catch_unwind
here, but I wanted to run this by before I went in that direction.
Add a new API for reusing heap allocation backed by Shards.
This PR adds new APIs to reusing heap allocations. As covered in #2 , when using the
Slab
, any heap allocated object stored in the slab would be dropped when the object was removed from it. This meant that each object stored in theSlab
would have to be individually allocated. ThePool
type aims to reuse these allocations by providing an in-place modification APIs.In order to provide this, the internals of the crate have been significantly refactored in order to allow as much code reuse between the two types as possible.
Pool
specific APIs have the additional trait bounds ofT: Clear + Default
and methods specific for this type are in impl blocks bounded by these traits. Slab specific methods are implemented for types generic overOption<T>
. This is a pattern which is followed down the abstractions toSlot
.