Struct with_lock::MutexCell [−][src]
pub struct MutexCell<T> { /* fields omitted */ }
Implementations
Construct a new Mutex.
What is going on
This function creates a new Mutex
where data (a field only visible in this crate) is a WithLock
It then constructs the standard library’s Mutex, which is then wrapped around by the WithLock
.
The only change from using the standard library’s Mutex is that all cases of .lock().unwrap()
are handled for you,
so you don’t need to call .unwrap() after calling .lock().
Example
use with_lock::MutexCell;
let mutex = MutexCell::new(23);
assert_eq!(mutex.get(), 23)
Examples found in repository
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
let mut a = MutexCell::new(2);
let b = MutexCell::new(3);
println!("{:?}", a.get() + b.get()); // 5
b.set(4);
println!("{:?}", a.get() + b.get()); // 6
a.swap(&b);
println!("A: {:?} B: {:?}", a.get(), b.get()); // A: 4 B: 2
a.replace(4);
println!("{:?}", a.get() + b.get()); // 8
let cell = a.get_mut();
*cell += 1;
println!("{:?}", a.get()); // 5
let a_new = a.take();
println!("A: {:?}", a.into_inner());
println!("A_NEW: {:?}", a_new);
}
The get function. It gets the value inside the mutex.
What is going on
Locks the mutex and retrieves the value, then unlocks the mutex.
Examples found in repository
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
let mut a = MutexCell::new(2);
let b = MutexCell::new(3);
println!("{:?}", a.get() + b.get()); // 5
b.set(4);
println!("{:?}", a.get() + b.get()); // 6
a.swap(&b);
println!("A: {:?} B: {:?}", a.get(), b.get()); // A: 4 B: 2
a.replace(4);
println!("{:?}", a.get() + b.get()); // 8
let cell = a.get_mut();
*cell += 1;
println!("{:?}", a.get()); // 5
let a_new = a.take();
println!("A: {:?}", a.into_inner());
println!("A_NEW: {:?}", a_new);
}
The get_mut function. It gets the value inside the mutex and returns it as mutable.
What is going on
Locks the mutex and retrieves the value, then unlocks the mutex.
Examples found in repository
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
let mut a = MutexCell::new(2);
let b = MutexCell::new(3);
println!("{:?}", a.get() + b.get()); // 5
b.set(4);
println!("{:?}", a.get() + b.get()); // 6
a.swap(&b);
println!("A: {:?} B: {:?}", a.get(), b.get()); // A: 4 B: 2
a.replace(4);
println!("{:?}", a.get() + b.get()); // 8
let cell = a.get_mut();
*cell += 1;
println!("{:?}", a.get()); // 5
let a_new = a.take();
println!("A: {:?}", a.into_inner());
println!("A_NEW: {:?}", a_new);
}
The set function. It sets the value inside the mutex.
What is going on
Locks the mutex and updates the value, then unlocks the mutex.
Examples found in repository
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
let mut a = MutexCell::new(2);
let b = MutexCell::new(3);
println!("{:?}", a.get() + b.get()); // 5
b.set(4);
println!("{:?}", a.get() + b.get()); // 6
a.swap(&b);
println!("A: {:?} B: {:?}", a.get(), b.get()); // A: 4 B: 2
a.replace(4);
println!("{:?}", a.get() + b.get()); // 8
let cell = a.get_mut();
*cell += 1;
println!("{:?}", a.get()); // 5
let a_new = a.take();
println!("A: {:?}", a.into_inner());
println!("A_NEW: {:?}", a_new);
}
The replace function. It replaces the value inside the mutex and returns the previous value.
What is going on
Locks this cell, and then calls mem::replace
on the locked value.
Examples found in repository
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
let mut a = MutexCell::new(2);
let b = MutexCell::new(3);
println!("{:?}", a.get() + b.get()); // 5
b.set(4);
println!("{:?}", a.get() + b.get()); // 6
a.swap(&b);
println!("A: {:?} B: {:?}", a.get(), b.get()); // A: 4 B: 2
a.replace(4);
println!("{:?}", a.get() + b.get()); // 8
let cell = a.get_mut();
*cell += 1;
println!("{:?}", a.get()); // 5
let a_new = a.take();
println!("A: {:?}", a.into_inner());
println!("A_NEW: {:?}", a_new);
}
The swap function. It swaps the value of one MutexCell with another.
What is going on
Locks this cell, then locks new
. Then, we swap the data using mem::swap
.
Examples found in repository
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
let mut a = MutexCell::new(2);
let b = MutexCell::new(3);
println!("{:?}", a.get() + b.get()); // 5
b.set(4);
println!("{:?}", a.get() + b.get()); // 6
a.swap(&b);
println!("A: {:?} B: {:?}", a.get(), b.get()); // A: 4 B: 2
a.replace(4);
println!("{:?}", a.get() + b.get()); // 8
let cell = a.get_mut();
*cell += 1;
println!("{:?}", a.get()); // 5
let a_new = a.take();
println!("A: {:?}", a.into_inner());
println!("A_NEW: {:?}", a_new);
}
The take function. It takes the value from the Mutex, returns it and sets the value to Default::default()
What is going on
Calls the replace
function, and then sets it to Default::default()
.
Examples found in repository
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
let mut a = MutexCell::new(2);
let b = MutexCell::new(3);
println!("{:?}", a.get() + b.get()); // 5
b.set(4);
println!("{:?}", a.get() + b.get()); // 6
a.swap(&b);
println!("A: {:?} B: {:?}", a.get(), b.get()); // A: 4 B: 2
a.replace(4);
println!("{:?}", a.get() + b.get()); // 8
let cell = a.get_mut();
*cell += 1;
println!("{:?}", a.get()); // 5
let a_new = a.take();
println!("A: {:?}", a.into_inner());
println!("A_NEW: {:?}", a_new);
}
The into_inner function. It takes the Mutex and calls into_inner
on it.
What is going on
It takes the Mutex and calls into_inner
on it.
Examples found in repository
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() {
let mut a = MutexCell::new(2);
let b = MutexCell::new(3);
println!("{:?}", a.get() + b.get()); // 5
b.set(4);
println!("{:?}", a.get() + b.get()); // 6
a.swap(&b);
println!("A: {:?} B: {:?}", a.get(), b.get()); // A: 4 B: 2
a.replace(4);
println!("{:?}", a.get() + b.get()); // 8
let cell = a.get_mut();
*cell += 1;
println!("{:?}", a.get()); // 5
let a_new = a.take();
println!("A: {:?}", a.into_inner());
println!("A_NEW: {:?}", a_new);
}