1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
//! See more at [](https://github.com/zao111222333/mut_set)
//!
//!```
//! #[derive(Debug)]
//! #[mut_set_derive::item]
//! pub(super) struct MyItem<T1, T2>
//! where
//! T1: Sized,
//! {
//! #[id]
//! pub id1: usize,
//! pub ctx1: T1,
//! pub(in crate::derive) ctx2: T2,
//! #[id]
//! pub id2: String,
//! }
//!
//! #[test]
//! fn test() {
//! let mut set = mut_set::MutSet::new();
//! println!("{:?}", set);
//! set.insert(MyItem {
//! id1: 2,
//! id2: "www".to_string(),
//! ctx1: -1,
//! ctx2: "ccc".to_string(),
//! });
//! set.insert(MyItem {
//! id1: 1,
//! id2: "ww".to_string(),
//! ctx1: -2,
//! ctx2: "cc".to_string(),
//! });
//! 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(&MyItem::id(2, "www".to_string())));
//! set.replace(MyItem {
//! id1: 1,
//! id2: "ww".to_string(),
//! ctx1: -2,
//! ctx2: "cc".to_string(),
//! });
//! println!("{:?}", set.get(&id1));
//! for v in set.into_iter() {
//! println!("{:?}", v);
//! }
//! }
//! ```
mod set;
use std::{
collections::HashMap,
hash::{Hash, RandomState},
ops::Deref,
};
pub trait Item
where
Self: Hash + Sized,
{
type ImmutIdItem: From<Self> + Into<Self> + Deref<Target = Self>;
type Id: Hash + ?Sized;
}
/// See more at [](https://github.com/zao111222333/mut_set)
///
///```
/// #[derive(Debug)]
/// #[mut_set_derive::item]
/// pub(super) struct MyItem<T1, T2>
/// where
/// T1: Sized,
/// {
/// #[id]
/// pub id1: usize,
/// pub ctx1: T1,
/// pub(in crate::derive) ctx2: T2,
/// #[id]
/// pub id2: String,
/// }
///
/// #[test]
/// fn test() {
/// let mut set = mut_set::MutSet::new();
/// println!("{:?}", set);
/// set.insert(MyItem {
/// id1: 2,
/// id2: "www".to_string(),
/// ctx1: -1,
/// ctx2: "ccc".to_string(),
/// });
/// set.insert(MyItem {
/// id1: 1,
/// id2: "ww".to_string(),
/// ctx1: -2,
/// ctx2: "cc".to_string(),
/// });
/// 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(&MyItem::id(2, "www".to_string())));
/// set.replace(MyItem {
/// id1: 1,
/// id2: "ww".to_string(),
/// ctx1: -2,
/// ctx2: "cc".to_string(),
/// });
/// println!("{:?}", set.get(&id1));
/// for v in set.into_iter() {
/// println!("{:?}", v);
/// }
/// }
/// ```
pub struct MutSet<T: Item, S = RandomState> {
inner: HashMap<u64, T::ImmutIdItem, S>,
}