pub struct Collector { /* private fields */ }
Expand description
A garbage collector for Owned
and Shared
allocations.
If a Collector
is dropped, it will leak all associated allocations as
well as its internal data structures. To avoid this, ensure that all
allocations have been collected and all Handle
s have been dropped, then
call try_cleanup
.
Implementations§
Source§impl Collector
impl Collector
Sourcepub fn collect(&mut self)
pub fn collect(&mut self)
Drops all of the garbage in the queue.
§Examples
use basedrop::{Collector, Handle, Owned};
use core::mem::drop;
let mut collector = Collector::new();
let handle = collector.handle();
let x = Owned::new(&handle, 1);
let y = Owned::new(&handle, 2);
let z = Owned::new(&handle, 3);
assert_eq!(collector.alloc_count(), 3);
drop(x);
drop(y);
drop(z);
collector.collect();
assert_eq!(collector.alloc_count(), 0);
Sourcepub fn collect_one(&mut self) -> bool
pub fn collect_one(&mut self) -> bool
Attempts to drop the first allocation in the queue. If successful, returns true; otherwise returns false.
§Examples
use basedrop::{Collector, Handle, Owned};
use core::mem::drop;
let mut collector = Collector::new();
let handle = collector.handle();
let x = Owned::new(&handle, 1);
let y = Owned::new(&handle, 2);
let z = Owned::new(&handle, 3);
assert_eq!(collector.alloc_count(), 3);
drop(x);
drop(y);
drop(z);
assert!(collector.collect_one());
assert!(collector.collect_one());
assert!(collector.collect_one());
assert!(!collector.collect_one());
assert_eq!(collector.alloc_count(), 0);
Sourcepub fn handle_count(&self) -> usize
pub fn handle_count(&self) -> usize
Gets the number of live Handle
s to this Collector
.
Sourcepub fn alloc_count(&self) -> usize
pub fn alloc_count(&self) -> usize
Gets the number of live allocations associated with this Collector
.
Sourcepub fn try_cleanup(self) -> Result<(), Self>
pub fn try_cleanup(self) -> Result<(), Self>
Attempts to free all resources associated with this Collector
. This
method will fail and return the original Collector
if there are any
live Handle
s or allocations associated with it.
§Examples
use basedrop::{Collector, Handle, Owned};
use core::mem::drop;
let mut collector = Collector::new();
let handle = collector.handle();
let x = Owned::new(&handle, 3);
let result = collector.try_cleanup();
assert!(result.is_err());
let mut collector = result.unwrap_err();
drop(handle);
drop(x);
collector.collect();
assert!(collector.try_cleanup().is_ok());
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Collector
impl RefUnwindSafe for Collector
impl !Sync for Collector
impl Unpin for Collector
impl UnwindSafe for Collector
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