SyncWeakListElement

Struct SyncWeakListElement 

Source
pub struct SyncWeakListElement<T>
where T: ?Sized,
{ /* private fields */ }
Expand description

An thread-safe element that can be inserted into a weak list.

Each element can be attached to 0 or 1 list. Attaching it to a list automatically detaches itself from the previous list.

When this object is dropped, it detaches itself from its current list.

Implementations§

Source§

impl<T> SyncWeakListElement<T>
where T: ?Sized,

Source

pub fn new(t: Weak<T>) -> Self

Creates a new list element.

This object holds a weak reference to the T. When this object is dropped, it automatically detaches itself from the list it is currently attached to.

Often, this object is directly contained in the T:

use std::sync::Arc;
use weak_lists::SyncWeakListElement;

struct Client {
    element: SyncWeakListElement<Client>,
}

let client = Arc::new_cyclic(|slf| Client {
    element: SyncWeakListElement::new(slf.clone()),
});

Since only weak references are stored, this does not create any actual reference cycles.

Source

pub fn attach(&self, to: &SyncWeakList<T>)

Attaches the list element to a list.

If this object was previously attached to a list, it is automatically detached from that list.

The list will only hold on a weak reference to this element and vice versa.

Any existing iterator over the list might or might not see this element, this is unspecified.

§Examples
use std::sync::Arc;
use weak_lists::{SyncWeakList, SyncWeakListElement};

struct Client {
    element: SyncWeakListElement<Client>,
}

let clients1 = SyncWeakList::default();

let client = Arc::new_cyclic(|slf| Client {
    element: SyncWeakListElement::new(slf.clone()),
});

client.element.attach(&clients1);

assert!(clients1.iter().next().is_some());

let clients2 = SyncWeakList::default();

client.element.attach(&clients2);

assert!(clients1.iter().next().is_none());
assert!(clients2.iter().next().is_some());
Source

pub fn detach(&self)

Detaches the element from its current list.

§Examples
use std::rc::Rc;
use std::sync::Arc;
use weak_lists::{SyncWeakList, SyncWeakListElement};

struct Client {
    element: SyncWeakListElement<Client>,
}

let clients = SyncWeakList::default();

let client = Arc::new_cyclic(|slf| Client {
    element: SyncWeakListElement::new(slf.clone()),
});

client.element.attach(&clients);

assert!(clients.iter().next().is_some());

client.element.detach();

assert!(clients.iter().next().is_none());

Trait Implementations§

Source§

impl<T> Debug for SyncWeakListElement<T>
where T: ?Sized,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Drop for SyncWeakListElement<T>
where T: ?Sized,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for SyncWeakListElement<T>

§

impl<T> !RefUnwindSafe for SyncWeakListElement<T>

§

impl<T> Send for SyncWeakListElement<T>
where T: Sync + Send + ?Sized,

§

impl<T> Sync for SyncWeakListElement<T>
where T: Sync + Send + ?Sized,

§

impl<T> Unpin for SyncWeakListElement<T>
where T: ?Sized,

§

impl<T> !UnwindSafe for SyncWeakListElement<T>

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.