Struct RwStore

Source
pub struct RwStore<Element> { /* private fields */ }
Expand description

A concurrent, unordered collection where each element has an internally generated ID and a read-write lock. See the module-level documentation for more.

Implementations§

Source§

impl<Element> RwStore<Element>

Source

pub fn new() -> Self

Creates a new, empty store.

Source

pub fn insert(&self, element: Element) -> Id

Inserts an element into the store, returning it’s generated unique ID. The returned ID can be used to subsequently read, modify or remove the element.

§Example
let store = RwStore::new();
let id = store.insert(42);
Source

pub fn remove(&self, id: Id) -> Option<Element>

Removes an element from the store using its ID if it has not already been removed. Returns the element if it was present.

If a read or write lock is held on the element, this will block until it is released.

§Example
let store = RwStore::new();
let id = store.insert(42);
assert_eq!(store.remove(id), Some(42));
§Safety

The given ID must have been created by this store, see the struct-level documentation.

Source

pub fn remove_with_timeout( &self, id: Id, timeout: Timeout, ) -> BlockResult<Option<Element>>

Removes an element from the store using its ID if it has not already been removed. Returns the element if it was present.

If a read or write lock is held on the element, this will block until it is released or the given timeout expires.

§Example
let store = RwStore::new();
let id = store.insert(42);
let read_lock = store.read(id);
assert!(store.remove_with_timeout(id, DontBlock).is_err());
§Safety

The given ID must have been created by this store, see the struct-level documentation.

Source

pub fn remove_locked(&self, lock: WriteLock<'_, Element>) -> Element

Removes an element from the store directly using a write lock held over the element. This is likely more efficient than unlocking and removing the element, and is guaranteed to succeed atomically, without blocking.

§Example
let store = RwStore::new();
let id = store.insert(42);
let write_lock = store.write(id).unwrap();
assert_eq!(store.remove_locked(write_lock), 42);
§Safety

The given lock must have been acquired from one of the locking methods on this store. Using a lock from another store will panic when debug assertions are enabled, but may cause undefined behavior when they are disabled (i.e. in release mode).

Source

pub fn read(&self, id: Id) -> Option<ReadLock<'_, Element>>

Acquires a read lock on an element given its ID, if it is still present in the store.

If a write lock is held on the element, this will block until it is released.

This may be called reentrantly, but acquiring more than 231 - 2 concurrent read locks on the same element will panic.

§Example
let store = RwStore::new();
let id = store.insert(42);
let read_lock = store.read(id).unwrap();
assert_eq!(*read_lock, 42);
§Safety

The given ID must have been created by this store, see the struct-level documentation.

Source

pub fn read_with_timeout( &self, id: Id, timeout: Timeout, ) -> BlockResult<Option<ReadLock<'_, Element>>>

Acquires a read lock on an element given its ID, if it is still present in the store.

If a write lock is held on the element, this will block until it is released or the given timeout expires.

This may be called reentrantly, but acquiring more than 231 - 2 concurrent read locks on the same element will panic.

§Example
let store = RwStore::new();
let id = store.insert(42);
let write_lock = store.write(id).unwrap();
assert!(store.read_with_timeout(id, DontBlock).is_err());
§Safety

The given ID must have been created by this store, see the struct-level documentation.

Source

pub fn write(&self, id: Id) -> Option<WriteLock<'_, Element>>

Acquires a write lock on an element given its ID, if it is still present in the store.

If a read or write lock is held on the element, this will block until it is released.

§Example
let store = RwStore::new();
let id = store.insert(42);
let mut write_lock = store.write(id).unwrap();
*write_lock = 24;
§Safety

The given ID must have been created by this store, see the struct-level documentation.

Source

pub fn write_with_timeout( &self, id: Id, timeout: Timeout, ) -> BlockResult<Option<WriteLock<'_, Element>>>

Acquires a write lock on an element given its ID, if it is still present in the store.

If a read or write lock is held on the element, this will block until it is released or the given timeout expires.

§Example
let store = RwStore::new();
let id = store.insert(42);
let read_lock = store.read(id).unwrap();
assert!(store.write_with_timeout(id, DontBlock).is_err());
§Safety

The given ID must have been created by this store, see the struct-level documentation.

Source

pub fn get_mut(&mut self, id: Id) -> Option<&mut Element>

Directly obtains a mutable reference to an element given its ID, if it is still present in the store.

Because a mutable reference is held over the store, this can avoid the overhead of locking.

§Example
let mut store = RwStore::new();
let id = store.insert(42);
assert_eq!(store.get_mut(id), Some(&mut 42));
§Safety

The given ID must have been created by this store, see the struct-level documentation.

Source

pub unsafe fn get_mut_unchecked(&mut self, id: Id) -> &mut Element

Directly obtains a mutable reference to an element given its ID, assuming it is still present in the store.

Because a mutable reference is held over the store, this can avoid the overhead of locking.

§Example
let mut store = RwStore::new();
let id = store.insert(42);

unsafe {
    assert_eq!(store.get_mut_unchecked(id), &mut 42);
}
§Safety

If the element whose ID is passed to this method has been removed, then this may cause undefined behavior.

The given ID must have been created by this store, see the struct-level documentation.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, Element>

Creates an iterator that visits each element still present in the store, yielding its ID and a mutable reference to it.

The order in which this iterator traverses elements is unspecified.

§Example
let mut store = RwStore::new();
let id = store.insert(42);
let mut iter = store.iter_mut();
assert_eq!(iter.next().unwrap().1, &mut 42);
assert!(iter.next().is_none());
Source

pub fn capacity(&self) -> (u32, u32)

Determines the touched and allocated capacity for this store, and returns them in that order. These should be regarded as hints, if the store is being accessed concurrently the actual capacity values may be larger (but not smaller) than those returned by this method.

The touched capacity is equal to the largest number of elements ever contained in this store at the same time. The allocated capacity is the total number of element slots allocated for use with this store. Neither of these numbers ever decrease.

§Example
let store = RwStore::new();
assert_eq!(store.capacity(), (0, 0));

store.insert(42);
let (touched, allocated) = store.capacity();
assert_eq!(touched, 1);
assert!(allocated >= 1);

Trait Implementations§

Source§

impl<Element> Default for RwStore<Element>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<Element> IntoIterator for RwStore<Element>

Source§

fn into_iter(self) -> IntoIter<Element>

Creates a consuming iterator that visits each element still present in the store, yielding its ID and value.

The order in which this iterator traverses elements is unspecified.

Source§

type Item = (Id, Element)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<Element>

Which kind of iterator are we turning this into?
Source§

impl<Element: RefUnwindSafe> RefUnwindSafe for RwStore<Element>

Source§

impl<Element: Send> Send for RwStore<Element>

Source§

impl<Element: Send + Sync> Sync for RwStore<Element>

Source§

impl<Element: UnwindSafe> UnwindSafe for RwStore<Element>

Auto Trait Implementations§

§

impl<Element> !Freeze for RwStore<Element>

§

impl<Element> Unpin for RwStore<Element>

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.