SyncPool

Struct SyncPool 

Source
pub struct SyncPool { /* private fields */ }
Expand description

A thread-safe pool that allows storing abritrary objects.

This version is thread-safe. For the not thread-safe version take a look at Pool.

Implementations§

Source§

impl SyncPool

Source

pub fn new() -> Self

Creates a new SyncPool with the default fallback configuration.

§Example
use generic_pool::{SyncPool, Config};

fn main() {
    let pool = SyncPool::new();
    // Equivalent:
    // let pool = SyncPool::default();

    assert_eq!(pool.fallback_config(), Config::default());
}
Source

pub fn with_fallback_config(config: Config) -> Self

Create a new SyncPool with the provided Config as the fallback configuration.

§Example
use generic_pool::{SyncPool, Config};

fn main() {
    // Use a non-default config.
    let config = Config {
        max: 1_000,
        step: 100,
        start: 500,
    };

    assert_ne!(config, Config::default());

    let mut pool = SyncPool::with_fallback_config(config); // NOTE Config implements Copy.

    assert_eq!(config, pool.fallback_config());
}
Source

pub fn get_config<T: Any + Send + Sync>(&mut self) -> Config

Retrieve the currently active Config for the provided object type.

If you have not manually set a Config for the provided object type, this method will return the fallback configuration.

§Example
use generic_pool::{SyncPool, Config};

fn main() {
    let mut pool = SyncPool::new();

    let config = Config {
        max: 1_000,
        step: 100,
        start: 500,
    };

    // Set the config for `Vec<u8>`.
    pool.set_config::<Vec<u8>>(config); // NOTE: Config implements Copy.

    // Retrieve the config for `Vec<u8>`.
    // We would get back the fallback config without the line above.
    let config_compare = pool.get_config::<Vec<u8>>();

    assert_eq!(config_compare, config);
    assert_ne!(config_compare, pool.fallback_config());
}
Source

pub fn fallback_config(&self) -> Config

Retrieve the fallback Config for all object types which do not have a specific Config set.

§Example
use generic_pool::{SyncPool, Config};

fn main() {
    // Use a non-default config.
    let config = Config {
        max: 1_000,
        step: 100,
        start: 500,
    };

    assert_ne!(config, Config::default());

    let mut pool = SyncPool::with_fallback_config(config); // NOTE Config implements Copy.

    assert_eq!(config, pool.fallback_config());
}
Source

pub fn set_config<T: Any + Send + Sync>(&mut self, config: Config)

Update the Config for the provided object type.

§Example
use generic_pool::{SyncPool, Config};

fn main() {
    let mut pool = SyncPool::new();

    let config = Config {
        max: 1_000,
        step: 100,
        start: 500,
    };

    // Set the config for `Vec<u8>`.
    pool.set_config::<Vec<u8>>(config); // NOTE: Config implements Copy.

    // Retrieve the config for `Vec<u8>`.
    // We would get back the fallback config without the line above.
    let config_compare = pool.get_config::<Vec<u8>>();

    assert_eq!(config_compare, config);
    assert_ne!(config_compare, pool.fallback_config());
}
Source

pub fn delete_config<T: Any>(&mut self)

Delete the costum Config for the provided object type. Afterwards the fallback config will apply again.

§Example
use generic_pool::{SyncPool, Config};

fn main() {
    let mut pool = SyncPool::new();

    let config = Config {
        max: 1_000,
        step: 100,
        start: 500,
    };

    // Set the config for `Vec<u8>`.
    pool.set_config::<Vec<u8>>(config); // NOTE: Config implements Copy.

    assert_eq!(pool.get_config::<Vec<u8>>(), config);

    // Delete the costum config. Afterwards the fallback config will apply.
    pool.delete_config::<Vec<u8>>();

    assert_ne!(pool.get_config::<Vec<u8>>(), config);
    assert_eq!(pool.get_config::<Vec<u8>>(), pool.fallback_config());
}
Source

pub fn set_fallback_config(&mut self, config: Config)

Set the fallback Config for all object types which do not have a specific Config set.

§Example
use generic_pool::{SyncPool, Config};

fn main() {
    // Start with the default config.
    let mut pool = SyncPool::new();

    // Create a non-default config.
    let config = Config {
        max: 1_000,
        step: 100,
        start: 500,
    };

    assert_ne!(config, pool.fallback_config());

    pool.set_fallback_config(config);

    assert_eq!(config, pool.fallback_config());
}
Source

pub fn get<T: Any + Send + Sync>(&self) -> Option<T>

Try to retrieve an object of the specified type. Will give back None if the there are no objects of this type currently stored in the pool.

§Example
use generic_pool::SyncPool;

fn main() {
    let mut pool = SyncPool::new();

    let buffer = match pool.get::<Vec<u8>>() {
        Some(mut buffer) => {
            buffer.clear();
            buffer
        }
        None => Vec::new(),
    };

    assert_eq!(buffer.len(), 0);
}
Source

pub fn get_or_default<T: Any + Send + Sync + Default>(&self) -> T

Retrieve an object of the specified type. If there is no object of this type currently stored in the pool it will create one using its Default implementation.

§Example
use generic_pool::SyncPool;

fn main() {
    let mut pool = SyncPool::new();

    let buffer = pool.get_or_default::<Vec<u8>>();

    assert_eq!(buffer.len(), 0);
}
Source

pub fn get_with_guard<T: Any + Send + Sync>( &mut self, ) -> Option<SyncDropGuard<T>>

Try to retrieve an object of the specified type with a drop guard. Will give back None if the there are no objects of this type currently stored in the pool.

Once the returned SyncDropGuard - if any - goes out of scope it will automatically put the contained object pack into the pool. Note that the object might still get dropped by the pool if the corresponding internal buffer is full.

§Example
use generic_pool::{SyncPool, SyncDropGuard};

fn main() {
    let mut pool = SyncPool::new();

    let buffer: SyncDropGuard<Vec<u8>> = match pool.get_with_guard::<Vec<u8>>() {
        Some(mut buffer) => {
            // DropGuard is a smart pointer, so we can call Vec.clear() directly.
            buffer.clear();
            buffer
        }
        None => SyncDropGuard::new(Vec::new(), &pool),
    };

    assert_eq!(buffer.len(), 0);

    assert!(pool.get::<Vec<u8>>().is_none());

    // Will put the buffer back into the pool.
    drop(buffer);

    assert!(pool.get::<Vec<u8>>().is_some());
}
Source

pub fn get_or_default_with_guard<T: Any + Send + Sync + Default>( &mut self, ) -> SyncDropGuard<T>

Retrieve an object of the specified type with a drop guard. If there is no object of this type currently stored in the pool it will create one using its Default implementation.

Once the returned SyncDropGuard goes out of scope it will automatically put the contained object pack into the pool. Note that the object might still get dropped by the pool if the corresponding internal buffer is full.

§Example
use generic_pool::{SyncPool, SyncDropGuard};

fn main() {
    let mut pool = SyncPool::new();

    let buffer: SyncDropGuard<Vec<u8>> = pool.get_or_default_with_guard::<Vec<u8>>();

    assert_eq!(buffer.len(), 0);

    assert!(pool.get::<Vec<u8>>().is_none());

    // Will put the buffer back into the pool.
    drop(buffer);

    assert!(pool.get::<Vec<u8>>().is_some());
}
Source

pub fn put<T: Any + Send + Sync>(&self, obj: T)

Add an object to the pool. Unless the corresponding internal buffer is full, you can retrieve it later on by calling SyncPool.get() or one of its variants.

§Example
use generic_pool::SyncPool;

fn main() {
    let mut pool = SyncPool::new();

    assert!(pool.get::<Vec<u8>>().is_none());

    pool.put(vec![0u8; 1024]);

    let buffer = pool.get::<Vec<u8>>().unwrap();

    assert_eq!(buffer.len(), 1024);
}

Trait Implementations§

Source§

impl Clone for SyncPool

The cloned SyncPool will still point to the same instance.

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for SyncPool

Source§

fn default() -> SyncPool

Returns the “default value” for a type. Read more
Source§

impl Display for SyncPool

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.