# ObjectPool

Version: 0.5.0
Framework commit: d503df2befa756e681fcdc6250e9552dc7388bea
Canonical URL: /docs/0.5.0/api/ferroui-base/utilities/struct-objectpool/

**struct** · [`ferroui-base`](/docs/0.5.0/api/ferroui-base/) · v0.5.0

```rust
struct ObjectPool<T>
```

Provides a thread-safe object pool with size limits and object validation.

[View source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/utilities/object_pool.rs#L5)

## Members

- [`DEFAULT_MAX_SIZE`](#assoc_const-default_max_size)
- [`new`](#function-new)
- [`rent`](#function-rent)
- [`return_item`](#function-return_item)

<a id="assoc_const-default_max_size"></a>

### DEFAULT_MAX_SIZE

```rust
const DEFAULT_MAX_SIZE: i32
```

The default of the maximum number of objects to keep in the pool.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/utilities/object_pool.rs#L15)

<a id="function-new"></a>

### new

```rust
fn new(factory: impl Fn() -> T + Send + Sync + 'static, validator: Option<Box<dyn Fn(&mut T) -> bool + Send + Sync>>, max_size: i32) -> Self
```

Initializes a new instance of the pool.

`factory` creates new instances. The optional `validator` cleans and
validates an object before it returns to the pool; it returns false
to discard the object. `max_size` is the maximum number of objects to
keep in the pool.

Panics when `max_size` is less than 1.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/utilities/object_pool.rs#L25)

<a id="function-rent"></a>

### rent

```rust
fn rent(self: &Self) -> T
```

Rents an object from the pool or creates a new one if the pool is empty.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/utilities/object_pool.rs#L45)

<a id="function-return_item"></a>

### return_item

```rust
fn return_item(self: &Self, item: T)
```

Returns an object to the pool if it passes validation and the pool is not full.

Callers must not return the same item more than once without an
intervening `rent`. The pool does not detect duplicate
returns. The pool also does nothing with the items it drops (when
full or when the validator returns false) beyond dropping them; any
cleanup belongs in the validator or in the caller.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/utilities/object_pool.rs#L63)

## Trait implementations

- `Send`
- `Sync`
- `Freeze`
- `UnsafeUnpin`
- `Unpin`
- `UnwindSafe`
- `RefUnwindSafe`
- [`ClassDefaults`](/docs/0.5.0/api/ferroui-base/trait-classdefaults/)
- `Borrow<T>`
- `BorrowMut<T>`
- `Into<U>`
- `From<T>`
- `TryInto<U>`
- `TryFrom<U>`
- `Any`
- `ErasedDestructor`
- `IntoEither`

## Availability

Extracted for `aarch64-apple-darwin`. Cargo features are listed in the [crate index](/docs/0.5.0/api/).
