| Crates.io | cowboy |
| lib.rs | cowboy |
| version | 0.1.14 |
| created_at | 2025-05-10 05:10:52.651097+00 |
| updated_at | 2025-05-11 03:16:07.070812+00 |
| description | Safe & convenient container for quick prototyping. |
| homepage | |
| repository | https://github.com/anchpop/cowboy |
| max_upload_size | |
| id | 1668080 |
| size | 38,505 |
A thread-safe wrapper for shared data with a focus on ergonomics.
Designed for quick prototyping, when you don't really want to think about lifetimes and just want to get something working as fast as possible.
Cowboy<T> is just a wrapper for Arc<RwLock<T>> that's designed to be as low-boilerplate as possible. It has more concise method names, and lots of trait implementations that make it work in more situations. (It implements Eq, Hash, Ord, and so on as long as the underlying type supports it).
.r() to read and .w() to write.Clone, PartialEq, Hash, etc.SHERIFF for global cowboy storage.I think we can all agree that you shouldn't use Cowboy or SHERIFF in production code, but I'm hopeful it can be useful for when you're prototyping and want the borrow checker to get out of your way. Here's how they work:
use cowboy::*;
// use `.cowboy()` on any value to get a Cowboy version of it.
let counter = 0.cowboy();
println!("Counter: {counter}");
// Cloning a cowboy gives you a pointer to the same underlying data
let counter_2 = counter.clone();
// Modify the value
*counter.w() += 1;
// Both counter and counter_2 were modified
assert_eq!(counter, counter_2);
SHERIFFuse cowboy::*;
let counter = 0.cowboy();
// You can register cowboys with the SHERIFF using any key type
SHERIFF.register("counter", counter.clone());
SHERIFF.register(42, counter.clone());
// Access from anywhere
let counter_1 = SHERIFF.get::<_, i32>("counter");
let counter_2 = SHERIFF.get::<_, i32>(42); // Note: not &42
*counter.w() += 1;
*counter_1.w() += 2;
*counter_2.w() += 3;
// All counters should have the same value since they're all
// clones of the same original counter
assert_eq!(counter_1, counter_2);
println!("Counter: {counter}");
use cowboy::*;
let counter = 0.cowboy();
counter.save("counter.json");
let new_counter = Cowboy::<i64>::load("counter.json");
println!("Counter: {counter}");
Check out the examples directory for more usage patterns:
basic.rs: Simple usageshared_data.rs: Sharing data between threadsunsafe_methods.rs: Demonstrating the unsafe methodsMIT