From 6b3d8acdf183e67506ec44001d920c4edd9f4b5e Mon Sep 17 00:00:00 2001 From: Milo Turner Date: Tue, 10 Mar 2020 18:34:55 -0400 Subject: [PATCH] change ack msg to store as array --- hptp/src/msg.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/hptp/src/msg.rs b/hptp/src/msg.rs index 99cfa38..4eab760 100644 --- a/hptp/src/msg.rs +++ b/hptp/src/msg.rs @@ -1,7 +1,6 @@ use super::seg::SegIdx; pub use super::seg::{DOWN_HEADER_SIZE, MAX_TOTAL_PACKET_SIZE, UP_HEADER_SIZE}; use byteorder::ByteOrder; -use std::collections::HashSet; #[derive(Clone, Debug)] pub enum UpMsg { @@ -14,7 +13,8 @@ pub enum UpMsg { #[derive(Clone, Debug)] pub enum DownMsg { - Ack { idxs: HashSet }, + /// `idxs` must be distinct and in increasing order. + Ack { idxs: Vec }, } #[derive(Error, Debug)] @@ -56,19 +56,29 @@ impl SerDes for UpMsg { impl SerDes for DownMsg { fn des(buf: &[u8]) -> Result { - let mut idxs = HashSet::new(); + let mut idxs = vec![]; for (i, b) in buf.iter().cloned().enumerate() { for j in 0..8 { if b & (1 << j) != 0 { - idxs.insert((i * 8 + j) as SegIdx); + idxs.push((i * 8 + j) as SegIdx); } } } Ok(DownMsg::Ack { idxs }) } - fn ser_to(&self, _buf: &mut [u8]) -> usize { - // TODO: implement this! - 0 + fn ser_to(&self, buf: &mut [u8]) -> usize { + match self { + DownMsg::Ack { idxs } => { + let mut max_i = 0; + for seg_idx in idxs.iter().cloned() { + let i = seg_idx as usize / 8; + let j = (seg_idx % 8) as u8; + buf[i] |= 1 << j; + max_i = std::cmp::max(i, max_i); + } + max_i + 1 + } + } } }