torrust_tracker_contrib_bencode/access/
bencode.rs1use crate::access::dict::BDictAccess;
2use crate::access::list::BListAccess;
3
4pub enum RefKind<'a, K, V> {
6 Int(i64),
8 Bytes(&'a [u8]),
10 List(&'a dyn BListAccess<V>),
12 Dict(&'a dyn BDictAccess<K, V>),
14}
15
16pub trait BRefAccess: Sized {
18 type BKey;
19 type BType: BRefAccess<BKey = Self::BKey>;
20
21 fn kind(&self) -> RefKind<'_, Self::BKey, Self::BType>;
23
24 fn str(&self) -> Option<&str>;
26
27 fn int(&self) -> Option<i64>;
29
30 fn bytes(&self) -> Option<&[u8]>;
32
33 fn list(&self) -> Option<&dyn BListAccess<Self::BType>>;
35
36 fn dict(&self) -> Option<&dyn BDictAccess<Self::BKey, Self::BType>>;
38}
39
40pub trait BRefAccessExt<'a>: BRefAccess {
46 fn str_ext(&self) -> Option<&'a str>;
48
49 fn bytes_ext(&self) -> Option<&'a [u8]>;
51}
52
53impl<'a, T> BRefAccess for &'a T
54where
55 T: BRefAccess,
56{
57 type BKey = T::BKey;
58 type BType = T::BType;
59
60 fn kind(&self) -> RefKind<'_, Self::BKey, Self::BType> {
61 (*self).kind()
62 }
63
64 fn str(&self) -> Option<&str> {
65 (*self).str()
66 }
67
68 fn int(&self) -> Option<i64> {
69 (*self).int()
70 }
71
72 fn bytes(&self) -> Option<&[u8]> {
73 (*self).bytes()
74 }
75
76 fn list(&self) -> Option<&dyn BListAccess<Self::BType>> {
77 (*self).list()
78 }
79
80 fn dict(&self) -> Option<&dyn BDictAccess<Self::BKey, Self::BType>> {
81 (*self).dict()
82 }
83}
84
85impl<'a: 'b, 'b, T> BRefAccessExt<'a> for &'b T
86where
87 T: BRefAccessExt<'a>,
88{
89 fn str_ext(&self) -> Option<&'a str> {
90 (*self).str_ext()
91 }
92
93 fn bytes_ext(&self) -> Option<&'a [u8]> {
94 (*self).bytes_ext()
95 }
96}
97
98pub enum MutKind<'a, K, V> {
100 Int(i64),
102 Bytes(&'a [u8]),
104 List(&'a mut dyn BListAccess<V>),
106 Dict(&'a mut dyn BDictAccess<K, V>),
108}
109
110pub trait BMutAccess: Sized + BRefAccess {
112 fn kind_mut(&mut self) -> MutKind<'_, Self::BKey, Self::BType>;
114
115 fn list_mut(&mut self) -> Option<&mut dyn BListAccess<Self::BType>>;
117
118 fn dict_mut(&mut self) -> Option<&mut dyn BDictAccess<Self::BKey, Self::BType>>;
120}