1use std::collections::HashMap;
2use std::fmt;
3
4
5use crate::Int;
6use crate::_String;
8use crate::Object;
10use super::Hashable;
13use super::SetItem;
14use std::hash::Hash;
15pub struct Dict<T: Sized + Eq + Hash + PartialEq> {
21 _dict: HashMap<Hashable<T>, Object>,
23}
24
25impl<T> Dict<T>
26where
27 T: Sized + Eq + Hash + PartialEq,
28{
29 pub fn new() -> Dict<T> {
31 Dict {
32 _dict: HashMap::new(),
33 }
34 }
35}
36
37impl<T> SetItem<Int<T>, _String> for Dict<T>
46where
47 T: Sized + Eq + Hash + PartialEq,
48{
49 fn set(&mut self, key: Int<T>, value: _String) {
50 self._dict.insert(Hashable::Int(key), Object::String(value));
51 }
52}
53
54#[allow(unused_must_use)]
63
64impl<T> fmt::Display for Dict<T>
65where
66 T: Sized + fmt::Display + Eq + Hash + PartialEq,
67{
68 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69 writeln!(f, "{{");
70 for (key, value) in self._dict.iter() {
71 write!(f, " {}: {}", key, value);
72 }
73 writeln!(f, "\n}}")
74 }
75}
76
77
78impl<T> Default for Dict<T>
79where
80 T: Sized + Eq + Hash + PartialEq,
81{
82 fn default() -> Self {
83 Self::new()
84 }
85}