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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use std::borrow::Cow;
use std::collections::HashMap;
use std::ops::{Deref, DerefMut};
use super::Value;
#[derive(Debug, Clone, PartialEq)]
pub struct Table<'a> {
values: HashMap<Cow<'a, str>, Value<'a>>,
}
impl Table<'static> {
pub fn new() -> Self {
Self::from_hashmap(HashMap::new())
}
}
impl<'a> Table<'a> {
pub fn from_hashmap(hashmap: HashMap<Cow<'a, str>, Value<'a>>) -> Self {
Table {
values: hashmap
}
}
pub fn with_capacity(cap: usize) -> Self {
Self::from_hashmap(HashMap::with_capacity(cap))
}
pub fn into_static(self) -> Table<'static> {
let hashmap = self.values.into_iter()
.map(|(k, v)| { (k.into_owned().into(), v.into_static()) })
.collect();
Table::from_hashmap(hashmap)
}
pub fn insert<K, V>(&mut self, key: K, value: V) -> Option<Value<'a>>
where K: Into<Cow<'a, str>>,
V: Into<Value<'a>>
{
self.values.insert(key.into(), value.into())
}
pub fn amqp_size(&self) -> usize {
self.values.iter()
.map(|(k, v)| k.len() + v.amqp_size())
.sum()
}
}
impl Default for Table<'static> {
fn default() -> Self {
Table::new()
}
}
impl<'a> Deref for Table<'a> {
type Target = HashMap<Cow<'a, str>, Value<'a>>;
fn deref(&self) -> &Self::Target {
&self.values
}
}
impl<'a> DerefMut for Table<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.values
}
}