edn/intern_set.rs
1// Copyright 2016 Mozilla
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4// this file except in compliance with the License. You may obtain a copy of the
5// License at http://www.apache.org/licenses/LICENSE-2.0
6// Unless required by applicable law or agreed to in writing, software distributed
7// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8// CONDITIONS OF ANY KIND, either express or implied. See the License for the
9// specific language governing permissions and limitations under the License.
10
11use std::collections::HashSet;
12use std::hash::Hash;
13use std::ops::{Deref, DerefMut};
14
15use crate::ValueRc;
16
17/// An `InternSet` allows to "intern" some potentially large values, maintaining a single value
18/// instance owned by the `InternSet` and leaving consumers with lightweight ref-counted handles to
19/// the large owned value. This can avoid expensive clone() operations.
20///
21/// In Mentat, such large values might be strings or arbitrary [a v] pairs.
22///
23/// See https://en.wikipedia.org/wiki/String_interning for discussion.
24#[derive(Clone, Debug, Default, Eq, PartialEq)]
25pub struct InternSet<T>
26where
27 T: Eq + Hash,
28{
29 inner: HashSet<ValueRc<T>>,
30}
31
32impl<T> Deref for InternSet<T>
33where
34 T: Eq + Hash,
35{
36 type Target = HashSet<ValueRc<T>>;
37
38 fn deref(&self) -> &Self::Target {
39 &self.inner
40 }
41}
42
43impl<T> DerefMut for InternSet<T>
44where
45 T: Eq + Hash,
46{
47 fn deref_mut(&mut self) -> &mut Self::Target {
48 &mut self.inner
49 }
50}
51
52impl<T> InternSet<T>
53where
54 T: Eq + Hash,
55{
56 pub fn new() -> InternSet<T> {
57 InternSet {
58 inner: HashSet::new(),
59 }
60 }
61
62 /// Intern a value, providing a ref-counted handle to the interned value.
63 ///
64 /// ```
65 /// use edn::{InternSet, ValueRc};
66 ///
67 /// let mut s = InternSet::new();
68 ///
69 /// let one = "foo".to_string();
70 /// let two = ValueRc::new("foo".to_string());
71 ///
72 /// let out_one = s.intern(one);
73 /// assert_eq!(out_one, two);
74 /// // assert!(!&out_one.ptr_eq(&two)); // Nightly-only.
75 ///
76 /// let out_two = s.intern(two);
77 /// assert_eq!(out_one, out_two);
78 /// assert_eq!(1, s.len());
79 /// // assert!(&out_one.ptr_eq(&out_two)); // Nightly-only.
80 /// ```
81 pub fn intern<R: Into<ValueRc<T>>>(&mut self, value: R) -> ValueRc<T> {
82 let key: ValueRc<T> = value.into();
83 if self.inner.insert(key.clone()) {
84 key
85 } else {
86 self.inner.get(&key).unwrap().clone()
87 }
88 }
89}