pub struct DropGuard<T: Any> { /* private fields */ }Expand description
A smart pointer which automatically puts the contained object back into the Pool on drop.
This version is not thread-safe. For the thread-safe version take a look at SyncDropGuard.
Implementations§
Source§impl<T: Any> DropGuard<T>
impl<T: Any> DropGuard<T>
Sourcepub fn new(obj: T, pool: &Pool) -> Self
pub fn new(obj: T, pool: &Pool) -> Self
Creates a new DropGuard from an abritrary object and adds the reference to a regular Pool.
§Example
Creating a DropGuard yourself might be usefull if you want to use objects which
you want to manually create, but where you still want to have the auto-adding to the pool
once they go out of scope.
use generic_pool::{Pool, DropGuard};
struct Buffer(Vec<u8>);
impl Buffer {
fn len(&self) -> usize {
self.0.len()
}
}
fn main() {
let mut pool = Pool::new();
// You use buffers which have a length of exactly 1kb.
let buffer = match pool.get_with_guard::<Buffer>() {
Some(buffer) => buffer,
None => DropGuard::new(Buffer(vec![0u8; 1024]), &pool),
};
assert_eq!(buffer.len(), 1024);
assert!(pool.get::<Buffer>().is_none());
drop(buffer);
let buffer = pool.get::<Buffer>().unwrap();
assert_eq!(buffer.len(), 1024);
}Sourcepub fn into_inner(guard: DropGuard<T>) -> T
pub fn into_inner(guard: DropGuard<T>) -> T
Consume this guard and return the contained object.
Note that this is an associated function and not a method. See the example.
§Example
use generic_pool::{Pool, DropGuard};
struct Buffer(Vec<u8>);
impl Buffer {
fn len(&self) -> usize {
self.0.len()
}
}
fn main() {
let mut pool = Pool::new();
// You use buffers which have a length of exactly 1kb.
let buffer = match pool.get_with_guard::<Buffer>() {
Some(buffer) => buffer,
None => DropGuard::new(Buffer(vec![0u8; 1024]), &pool),
};
// Maybe you want to use the buffer for something else.
let buffer: Buffer = DropGuard::into_inner(buffer);
let mut buffer: Vec<u8> = buffer.0;
buffer.clear();
assert_eq!(buffer.len(), 0);
}Trait Implementations§
Source§impl<T: Any> Drop for DropGuard<T>
Ensures the contained value gets automatically added back to the Pool it came from.
impl<T: Any> Drop for DropGuard<T>
Ensures the contained value gets automatically added back to the Pool it came from.
Source§impl<T: Any + Hash> Hash for DropGuard<T>
The hash value corresponds to the hash value of the contained object.
impl<T: Any + Hash> Hash for DropGuard<T>
The hash value corresponds to the hash value of the contained object.
Source§impl<T: Any + Ord> Ord for DropGuard<T>
impl<T: Any + Ord> Ord for DropGuard<T>
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl<T: Any + PartialOrd> PartialOrd for DropGuard<T>
impl<T: Any + PartialOrd> PartialOrd for DropGuard<T>
impl<T: Any + Eq> Eq for DropGuard<T>
Auto Trait Implementations§
impl<T> Freeze for DropGuard<T>where
T: Freeze,
impl<T> !RefUnwindSafe for DropGuard<T>
impl<T> !Send for DropGuard<T>
impl<T> !Sync for DropGuard<T>
impl<T> Unpin for DropGuard<T>where
T: Unpin,
impl<T> !UnwindSafe for DropGuard<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more