Crates.io | armc |
lib.rs | armc |
version | 1.4.5 |
source | src |
created_at | 2022-11-24 19:44:14.748247 |
updated_at | 2023-04-26 16:16:35.600448 |
description | Armc is a rust implementation that facilitates Mutex access to variables. |
homepage | |
repository | https://github.com/patrickfp93/armc/ |
max_upload_size | |
id | 722364 |
size | 18,853 |
Armc is a Rust library that provides a wrapper for shared data, ensuring data integrity and thread-blocking during modifications and reads.
You can add the Armc dependency to your Cargo.toml file:
[dependencies]
armc = "1.4.4"
To use the library, simply import it with the following code:
use armc::Armc;
Below are some of the library's features:
To create an Armc object, simply use the new method and pass the data you want to store:
let armc = Armc::new(5);
Accessing the data of an Armc object You can access the stored data by blocking possible mutations. Multiple accesses can be done in parallel.
let data = armc.lock_ref();
println!("Data: {:?}", data);
Modifying data of an Armc object To modify the data of an Armc object, you need to use the lock method, which will block all mutation accesses:
let mut data = armc.lock();
*data = 10;
println!("Data: {:?}", data);
You can clone an Armc object using the clone method:
let armc_clone = armc.clone();
println!("Data: {:?}", *armc_clone.lock_ref());
object!
and its derivativesOne set of macros that might be useful for Rust programmers are the object!
, object_with_new!
, object_ref_access!
, and object_mut_access!
macros. These macros are designed to simplify the creation of structs with thread-safe access to their fields.
The object!
macro creates a struct with fields wrapped in an ARMC (Atomic Reference-Counting Mutex) to allow thread-safe mutation. The macro retains the name of the macro for the name of the struct.
The object_with_new!
macro is similar to the object!
macro, but it also generates a constructor method named new
that takes in the initial values for each field.
The object_ref_access!
macro is similar to the object_with_new!
macro, but it also generates getter methods for each field.
The object_mut_access!
macro is similar to the object_ref_access!
macro, but it also generates mutable setter methods for each field with the suffix _mut
.
Attention! you need a crate paste
dependency in your project for object_mut_access to work.
[dependencies]
paste = "1.0.12"
These macros are designed to save time and effort when creating structs with thread-safe access to their fields, and can be used in the following way:
#[macro_use]
extern crate my_crate;
object_mut_access!(MyStruct {
foo: u32,
bar: String,
});
let mut my_struct = MyStruct::new(42, "hello".to_string());
assert_eq!(*my_struct.foo(), 42);
my_struct.foo_mut(13);
assert_eq!(*my_struct.foo(), 13);
For more information on the implementation of these macros, see the documentation for each individual macro.
Contributions are welcome! Feel free to open an issue or submit a pull request.