shared-type 1.0.0

Shared type alias and several traits to simplify working with Arc<Mutex<T>>.
Documentation
  • Coverage
  • 37.5%
    3 out of 8 items documented0 out of 0 items with examples
  • Size
  • Source code size: 8.79 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.26 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 12s Average build duration of successful builds.
  • all releases: 12s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • an-dr/shared-type-rs
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • an-dr

shared-type

GitHub Release

Shared type alias and several traits to simplify working with Arc<Mutex<T>>

TL;DR

With the crate Without the crate
var.into_shared() Arc::new(Mutex::new(var))
let r = var.with_inner(|v|{...}) let r = var.lock().ok().map(|mut v| {...});
let r = var.try_with_inner(|v|{...}) let r = var.try_lock().ok().map(|mut v| {...});

Description

The crate provides a shared type alias Shared<T> which is a shorthand for Arc<Mutex<T>>. It also provides following traits:

  • IntoShared - To convert a value into Shared<T> (Arc<Mutex<T>>)
  • WithSharedInner - Closure to unwrap the value from Shared<T>

How to use

Add the following to your Cargo.toml:

shared-type = "1.0.0"

Code!

let vec = vec![1, 2, 3];
let vec_shared = vec.into_shared(); // Arc<Mutex<Vec<i32>>>

// The simple example of using the shared value that will wait for
// the lock to become available and then call the closure
vec_shared.with_inner(|vec| {
    vec.push(4);
});

// The same example but with the return value
let new_len = vec_shared.with_inner(|vec| {
    vec.push(5);
    vec.len()
});

// Or we can use the `try_with` to non-blocking access
let newer_len = vec_shared.try_with_inner(|vec| {
    vec.push(6);
    vec.len()
});

Find the full example in the examples directory.