pub struct SyncDropGuard<T: Any + Send + Sync> { /* private fields */ }Expand description
Implementations§
Source§impl<T: Any + Send + Sync> SyncDropGuard<T>
impl<T: Any + Send + Sync> SyncDropGuard<T>
Sourcepub fn new(obj: T, pool: &SyncPool) -> Self
pub fn new(obj: T, pool: &SyncPool) -> Self
Creates a new DropGuard from an abritrary object and adds the reference to a SyncPool.
§Example
Creating a SyncDropGuard 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::{SyncPool, SyncDropGuard};
struct Buffer(Vec<u8>);
impl Buffer {
fn len(&self) -> usize {
self.0.len()
}
}
fn main() {
let mut pool = SyncPool::new();
// You use buffers which have a length of exactly 1kb.
let buffer = match pool.get_with_guard::<Buffer>() {
Some(buffer) => buffer,
None => SyncDropGuard::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: SyncDropGuard<T>) -> T
pub fn into_inner(guard: SyncDropGuard<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::{SyncPool, SyncDropGuard};
struct Buffer(Vec<u8>);
impl Buffer {
fn len(&self) -> usize {
self.0.len()
}
}
fn main() {
let mut pool = SyncPool::new();
// You use buffers which have a length of exactly 1kb.
let buffer = match pool.get_with_guard::<Buffer>() {
Some(buffer) => buffer,
None => SyncDropGuard::new(Buffer(vec![0u8; 1024]), &pool),
};
// Maybe you want to use the buffer for something else.
let buffer: Buffer = SyncDropGuard::into_inner(buffer);
let mut buffer: Vec<u8> = buffer.0;
buffer.clear();
assert_eq!(buffer.len(), 0);
}Trait Implementations§
Source§impl<T: Any + Send + Sync> Drop for SyncDropGuard<T>
Ensures the contained value gets automatically added back to the SyncPool it came from.
impl<T: Any + Send + Sync> Drop for SyncDropGuard<T>
Ensures the contained value gets automatically added back to the SyncPool it came from.
Source§impl<T: Any + Send + Sync + Hash> Hash for SyncDropGuard<T>
The hash value corresponds to the hash value of the contained object.
impl<T: Any + Send + Sync + Hash> Hash for SyncDropGuard<T>
The hash value corresponds to the hash value of the contained object.
Source§impl<T: Any + Send + Sync + Ord> Ord for SyncDropGuard<T>
impl<T: Any + Send + Sync + Ord> Ord for SyncDropGuard<T>
Source§fn cmp(&self, other: &SyncDropGuard<T>) -> Ordering
fn cmp(&self, other: &SyncDropGuard<T>) -> Ordering
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 + Send + Sync + PartialOrd> PartialOrd for SyncDropGuard<T>
impl<T: Any + Send + Sync + PartialOrd> PartialOrd for SyncDropGuard<T>
impl<T: Any + Send + Sync + Eq> Eq for SyncDropGuard<T>
Auto Trait Implementations§
impl<T> Freeze for SyncDropGuard<T>where
T: Freeze,
impl<T> RefUnwindSafe for SyncDropGuard<T>where
T: RefUnwindSafe,
impl<T> Send for SyncDropGuard<T>
impl<T> Sync for SyncDropGuard<T>
impl<T> Unpin for SyncDropGuard<T>where
T: Unpin,
impl<T> UnwindSafe for SyncDropGuard<T>where
T: UnwindSafe,
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