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
impl SyncPool
Sourcepub fn with_fallback_config(config: Config) -> Self
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());
}Sourcepub fn get_config<T: Any + Send + Sync>(&mut self) -> Config
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());
}Sourcepub fn fallback_config(&self) -> Config
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());
}Sourcepub fn set_config<T: Any + Send + Sync>(&mut self, config: Config)
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());
}Sourcepub fn delete_config<T: Any>(&mut self)
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());
}Sourcepub fn set_fallback_config(&mut self, config: Config)
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());
}Sourcepub fn get<T: Any + Send + Sync>(&self) -> Option<T>
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);
}Sourcepub fn get_or_default<T: Any + Send + Sync + Default>(&self) -> T
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);
}Sourcepub fn get_with_guard<T: Any + Send + Sync>(
&mut self,
) -> Option<SyncDropGuard<T>>
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());
}Sourcepub fn get_or_default_with_guard<T: Any + Send + Sync + Default>(
&mut self,
) -> SyncDropGuard<T>
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());
}Sourcepub fn put<T: Any + Send + Sync>(&self, obj: T)
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);
}