tag | a439273099f8181307e9d1878af69746721dffe3 | |
---|---|---|
tagger | The Android Open Source Project <initial-contribution@android.com> | Thu May 19 16:14:54 2022 -0700 |
object | 71b9a6464eaf9397bde1253103b0e002a1add8e0 |
Platform Tools Release 33.0.0 (8141338)
commit | 71b9a6464eaf9397bde1253103b0e002a1add8e0 | [log] [tgz] |
---|---|---|
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | Thu Dec 16 00:04:37 2021 +0000 |
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | Thu Dec 16 00:04:37 2021 +0000 |
tree | db428e02daefd369d8c754a70557de3b39686571 | |
parent | 70bc82f184783f0389a3dd5efcf36b38f1453127 [diff] | |
parent | 93d924b9bab5b33fa2d3af0a2695e55dfa43f228 [diff] |
Snap for 8005954 from 93d924b9bab5b33fa2d3af0a2695e55dfa43f228 to sdk-release Change-Id: If55af73d8f974b0c9ea480d2295a777cb5b6dec3
This crate defines several kinds of weak hash maps and sets. See the full API documentation for details.
This crate supports Rust version 1.32 and later.
Here we create a weak hash map and demonstrate that it forgets mappings whose keys expire:
use weak_table::WeakKeyHashMap; use std::sync::{Arc, Weak}; let mut table = <WeakKeyHashMap<Weak<str>, u32>>::new(); let one = Arc::<str>::from("one"); let two = Arc::<str>::from("two"); table.insert(one.clone(), 1); assert_eq!( table.get("one"), Some(&1) ); assert_eq!( table.get("two"), None ); table.insert(two.clone(), 2); *table.get_mut(&one).unwrap() += 10; assert_eq!( table.get("one"), Some(&11) ); assert_eq!( table.get("two"), Some(&2) ); drop(one); assert_eq!( table.get("one"), None ); assert_eq!( table.get("two"), Some(&2) );
Here we use a weak hash set to implement a simple string interning facility:
use weak_table::WeakHashSet; use std::ops::Deref; use std::rc::{Rc, Weak}; #[derive(Clone, Debug)] pub struct Symbol(Rc<str>); impl PartialEq for Symbol { fn eq(&self, other: &Symbol) -> bool { Rc::ptr_eq(&self.0, &other.0) } } impl Eq for Symbol {} impl Deref for Symbol { type Target = str; fn deref(&self) -> &str { &self.0 } } #[derive(Debug, Default)] pub struct SymbolTable(WeakHashSet<Weak<str>>); impl SymbolTable { pub fn new() -> Self { Self::default() } pub fn intern(&mut self, name: &str) -> Symbol { if let Some(rc) = self.0.get(name) { Symbol(rc) } else { let rc = Rc::<str>::from(name); self.0.insert(Rc::clone(&rc)); Symbol(rc) } } } #[test] fn interning() { let mut tab = SymbolTable::new(); let a0 = tab.intern("a"); let a1 = tab.intern("a"); let b = tab.intern("b"); assert_eq!(a0, a1); assert_ne!(a0, b); }