basedrop

Struct Collector

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

Implementations§

Source§

impl Collector

Source

pub fn new() -> Collector

Constructs a new Collector.

Source

pub fn handle(&self) -> Handle

Gets a Handle to this Collector.

Source

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);
Source

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);
Source

pub fn handle_count(&self) -> usize

Gets the number of live Handles to this Collector.

Source

pub fn alloc_count(&self) -> usize

Gets the number of live allocations associated with this Collector.

Source

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

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.