Crates.io | mut_set |
lib.rs | mut_set |
version | 0.9.0 |
created_at | 2024-03-25 23:27:03.219252+00 |
updated_at | 2025-06-22 19:41:26.630096+00 |
description | A safe implementation for HashSet with iter_mut and get_mut |
homepage | |
repository | https://github.com/zao111222333/mut_set |
max_upload_size | |
id | 1185816 |
size | 13,130 |
Use the idea of readonly to implement HashSet
& IndexMap
with iter_mut
and get_mut
.
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);
}