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 Handles have been dropped, then call try_cleanup.

Implementations

Constructs a new Collector.

Gets a Handle to this Collector.

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 Handles 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 Handles 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

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.