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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use core::borrow::Borrow;
use alloc::collections::BTreeMap;
use core::hash::Hash;
use crate::At;

#[cfg(feature="hashbrown")]
impl<Q,K,V> At<&Q> for hashbrown::HashMap<K,V> where
    K: Borrow<Q> + Eq + Hash,
    Q: ?Sized + Eq + Hash
{
    type View = V;

    fn access_at<R,F>(&mut self, i: &Q, f: F) -> Option<R> where
        F: FnOnce(&mut V) -> R
    {
        self.get_mut(i).map(|v| f(v))
    }
}

#[cfg(feature="hashbrown")]
impl<K,V> At<(K,V)> for hashbrown::HashMap<K,V> where
    K: Eq + Hash,
{
    type View = V;

    fn access_at<R,F>(&mut self, kv: (K,V), f: F) -> Option<R> where
        F: FnOnce(&mut V) -> R
    {
        Some(f(self.entry(kv.0).or_insert(kv.1)))
    }
}

#[cfg(feature="hashbrown")]
impl<K,V,M> At<(K,V,M)> for hashbrown::HashMap<K,V> where
    K: Eq + Hash,
    M: FnOnce(&mut V)
{
    type View = V;

    fn access_at<R,F>(&mut self, kvm: (K,V,M), f: F) -> Option<R> where
        F: FnOnce(&mut V) -> R
    {
        Some(f(self.entry(kvm.0).and_modify(kvm.2).or_insert(kvm.1)))
    }
}


#[cfg(feature="std_hashmap")]
extern crate std;


#[cfg(feature="std_hashmap")]
impl<Q,K,V> At<&Q> for std::collections::HashMap<K,V> where
    K: Borrow<Q> + Eq + Hash,
    Q: ?Sized + Eq + Hash
{
    type View = V;

    fn access_at<R,F>(&mut self, i: &Q, f: F) -> Option<R> where
        F: FnOnce(&mut V) -> R
    {
        self.get_mut(i).map(|v| f(v))
    }
}

#[cfg(feature="std_hashmap")]
impl<K,V> At<(K,V)> for std::collections::HashMap<K,V> where
    K: Eq + Hash,
{
    type View = V;

    fn access_at<R,F>(&mut self, kv: (K,V), f: F) -> Option<R> where
        F: FnOnce(&mut V) -> R
    {
        Some(f(self.entry(kv.0).or_insert(kv.1)))
    }
}

#[cfg(feature="std_hashmap")]
impl<K,V,M> At<(K,V,M)> for std::collections::HashMap<K,V> where
    K: Eq + Hash,
    M: FnOnce(&mut V)
{
    type View = V;

    fn access_at<R,F>(&mut self, kvm: (K,V,M), f: F) -> Option<R> where
        F: FnOnce(&mut V) -> R
    {
        Some(f(self.entry(kvm.0).and_modify(kvm.2).or_insert(kvm.1)))
    }
}






impl<Q,K,V> At<&Q> for BTreeMap<K,V> where
    K: Borrow<Q> + Ord,
    Q: ?Sized + Ord
{
    type View = V;

    fn access_at<R,F>(&mut self, i: &Q, f: F) -> Option<R> where
        F: FnOnce(&mut V) -> R
    {
        self.get_mut(i).map(|v| f(v))
    }
}


/* EDIT-ACCESSOR: WIP
impl<Q,K,V> At<Option<&Q>> for BTreeMap<K,V> where
    K: Borrow<Q> + Ord /* FIXME: remove Clone when remove_entry stabilizes */ + Clone,
    Q: ?Sized + Ord
{
    type View = Option<V>;

    fn access_at<R,F>(&mut self, maybe_i: Option<&Q>, f: F) -> Option<R> where
        F: FnOnce(&mut Option<V>) -> R
    {
        maybe_i.map(|i| {
            if let Some( (k,_) ) = self.get_key_value(i) {
                let k = k.clone();
                let v = self.remove(i).unwrap();

                let mut cell = Some(v);
                
                let result = f(&mut cell);

                if let Some(new_v) = cell {
                    self.insert(k, new_v);
                }

                Some(result)
            } else { None }
        }).flatten()

        /* UNSTABLE (rustc v1.44)
        maybe_i.map(|i| {
            self.remove_entry(i).map(|(k,v)| {
                let mut cell = Some(v);

                let result = f(&mut cell);

                if let Some(new_v) = cell {
                    self.insert(k, new_v);
                }

                result
            })
        }).flatten() */
    }
}*/

impl<K,V> At<(K,V)> for BTreeMap<K,V> where
    K: Ord,
{
    type View = V;

    fn access_at<R,F>(&mut self, kv: (K,V), f: F) -> Option<R> where
        F: FnOnce(&mut V) -> R
    {
        Some(f(self.entry(kv.0).or_insert(kv.1)))
    }
}

impl<K,V,M> At<(K,V,M)> for BTreeMap<K,V> where
    K: Ord,
    M: FnOnce(&mut V)
{
    type View = V;

    fn access_at<R,F>(&mut self, kvm: (K,V,M), f: F) -> Option<R> where
        F: FnOnce(&mut V) -> R
    {
        Some(f(self.entry(kvm.0).and_modify(kvm.2).or_insert(kvm.1)))
    }
}