pub struct WeakListElement<T>where
T: ?Sized,{ /* private fields */ }Expand description
An 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> WeakListElement<T>where
T: ?Sized,
impl<T> WeakListElement<T>where
T: ?Sized,
Sourcepub fn new(t: Weak<T>) -> Self
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::rc::Rc;
use weak_lists::WeakListElement;
struct Client {
element: WeakListElement<Client>,
}
let client = Rc::new_cyclic(|slf| Client {
element: WeakListElement::new(slf.clone()),
});Since only weak references are stored, this does not create any actual reference cycles.
Sourcepub fn attach(&self, to: &WeakList<T>)
pub fn attach(&self, to: &WeakList<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::rc::Rc;
use weak_lists::{WeakList, WeakListElement};
struct Client {
element: WeakListElement<Client>,
}
let clients1 = WeakList::default();
let client = Rc::new_cyclic(|slf| Client {
element: WeakListElement::new(slf.clone()),
});
client.element.attach(&clients1);
assert!(clients1.iter().next().is_some());
let clients2 = WeakList::default();
client.element.attach(&clients2);
assert!(clients1.iter().next().is_none());
assert!(clients2.iter().next().is_some());Sourcepub fn detach(&self)
pub fn detach(&self)
Detaches the element from its current list.
§Examples
use std::rc::Rc;
use weak_lists::{WeakList, WeakListElement};
struct Client {
element: WeakListElement<Client>,
}
let clients = WeakList::default();
let client = Rc::new_cyclic(|slf| Client {
element: WeakListElement::new(slf.clone()),
});
client.element.attach(&clients);
assert!(clients.iter().next().is_some());
client.element.detach();
assert!(clients.iter().next().is_none());