[][src]Crate sharded

Note: This crate is still in early development and undergoing API changes. Contributions, feature requests, and constructive feedback are warmly welcomed.

sharded   Build Crate

Sharded provides safe, fast, and obvious concurrent collections in Rust. This crate splits the underlying collection into N shards each with its own lock. Calling read(key) or write(key) returns a guard for a single shard.


  • Zero unsafe code. This library uses #![forbid(unsafe_code)]. There are some limitations with the raw locking API that could cause you to write a bug, but it should be hard to so!

  • Zero dependencies. By default, the library only uses std. If you'd like to pull in some community crates such as parking_lot, just use the 3rd-party feature.

  • Tiny footprint. The core logic is ~100 lines of code. This may build up over time as utilities and ergonomics are added.

  • Extremely fast. This implementation may be a more performant choice for your workload than some of the most popular concurrent hashmaps out there. ??

  • Flexible API.. Bring your own lock or collection types. sharded::Map is just a type alias for Shard<Lock<Collection<_>>>. There's support for Sets and Trees, too!

See Also

  • flurry - A port of Java's java.util.concurrent.ConcurrentHashMap to Rust. (Also part of a live stream series)
  • dashmap - Blazing fast concurrent HashMap for Rust.
  • countrie - A concurrent hash-trie map & set.

Quick Start


# Optionally use `parking_lot`, `hashbrown`, and `ahash`
# by specifing the feature "3rd-party"
sharded = { version = "0.0.1", features = ["3rd-party"] }


Use a concurrent HashMap

This example is not tested
use sharded::Map;
let concurrent = Map::new()

// or use an existing HashMap,

let users = Shard::from(users);

let guard = users.write(32);
guard.insert(32, user);


Many thanks to

  • Reddit community for a few pointers and some motivation to take this project further.

  • Jon Gjengset for the live streams and utility crates involved

  • and countless OSS contributors that made this work possible


Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in sharded by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.



The sharded lock collection. This is the main data type in the crate. See also the type aliases Map, Set, and so on.



Basic methods needing implemented for shard construction


Teases out the sharding key for example from an IntoIterator value.


Generic locking implementation.


Type Definitions


Sharded lock-based concurrent map using the crate default lock and map implementations.


Sharded lock-based concurrent set using the crate default lock and set implementations.