Skip to content

add Recycler to compliment Supplier #21

@vitiral

Description

@vitiral

Supplier is used to construct new instances, however the user may also want to control the maximum size allowed when items go back into the pool to avoid OOM.

I recommend the name Recycler, although you can use whatever name you think is fitting.

use case:

extern crate lifeguard;
use lifeguard::*;

fn main() {
 let pool : Pool<String> = pool()
   // The pool will allocate 128 values for immediate use. More will be allocated on demand.
   .with(StartingSize(128))
   // The pool will only grow up to 4096 values. Further values will be dropped.
   .with(MaxSize(4096))
   // The pool will use this closure (or other object implementing Supply<T>) to allocate
   .with(Supplier(|| String::with_capacity(1024)))
   // The pool will use this closure when adding recycled items back to the pool
   .with(Recycler(|s| {
        s.clear()
        // nightly only
        if s.capacity() > 4096 {
            s.shrink_to(4096); 
        }
    })
   .build();
  // ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions