Struct basedrop::Collector [−][src]
pub struct Collector { /* fields omitted */ }
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
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);
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);
Gets the number of live Handle
s to this Collector
.
Gets the number of live allocations associated with this Collector
.
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());