python/dict/
dict.rs

1use std::collections::HashMap;
2use std::fmt;
3
4
5use crate::Int;
6// use crate::Float;
7use crate::_String;
8// use crate::Char;
9use crate::Object;
10// use crate::_Object;
11// use crate::_Hashable;
12use super::Hashable;
13use super::SetItem;
14use std::hash::Hash;
15// use crate::_Object;
16
17
18// TODO implement hash trait for object
19/// Dict struct that represents the python dict
20pub struct Dict<T: Sized + Eq + Hash + PartialEq> {
21    /// hashmap of object and object
22    _dict: HashMap<Hashable<T>, Object>,
23}
24
25impl<T> Dict<T>
26where
27    T: Sized + Eq + Hash + PartialEq,
28{
29    /// creates a new empty python dict
30    pub fn new() -> Dict<T> {
31        Dict {
32            _dict: HashMap::new(),
33        }
34    }
35}
36
37// impl<K, V> SetItem<K, V> for Dict
38// where K: _Hashable, V: _Object + Sized
39// {
40//     fn set(&mut self, key: K, value: V) {
41//         self._dict.insert(key, value);
42//     }
43// }
44
45impl<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// impl<T> Default for Dict<T>
55// where T: Sized
56// {
57//     fn default() -> Self {
58//         Dict::new()
59//     }
60// }
61
62#[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}