Skip to content

Commit 1b7edcf

Browse files
work on collection type sizing
1 parent e526fcd commit 1b7edcf

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

amqp-lib/src/types/amqp_type.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ impl Encoded {
2525
pub fn constructor(&self) -> u8 {
2626
self.constructor
2727
}
28+
29+
pub fn data_len(&self) -> usize {
30+
match &self.data {
31+
Some(data) => data.len().into(),
32+
None => 0
33+
}
34+
}
2835
}
2936

3037

amqp-lib/src/types/collection.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ pub struct Map(IndexMap<AmqpType, AmqpType>);
1414

1515
impl Encode for List {
1616
fn encode(&self) -> Encoded {
17-
let size = std::mem::size_of_val(self);
18-
let len = self.0.len();
19-
match (len, size) {
17+
let encoded_list: Vec<Encoded> = self.0.iter()
18+
.map(|x| x.encode())
19+
.collect();
20+
let byte_size = encoded_list.iter()
21+
.fold(0, |acc, x| acc + x.data_len());
22+
match (encoded_list.len(), byte_size) {
2023
(0, _) => 0x45.into(),
2124
(len, size) if len <= 255 && size < 256 => 0xc0.into(),
2225
(_, _) => 0xd0.into()

0 commit comments

Comments
 (0)