Trait scc::ebr::Collectible

source ·
pub trait Collectible {
    // Required method
    fn next_ptr_mut(&mut self) -> &mut Option<NonNull<dyn Collectible>>;

    // Provided method
    fn drop_and_dealloc(&mut self) { ... }
}
Expand description

Collectible defines key methods for Self to be reclaimed by the EBR garbage collector.

The ebr module provides managed handles which implement Collectible in tandem with atomic reference counting, however it is also possible to manually implement the Collectible trait for a type to pass an instance of the type to the EBR garbage collector via Guard::defer.

§Examples

use scc::ebr::{Guard, Collectible};
use std::ptr::NonNull;

struct LazyString(String, Option<NonNull<dyn Collectible>>);

impl Collectible for LazyString {
    fn next_ptr_mut(&mut self) -> &mut Option<NonNull<dyn Collectible>> {
        &mut self.1
    }
}

let boxed: Box<LazyString> = Box::new(LazyString(String::from("Lazy"), None));

let static_ref: &'static LazyString = unsafe { std::mem::transmute(&*boxed) };
let guard_for_ref = Guard::new();

let guard_to_drop = Guard::new();
guard_to_drop.defer(boxed);
drop(guard_to_drop);

// The reference is valid as long as a `Guard` that had been created before `boxed` was
// passed to a `Guard` survives.
assert_eq!(static_ref.0, "Lazy");

Required Methods§

source

fn next_ptr_mut(&mut self) -> &mut Option<NonNull<dyn Collectible>>

Returns a mutable reference to the next Collectible pointer.

Provided Methods§

source

fn drop_and_dealloc(&mut self)

Drops itself and frees the memory.

If the instance of the Self type is not created via Box::new or the like, this method has to be implemented for the type.

Implementors§