Skip to main content

PotentialWell

Trait PotentialWell 

Source
pub trait PotentialWell {
    type Well<T>: Well<Target = T>;

    // Required method
    fn new<T>(data: T) -> Self::Well<T>;
}
Expand description

Potential well, representing a generic container.

Useful for recursive data structures, since it avoids recursive types.

§Examples

Atomic linked list via PotentialWell:

use potential_well::{PotentialWell, PotentialAtomicOption};

struct Link<T, W: PotentialWell = Box<()>> {
    data: T,
    next: PotentialAtomicOption<Link<T, W>, W>
}
struct List<T, W: PotentialWell = Box<()>> {
    root: PotentialAtomicOption<Link<T, W>, W>,
}

let list: List<u32> = List {
    root: PotentialAtomicOption::some(Box::new(Link {
        data: 1,
        next: PotentialAtomicOption::some(Box::new(Link {
            data: 2,
            next: PotentialAtomicOption::some(Box::new(Link {
                data: 3,
                next: PotentialAtomicOption::none(),
            }))
        })),
    })),
};

// no Drop implementation needed!
drop(list);

Without PotentialWell, you immediately run into trouble:

use potential_well::{Well, AtomicOption};

// recursive type: Link<T, Box<Link<T, Box<Link<T, ...>>>>>
struct Link<T, W: Well<Target = Link<T, Self>>> {
    data: T,
    next: AtomicOption<W>,
}
struct List<T, W: Well<Target = Link<T, Self>>> {
    root: AtomicOption<W>,
}

let list: List<u32, Box<_>> = List {
    root: AtomicOption::some(Box::new(Link {
        data: 1,
        next: AtomicOption::some(Box::new(Link {
            data: 2,
            next: AtomicOption::some(Box::new(Link {
                data: 3,
                next: AtomicOption::none(),
            }))
        })),
    })),
};

Required Associated Types§

Source

type Well<T>: Well<Target = T>

Makes a well of a particular type.

Required Methods§

Source

fn new<T>(data: T) -> Self::Well<T>

Makes that well.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl PotentialWell for Box<()>

Available on crate features alloc only.
Source§

type Well<T> = Box<T>

Source§

fn new<T>(data: T) -> Box<T>

Source§

impl PotentialWell for Rc<()>

Available on crate feature alloc only.
Source§

type Well<T> = Rc<T>

Source§

fn new<T>(data: T) -> Rc<T>

Source§

impl PotentialWell for Weak<()>

Available on crate feature alloc only.
Source§

type Well<T> = Weak<T>

Source§

fn new<T>(_: T) -> Weak<T>

Source§

impl PotentialWell for Arc<()>

Available on crate feature alloc only.
Source§

type Well<T> = Arc<T>

Source§

fn new<T>(data: T) -> Arc<T>

Source§

impl PotentialWell for Weak<()>

Available on crate feature alloc only.
Source§

type Well<T> = Weak<T>

Source§

fn new<T>(_: T) -> Weak<T>

Source§

impl PotentialWell for Pin<Box<()>>

Available on crate features alloc only.
Source§

type Well<T> = Pin<Box<T>>

Source§

fn new<T>(data: T) -> Pin<Box<T>>

Source§

impl PotentialWell for Pin<Rc<()>>

Available on crate feature alloc only.
Source§

type Well<T> = Pin<Rc<T>>

Source§

fn new<T>(data: T) -> Pin<Rc<T>>

Source§

impl PotentialWell for Pin<Arc<()>>

Available on crate feature alloc only.
Source§

type Well<T> = Pin<Arc<T>>

Source§

fn new<T>(data: T) -> Pin<Arc<T>>

Implementors§