summaryrefslogtreecommitdiffstats
path: root/toolkit/crashreporter/client/app/src/ui/windows/twoway.rs
blob: 8a18162e08a083ca5c9856f5f44ebf9e062dbbfc (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::collections::HashMap;

/// A two-way hashmap.
#[derive(Debug)]
pub struct TwoWay<K, V> {
    forward: HashMap<K, V>,
    reverse: HashMap<V, K>,
}

impl<K, V> Default for TwoWay<K, V> {
    fn default() -> Self {
        TwoWay {
            forward: Default::default(),
            reverse: Default::default(),
        }
    }
}

impl<K: Eq + std::hash::Hash + Clone, V: Eq + std::hash::Hash + Clone> TwoWay<K, V> {
    pub fn insert(&mut self, key: K, value: V) {
        self.forward.insert(key.clone(), value.clone());
        self.reverse.insert(value, key);
    }

    pub fn forward(&self) -> &HashMap<K, V> {
        &self.forward
    }

    pub fn reverse(&self) -> &HashMap<V, K> {
        &self.reverse
    }
}