Crates.io | auto_pool |
lib.rs | auto_pool |
version | |
source | src |
created_at | 2024-11-05 20:48:07.980794+00 |
updated_at | 2025-03-07 01:17:32.238175+00 |
description | A simple pool that returns items automatically after it's dropped |
homepage | |
repository | https://github.com/sild/libs_rs |
max_upload_size | |
id | 1437140 |
Cargo.toml error: | TOML parse error at line 20, column 1 | 20 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
This pool automatically manages the return of objects.
The primary difference from competitors is the interface - implementation follows the RAII pattern, meaning the user must provide objects when creating the pool.
The Pool
class has only three public methods:
with_config(config: Config, items)
: Creates a new pool with a custom configuration.new(items)
: An alias for with_config
that uses the default configuration.take(&self)
: Extracts an object from the pool.The configuration allows you to set the wait duration for the take
method (default is Duration::MAX
, which essentially means "Wait until you receive it").
Examples:
fn main() -> Result<(), autoreturn_pool::Error> {
// basic usage
let pool = autoreturn_pool::Pool::new([1, 2])?;
let item = pool.take()?.unwrap();
// with custom config
let config = autoreturn_pool::Config {
wait_duration: std::time::Duration::from_millis(5),
};
let pool = autoreturn_pool::Pool::with_config(config, [1, 2])?;
let item = pool.take()?.unwrap();
}
// with custom object:
#[derive(Default)]
struct MyObject {
value: i32,
}
fn main() -> Result<(), autoreturn_pool::Error> {
let pool_objects = [
MyObject::default(),
MyObject::default()
];
let pool = autoreturn_pool::Pool::new(pool_objects)?;
let mut item = pool.take()?.unwrap();
Ok(())
}