VecRangeLock

Struct VecRangeLock 

Source
pub struct VecRangeLock<T> { /* private fields */ }
Expand description

General purpose multi-thread range lock for std::vec::Vec.

§Example

use range_lock::VecRangeLock;
use std::{sync::{Arc, Barrier}, thread};

let data = vec![10, 11, 12, 13];

let data_lock0 = Arc::new(VecRangeLock::new(data));
let data_lock1 = Arc::clone(&data_lock0);
let data_lock2 = Arc::clone(&data_lock0);

// Thread barrier, only for demonstration purposes.
let barrier0 = Arc::new(Barrier::new(2));
let barrier1 = Arc::clone(&barrier0);

thread::scope(|s| {
    s.spawn(move || {
        {
            let mut guard = data_lock0.try_lock(0..2).expect("T0: Failed to lock 0..2");
            guard[0] = 100; // Write to data[0]
        }
        barrier0.wait(); // Synchronize with second thread.
        {
            let guard = data_lock0.try_lock(2..4).expect("T0: Failed to lock 2..4");
            assert_eq!(guard[0], 200); // Read from data[2]
        }
    });

    s.spawn(move || {
        {
            let mut guard = data_lock1.try_lock(2..4).expect("T1: Failed to lock 2..4");
            guard[0] = 200; // Write to data[2]
        }
        barrier1.wait(); // Synchronize with first thread.
        {
            let guard = data_lock1.try_lock(0..2).expect("T1: Failed to lock 0..2");
            assert_eq!(guard[0], 100); // Read from data[0]
        }
    });
});

let data = Arc::try_unwrap(data_lock2).expect("Arc unwrap failed").into_inner();

assert_eq!(data, vec![100, 11, 200, 13]);

Implementations§

Source§

impl<'a, T> VecRangeLock<T>

Source

pub fn new(data: Vec<T>) -> VecRangeLock<T>

Construct a new VecRangeLock.

  • data: The data Vec to protect.
Source

pub fn data_len(&self) -> usize

Get the length (in number of elements) of the embedded Vec.

Source

pub fn into_inner(self) -> Vec<T>

Unwrap this VecRangeLock into the contained data. This method consumes self.

Source

pub fn try_lock( &'a self, range: impl RangeBounds<usize>, ) -> TryLockResult<VecRangeLockGuard<'a, T>>

Try to lock the given data range.

Trait Implementations§

Source§

impl<T: Debug> Debug for VecRangeLock<T>

Source§

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

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

impl<T> Sync for VecRangeLock<T>
where T: Send,

Auto Trait Implementations§

§

impl<T> !Freeze for VecRangeLock<T>

§

impl<T> !RefUnwindSafe for VecRangeLock<T>

§

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

§

impl<T> Unpin for VecRangeLock<T>

§

impl<T> UnwindSafe for VecRangeLock<T>
where T: RefUnwindSafe,

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.