Skip to main content

IdMap

Struct IdMap 

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

极简版 IdMap:自动生成递增 Id + HashMap 存储 + 无条件编译

Implementations§

Source§

impl<V> IdMap<DefaultId, V>

Source

pub fn new() -> Self

创建空的 IdMap(初始 max_id = 0)

Source

pub fn with_capacity(capacity: usize) -> Self

创建指定初始容量的 IdMap

Source§

impl<K: Id, V> IdMap<K, V>

Source

pub fn with_id() -> Self

为自定义 Id 类型创建空 IdMap

Source

pub fn with_id_capacity(capacity: usize) -> Self

自定义 Id 类型创建指定初始容量的 IdMap

Source

pub fn insert(&mut self, value: V) -> K

插入值,自动生成递增 Id 并返回

Source

pub fn insert_with_id(&mut self, id: K, value: V) -> Option<V>

【手动指定 Id】插入键值对,返回旧值(若存在)

注意:若手动传入的 Id 大于当前 max_id,会更新 max_id 以保证自动生成的 Id 不重复

Source

pub fn from_vec(values: Vec<V>) -> (Self, Vec<K>)

从 Vec 批量插入值,自动生成递增 Id,返回对应的 Id 列表 生成的 Id 从当前 max_id + 1 开始连续递增

Source

pub fn insert_cyclic<F>(&mut self, f: F) -> K
where F: FnOnce(K) -> V,

循环插入:先生成递增 Id,再通过闭包(Id → V)生成值并插入 适用于值需要依赖自身 Id 的场景(如循环引用/关联 Id 的场景)

Source

pub fn get(&self, id: K) -> Option<&V>

根据 Id 查询值

Source

pub fn get_mut(&mut self, id: K) -> Option<&mut V>

根据 Id 查询可变值

Source

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

根据 Id 删除值

Source

pub fn contains_id(&self, id: K) -> bool

判断是否包含指定 Id

Source

pub fn max_id(&self) -> K

获取当前最大 Id(仅用于参考,删除 Id 后不会回退)

Source

pub fn len(&self) -> usize

获取元素数量

Source

pub fn is_empty(&self) -> bool

判断是否为空

Source

pub fn clear(&mut self)

清空所有元素(保留 max_id 不变,避免 Id 重复)

Trait Implementations§

Source§

impl<K: Clone + Id, V: Clone> Clone for IdMap<K, V>

Source§

fn clone(&self) -> IdMap<K, V>

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: Debug + Id, V: Debug> Debug for IdMap<K, V>

Source§

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

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

impl<K: Id, V> Default for IdMap<K, V>

Source§

fn default() -> Self

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

impl<'de, K: Id, V> Deserialize<'de> for IdMap<K, V>
where 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: Id, V> Index<K> for IdMap<K, V>

Source§

type Output = V

The returned type after indexing.
Source§

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

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

impl<K: Id, V> IndexMut<K> for IdMap<K, V>

Source§

fn index_mut(&mut self, id: K) -> &mut Self::Output

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

impl<K: Id, V> Serialize for IdMap<K, V>
where 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

Auto Trait Implementations§

§

impl<K, V> Freeze for IdMap<K, V>

§

impl<K, V> RefUnwindSafe for IdMap<K, V>

§

impl<K, V> Send for IdMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for IdMap<K, V>
where K: Sync, V: Sync,

§

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

§

impl<K, V> UnsafeUnpin for IdMap<K, V>

§

impl<K, V> UnwindSafe for IdMap<K, V>
where V: UnwindSafe, K: 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> 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>,