SyncHashMap

Struct SyncHashMap 

Source
pub struct SyncHashMap<K, V> { /* private fields */ }
Expand description

同步哈希映射,支持多读少写的场景 this sync map used to many reader, writer less. space-for-time strategy

Implementations§

Source§

impl<K, V> SyncHashMap<K, V>
where K: Eq + Hash,

Source

pub fn new_arc() -> Arc<Self>

创建一个新的Arc包装的SyncHashMap

Source

pub fn new() -> Self

创建一个新的空映射

Source

pub fn with_capacity(capacity: usize) -> Self

使用指定容量创建一个新的映射

Source

pub const fn with_map(map: HashMap<K, V>) -> Self

使用现有的HashMap创建一个同步映射

Source

pub fn insert(&self, k: K, v: V) -> Option<V>

插入键值对,使用写锁保护

Source

pub fn insert_mut(&mut self, k: K, v: V) -> Option<V>

可变引用下的插入操作,无锁保护(调用者需确保线程安全)

Source

pub fn remove(&self, k: &K) -> Option<V>

移除键值对,使用写锁保护

Source

pub fn remove_mut(&mut self, k: &K) -> Option<V>

可变引用下的移除操作,无锁保护(调用者需确保线程安全)

Source

pub fn len(&self) -> usize

获取映射长度,使用读锁保护

Source

pub fn is_empty(&self) -> bool

判断映射是否为空,使用读锁保护

Source

pub fn clear(&self)

清空映射,使用写锁保护

Source

pub fn clear_mut(&mut self)

可变引用下的清空操作,无锁保护(调用者需确保线程安全)

Source

pub fn shrink_to_fit(&self)

收缩容量以适应元素数量,使用写锁保护

Source

pub fn shrink_to_fit_mut(&mut self)

可变引用下的收缩容量操作,无锁保护(调用者需确保线程安全)

Source

pub fn from(map: HashMap<K, V>) -> Self

从HashMap创建SyncHashMap的便捷方法

Source

pub fn get<Q>(&self, k: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

获取键对应的值引用,使用读锁保护

键可以是映射键类型的任何借用形式,但借用形式的 HashEq 必须与键类型匹配。

§Examples
use fast_able::SyncHashMap;

let map = SyncHashMap::new();
map.insert(1, "a");
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);
Source

pub fn get_mut<Q>(&self, k: &Q) -> Option<SyncMapRefMut<'_, K, V>>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

获取键对应的可变值引用,使用写锁保护

Source

pub fn contains_key<Q>(&self, k: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

检查是否包含指定的键,使用读锁保护

Source

pub fn iter_mut(&self) -> IterMut<'_, K, V>

获取可变迭代器,使用写锁保护

Source

pub fn iter(&self) -> MapIter<'_, K, V>

获取只读迭代器,使用读锁保护

Source

pub fn dirty_ref(&self) -> &HashMap<K, V>

获取底层HashMap的引用(unsafe,调用者需确保无其他写操作)

Source

pub fn into_inner(self) -> HashMap<K, V>

消耗self并返回内部的HashMap

Source

pub fn keys(&self) -> Keys<'_, K, V>

获取键的迭代器,使用读锁保护

Source

pub fn values(&self) -> Values<'_, K, V>

获取值的只读迭代器,使用读锁保护

Source

pub fn values_mut(&self) -> ValuesMut<'_, K, V>

获取值的可变迭代器,使用写锁保护

Source

pub fn retain<F>(&self, f: F)
where F: FnMut(&K, &mut V) -> bool,

保留满足条件的元素,使用写锁保护

Source

pub fn capacity(&self) -> usize

获取映射的容量,使用读锁保护

Source

pub fn reserve(&self, additional: usize)

预留指定容量,使用写锁保护

Source

pub fn remove_entry(&self, k: &K) -> Option<(K, V)>

尝试移除键值对,如果键存在则返回被移除的值,使用写锁保护

Source

pub fn get_or_insert(&self, k: K, default: V) -> &mut V

如果键存在则返回其可变引用,否则插入新值,使用写锁保护

Source

pub fn get_or_insert_with<F>(&self, k: K, default: F) -> &mut V
where F: FnOnce() -> V,

如果键存在则返回其可变引用,否则使用函数返回值插入,使用写锁保护

Trait Implementations§

Source§

impl<K, V> Clone for SyncHashMap<K, V>
where K: Clone + Eq + Hash, V: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K, V> Debug for SyncHashMap<K, V>
where K: Eq + Hash + Debug, V: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K, V> Default for SyncHashMap<K, V>
where K: Eq + Hash,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de, K, V> Deserialize<'de> for SyncHashMap<K, V>
where K: Eq + Hash + Deserialize<'de>, V: Deserialize<'de>,

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<K, V> From<HashMap<K, V>> for SyncHashMap<K, V>
where K: Eq + Hash,

Source§

fn from(map: HashMap<K, V>) -> Self

Converts to this type from the input type.
Source§

impl<K, V> Index<&K> for SyncHashMap<K, V>
where K: Eq + Hash,

索引操作,无锁访问(unsafe但在单线程场景下高效)

Source§

type Output = V

The returned type after indexing.
Source§

fn index(&self, index: &K) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, K, V> IntoIterator for &'a SyncHashMap<K, V>
where K: Eq + Hash,

Source§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K, V> IntoIterator for SyncHashMap<K, V>
where K: Eq + Hash,

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K, V> PartialEq for SyncHashMap<K, V>
where K: Eq + Hash, V: PartialEq,

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<K, V> Serialize for SyncHashMap<K, V>
where K: Eq + Hash + Serialize, V: Serialize,

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<K, V> Eq for SyncHashMap<K, V>
where K: Eq + Hash, V: Eq,

Source§

impl<K, V> Send for SyncHashMap<K, V>

标记为Send,因为使用了UnsafeCell但有锁保护 this is safety, dirty mutex ensure

Source§

impl<K, V> Sync for SyncHashMap<K, V>

标记为Sync,因为使用了UnsafeCell但有锁保护 this is safety, dirty mutex ensure

Auto Trait Implementations§

§

impl<K, V> !Freeze for SyncHashMap<K, V>

§

impl<K, V> !RefUnwindSafe for SyncHashMap<K, V>

§

impl<K, V> Unpin for SyncHashMap<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for SyncHashMap<K, V>
where K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,