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>
impl<T> ConcurrentElement<T>
Sourcepub fn cloned(&self) -> Twhere
T: Clone,
pub fn cloned(&self) -> Twhere
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());
Sourcepub fn copied(&self) -> Twhere
T: Copy,
pub fn copied(&self) -> Twhere
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);
Sourcepub fn map<F, U>(&self, f: F) -> U
pub fn map<F, U>(&self, f: F) -> 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);
Sourcepub fn replace(&self, value: T) -> T
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']);
Sourcepub fn update<F>(&self, f: F)
pub fn update<F>(&self, f: F)
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>
impl<T: Debug> Debug for ConcurrentElement<T>
Source§impl<T> From<ConcurrentOption<T>> for ConcurrentElement<T>
impl<T> From<ConcurrentOption<T>> for ConcurrentElement<T>
Source§fn from(value: ConcurrentOption<T>) -> Self
fn from(value: ConcurrentOption<T>) -> Self
Converts to this type from the input type.
Source§impl<T: PartialEq> PartialEq<T> for ConcurrentElement<T>
impl<T: PartialEq> PartialEq<T> for ConcurrentElement<T>
Source§impl<T: PartialEq> PartialEq for ConcurrentElement<T>
impl<T: PartialEq> PartialEq for ConcurrentElement<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more