Crates.io | sovran-arc |
lib.rs | sovran-arc |
version | 0.1.4 |
source | src |
created_at | 2024-11-13 05:48:48.216833 |
updated_at | 2024-11-13 06:25:43.696035 |
description | Memory management swift-isms brought to Rust |
homepage | |
repository | https://github.com/sovran-rs/sovran-arc |
max_upload_size | |
id | 1446060 |
size | 32,097 |
This library provides convenient wrapper types that combine Arc
and Mutex
for safe shared mutable access across threads, drawing inspiration from Swift's reference counting and memory management patterns.
A thread-safe wrapper combining Arc
and Mutex
for shared mutable access to a value:
let counter = Arcm::new(0);
counter.modify(|n| *n += 1);
assert_eq!(counter.value(), 1);
Key features:
Default
Debug
, Clone
, and From
A weak reference version of Arcm
that doesn't prevent deallocation:
let strong = Arcm::new(42);
let weak = strong.downgrade();
// Access if still alive
if let Some(value) = weak.value() {
println!("Value still exists: {}", value);
}
Similar to Arcm
but wraps an Option<T>
, providing nullable semantics:
let value = Arcmo::some(42);
assert_eq!(value.value(), Some(42));
value.take(); // Remove the value
assert!(value.is_none());
value.replace(100); // Set a new value
assert_eq!(value.value(), Some(100));
Key features:
Arcm
take()
and replace()
is_some()
and is_none()
checksWeak reference version of Arcmo
:
let strong = Arcmo::some(42);
let weak = strong.downgrade();
assert!(weak.is_some());
assert_eq!(weak.value(), Some(42));
This library brings several Swift-like memory management features to Rust:
Reference Counting: Like Swift's strong
and weak
references, these types provide explicit reference counting with strong (Arcm
/Arcmo
) and weak (WeakArcm
/WeakArcmo
) variants.
Safe Shared Mutability: Similar to Swift's class instances, these types allow safe shared mutable access across multiple references.
Optional Value Semantics: Arcmo
provides similar semantics to Swift's optional references, allowing for nullable shared references.
Clean API: The API design focuses on ergonomics and safety, similar to Swift's emphasis on safe and expressive APIs.
All types in this library are thread-safe and can be safely shared across threads:
Mutex
Arc
// Create a shared counter
let counter = Arcm::new(0);
// Clone and share across threads
let counter2 = counter.clone();
std::thread::spawn(move || {
counter2.modify(|n| *n += 1);
});
// Modify in main thread
counter.modify(|n| *n += 1);
// Create an optional shared value
let value = Arcmo::some("Hello");
// Share with another thread
let value2 = value.clone();
std::thread::spawn(move || {
if value2.is_some() {
value2.modify(|s| *s = "World");
}
});
// Take the value if it exists
if let Some(v) = value.take() {
println!("Taken value: {}", v);
}
let strong = Arcm::new(vec![1, 2, 3]);
let weak = strong.downgrade();
// Modify through weak reference
weak.modify(|v| v.push(4));
// Check if value still exists
if let Some(vec) = weak.value() {
println!("Vector: {:?}", vec);
}
Clone
Debug
for debug formattingDefault
for default implementationCopyright 2024 Sovran.la, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.