3 releases

0.1.2 Jun 5, 2020
0.1.1 Jun 2, 2020
0.1.0 Jun 2, 2020

#694 in Asynchronous

24 downloads per month

MIT license

177 lines


A mutex for Rust that never deadlocks.

A Slock, or Smart Lock, is a smart wrapper around an atomically reference counted read/write lock.

All accesses and modifications are done in a contained manner. This ensures that threads will never deadlock on a Slock operation.

// Create a new lock with an initial value
let lock = Slock::new(5i32);

// Change the lock's value
lock.set(|v| v + 1).await;

// Get the lock's value
let value = lock.get().await;
// Or if the value doesn't implement copy
let value = lock.get_clone().await;

assert_eq!(value, 6);

It's also possible to extract only the data you need from larger structures without the need to clone the entire thing.

// A user struct that doesn't implement copy
struct User {
    name: String,
    age: i32,
    // ... lots of other things

let user = Slock::new(User {
    name: "Bob",
    age: 32,
    // ... lots of other things

// Get just the name
// This performs a clone on only the name
let name = user.map(|v| v.name.clone()).await;

// Get just the age
// Extracts only the age, leaving everything else untouched
let age = user.map(|v| v.age).await;


~55K SLoC