Struct ConcurrentElement

Source
pub struct ConcurrentElement<T>(/* private fields */);
Expand description

An element of the ConcurrentVec that provides thread safe read and write methods on the value of the element.

A concurrent element can be created by using the index operator vec[i] or calling vec.get(i) or vec.iter() on a concurrent vec or slice.

Implementations§

Source§

impl<T> ConcurrentElement<T>

Source

pub fn cloned(&self) -> T
where T: Clone,

Returns a clone of value of the element.

§Examples
use orx_concurrent_vec::*;

let vec = ConcurrentVec::new();
vec.extend(["foo", "bar"].map(|x| x.to_string()));

assert_eq!(vec[0].cloned(), "foo".to_string());
assert_eq!(vec[1].cloned(), "bar".to_string());

vec[1].set("baz".to_string());
assert_eq!(vec[1].cloned(), "baz".to_string());
Source

pub fn copied(&self) -> T
where T: Copy,

Returns a copy of value of the element.

§Examples
use orx_concurrent_vec::*;

let vec = ConcurrentVec::new();
vec.extend([42, 7]);

assert_eq!(vec[0].copied(), 42);
assert_eq!(vec[1].copied(), 7);

vec[1].set(0);
assert_eq!(vec[1].copied(), 0);
Source

pub fn map<F, U>(&self, f: F) -> U
where F: FnOnce(&T) -> U,

Maps the value and returns the result of f(&element).

§Examples
use orx_concurrent_vec::*;

let vec: ConcurrentVec<_> = [0, 1, 2, 3].into_iter().collect();

let one = vec[1].map(|x| x.to_string());
assert_eq!(one, 1.to_string());

let doubles: Vec<_> = vec.iter().map(|elem| elem.map(|x| x * 2)).collect();
assert_eq!(doubles, [0, 2, 4, 6]);

let mut sum = 0;
for i in 0..vec.len() {
    vec[i].map(|x| {
        sum += x;
    });
}
assert_eq!(sum, 6);
Source

pub fn replace(&self, value: T) -> T

Replaces the current value of the element with the given value, and returns the old value.

See also set if the old value is to be omitted.

§Examples
use orx_concurrent_vec::*;

let vec = ConcurrentVec::from_iter(['a', 'b', 'c', 'd']);

let c = vec[2].replace('x');

assert_eq!(c, 'c');
assert_eq!(&vec, &['a', 'b', 'x', 'd']);
Source

pub fn set(&self, value: T)

Sets (overwrites) value of the element with the given value.

See also replace if the old value is required.

§Examples
use orx_concurrent_vec::*;

let vec = ConcurrentVec::new();
vec.extend(['a', 'b', 'c', 'd']);

vec[2].set('x');
assert_eq!(&vec, &['a', 'b', 'x', 'd']);
Source

pub fn update<F>(&self, f: F)
where F: FnMut(&mut T),

Updates the current value of the element by calling the mutating function f(&mut element).

§Examples
use orx_concurrent_vec::*;

let vec = ConcurrentVec::from_iter([0, 1, 2, 3]);

vec[1].update(|x| *x *= 2);
vec[2].update(|x| *x += 10);
vec[3].update(|x| *x = 7);

assert_eq!(&vec, &[0, 2, 12, 7]);

Trait Implementations§

Source§

impl<T: Debug> Debug for ConcurrentElement<T>

Source§

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

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

impl<T> From<ConcurrentOption<T>> for ConcurrentElement<T>

Source§

fn from(value: ConcurrentOption<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq> PartialEq<T> for ConcurrentElement<T>

Source§

fn eq(&self, other: &T) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialEq> PartialEq for ConcurrentElement<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<T> !Freeze for ConcurrentElement<T>

§

impl<T> !RefUnwindSafe for ConcurrentElement<T>

§

impl<T> Send for ConcurrentElement<T>
where T: Send,

§

impl<T> Sync for ConcurrentElement<T>
where T: Sync,

§

impl<T> Unpin for ConcurrentElement<T>
where T: Unpin,

§

impl<T> UnwindSafe for ConcurrentElement<T>
where T: UnwindSafe,

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> SoM<T> for T

Source§

fn get_ref(&self) -> &T

Returns a reference to self.
Source§

fn get_mut(&mut self) -> &mut T

Returns a mutable reference to self.
Source§

impl<T> SoR<T> for T

Source§

fn get_ref(&self) -> &T

Returns a reference to self.
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.