Struct maskerad_object_pool::RcHandle [] [src]

pub struct RcHandle<T: Recyclable>(pub Rc<RefCell<T>>);

A wrapper around a Rc pointer to a Poolable object with interior mutability.

The Poolable object is wrapped by a RefCell, to be able to mutate the object with an immutable reference.

This RefCell is wrapped by an Rc, a reference-counted pointer, so multiple parts of the program can "own" the object.

This wrapper allows a custom Drop implementation: when a RcHandle is dropped, the contained Poolable object is reinitialized if its strong reference count is equal to two. If it is the case, the object is reinitialized, the inner Rc is dropped and the strong reference count decrease to 1, meaning that the only structure holding a reference is the RcPool itself.

Methods

impl<T: Recyclable> RcHandle<T>
[src]

[src]

Immutably borrows the wrapped value.

Refer to the RefCell::borrow method for more information.

Panics

Panics if the value is currently mutably borrowed.

Example

use maskerad_object_pool::RcPool;
let pool = RcPool::with_capacity(10, || {
    Monster::default()
});

let monster = pool.create_strict()?;
assert_eq!(monster.borrow().level, 10);

[src]

Immutably borrows the wrapped value, returning an error if the value is currently mutably borrowed.

Refer to the RefCell::try_borrow method for more information.

Example

use maskerad_object_pool::RcPool;
let pool = RcPool::with_capacity(10, || {
    Monster::default()
});

let monster = pool.create_strict()?;
let mut_borrowed = monster.borrow_mut();
// Monster is already mutably borrowed.
assert!(monster.try_borrow().is_err());

[src]

Mutably borrows the wrapped value.

Refer to the RefCell::borrow_mut method for more information.

Panics

Panics if the value is currently borrowed.

Example

use maskerad_object_pool::RcPool;
let pool = RcPool::with_capacity(10, || {
    Monster::default()
});

let monster = pool.create_strict()?;
monster.borrow_mut().level_up();
assert_eq!(monster.borrow().level, 11);

[src]

Mutably borrows the wrapped value, returning an error if the value is currently borrowed.

Refer to the RefCell::try_borrow_mut method for more information.

Example

use maskerad_object_pool::RcPool;
let pool = RcPool::with_capacity(10, || {
    Monster::default()
});

let monster = pool.create_strict()?;
let borrowed = monster.borrow();
// Monster already borrowed.
assert!(monster.try_borrow_mut().is_err());

[src]

Returns a raw pointer to the underlying data.

Refer to the RefCell::as_ptr method for more information.

Example

use maskerad_object_pool::RcPool;
let pool = RcPool::with_capacity(10, || {
    Monster::default()
});

let monster = pool.create_strict()?;
let monster_ptr = monster.as_ptr();

Trait Implementations

impl<T: Debug + Recyclable> Debug for RcHandle<T>
[src]

[src]

Formats the value using the given formatter.

impl<T: Eq + Recyclable> Eq for RcHandle<T>
[src]

impl<T: PartialEq + Recyclable> PartialEq for RcHandle<T>
[src]

[src]

This method tests for self and other values to be equal, and is used by ==. Read more

[src]

This method tests for !=.

impl<T: Ord + Recyclable> Ord for RcHandle<T>
[src]

[src]

This method returns an Ordering between self and other. Read more

1.22.0
[src]

Compares and returns the maximum of two values. Read more

1.22.0
[src]

Compares and returns the minimum of two values. Read more

impl<T: PartialOrd + Recyclable> PartialOrd for RcHandle<T>
[src]

[src]

This method returns an ordering between self and other values if one exists. Read more

[src]

This method tests less than (for self and other) and is used by the < operator. Read more

[src]

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

[src]

This method tests greater than (for self and other) and is used by the > operator. Read more

[src]

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl<T: Recyclable> AsRef<Rc<RefCell<T>>> for RcHandle<T>
[src]

[src]

Performs the conversion.

impl<T: Recyclable> Drop for RcHandle<T>
[src]

[src]

This Drop implementation allow us to reinitialize the Poolable object if the strong reference count of the inner Rc is equal to 2.

If it is the case, T is reinitialized, the inner Rc is dropped and the strong reference count is decreased to 1, meaning that the only structure holding a reference is the RcPool itself.

impl<T: Recyclable> Clone for RcHandle<T>
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more