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]
fn borrow(&self) -> Ref<T>
[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);
fn try_borrow(&self) -> Result<Ref<T>, BorrowError>
[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());
fn borrow_mut(&self) -> RefMut<T>
[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);
fn try_borrow_mut(&self) -> Result<RefMut<T>, BorrowMutError>
[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());
fn as_ptr(&self) -> *mut T
[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]
impl<T: Eq + Recyclable> Eq for RcHandle<T>
[src]
impl<T: PartialEq + Recyclable> PartialEq for RcHandle<T>
[src]
fn eq(&self, __arg_0: &RcHandle<T>) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &RcHandle<T>) -> bool
[src]
This method tests for !=
.
impl<T: Ord + Recyclable> Ord for RcHandle<T>
[src]
fn cmp(&self, __arg_0: &RcHandle<T>) -> Ordering
[src]
This method returns an Ordering
between self
and other
. Read more
fn max(self, other: Self) -> Self
1.22.0[src]
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
1.22.0[src]
Compares and returns the minimum of two values. Read more
impl<T: PartialOrd + Recyclable> PartialOrd for RcHandle<T>
[src]
fn partial_cmp(&self, __arg_0: &RcHandle<T>) -> Option<Ordering>
[src]
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, __arg_0: &RcHandle<T>) -> bool
[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, __arg_0: &RcHandle<T>) -> bool
[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, __arg_0: &RcHandle<T>) -> bool
[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, __arg_0: &RcHandle<T>) -> bool
[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]
impl<T: Recyclable> Drop for RcHandle<T>
[src]
fn drop(&mut self)
[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.