mut_set

Crates.iomut_set
lib.rsmut_set
version0.9.0
created_at2024-03-25 23:27:03.219252+00
updated_at2025-06-22 19:41:26.630096+00
descriptionA safe implementation for HashSet with iter_mut and get_mut
homepage
repositoryhttps://github.com/zao111222333/mut_set
max_upload_size
id1185816
size13,130
Junzhuo ZHOU (zao111222333)

documentation

https://docs.rs/mut_set

README

mut_set

License github crates.io Docs

Use the idea of readonly to implement HashSet & IndexMap with iter_mut and get_mut.

Demo

use mut_set::MutSetExt;

#[inline]
const fn f64_into_hash_ord_fn(val: &f64) -> ordered_float::OrderedFloat<f64> {
    ordered_float::OrderedFloat(*val)
}

#[derive(Debug, Default, Clone)]
#[mut_set::derive::item]
pub(super) struct MyItem<T1> {
    #[id]
    pub(self) id1: usize,
    pub(crate) ctx1: T1,
    #[id(into_hash_ord_fn = f64_into_hash_ord_fn)]
    pub id2: f64,
    #[id]
    pub id3: Option<String>,
}

let mut set = indexmap::IndexSet::new();
set.insert(MyItem { id1: 2, id2: 4.2, ctx1: -1, id3: None });
set.insert(MyItem { id1: 1, id2: 3.2, ctx1: -2, id3: None });
println!("{:?}", set);
for v in set.iter() {
    println!("{:?}", v);
}
for v in set.iter_mut() {
    v.ctx1 = 0;
    println!("{:?}", v.id1);
    // In `iter_mut` IDs write will be prohibited
    // v.id1 = 0;
}
println!("{:?}", set);
println!("{:?}", set.get(&MyItemId::new(2, 4.2, None)));
set.replace(MyItem { id1: 1, id2: 3.2, ctx1: -2, id3: None });
println!("{:?}", set);
for v in set.into_iter() {
    println!("{:?}", v);
}
Commit count: 60

cargo fmt