From f637593336c2cc7056cec3d7a47512a65062ae8d Mon Sep 17 00:00:00 2001 From: Monica Moniot Date: Thu, 27 Jul 2023 08:08:19 -0400 Subject: [PATCH] cargo fmt --- src/lib.rs | 38 ---------------- src/seq_queue.rs | 112 +++++++++++++++++------------------------------ 2 files changed, 41 insertions(+), 109 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 444e247..a9d1d1b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,41 +1,3 @@ - - pub type SeqNum = u32; pub mod seq_queue; - -//struct App(); - -//use seq_queue::ApplicationLayer; -//impl ApplicationLayer for App { -// type RecvData = (Vec, u64); - -// type RecvDataRef<'a> = (&'a [u8], &'a [u8], u64); - -// type RecvReturn = (); - -// type SendData = Vec; - -// fn send(&self, data: &Self::SendData) { -// todo!() -// } -// fn send_ack(&self, reply_num: SeqNum) { -// todo!() -// } -// fn send_empty_reply(&self, reply_num: SeqNum) { -// todo!() -// } - -// fn deserialize<'a>(data: &'a Self::RecvData) -> Self::RecvDataRef<'a> { -// let s = data.0.split_at(1); -// (s.0, s.1, data.1) -// } -// fn process( -// &self, -// packet: Self::RecvDataRef<'_>, -// reply: seq_queue::ReplyGuard<'_, Self>, -// send_data: Option, -// ) -> Self::RecvReturn { -// todo!() -// } -//} diff --git a/src/seq_queue.rs b/src/seq_queue.rs index 53db24d..8e33f0f 100644 --- a/src/seq_queue.rs +++ b/src/seq_queue.rs @@ -1,4 +1,3 @@ - use crate::SeqNum; const INITIAL_SEQ_NUM: SeqNum = 1; @@ -17,12 +16,7 @@ pub trait ApplicationLayer: Sized { fn send_empty_reply(&self, reply_num: SeqNum); fn deserialize<'a>(data: &'a Self::RecvData) -> Self::RecvDataRef<'a>; - fn process( - &self, - packet: Self::RecvDataRef<'_>, - reply: ReplyGuard<'_, Self>, - send_data: Option, - ) -> Self::RecvReturn; + fn process(&self, packet: Self::RecvDataRef<'_>, reply: ReplyGuard<'_, Self>, send_data: Option) -> Self::RecvReturn; } pub trait IntoRecvData: Into { @@ -34,7 +28,6 @@ impl IntoRecvData for AppInner::RecvData { } } - pub struct SeqQueue { pub retry_interval: i64, next_send_seq_num: SeqNum, @@ -65,6 +58,9 @@ pub struct ReplyGuard<'a, App: ApplicationLayer> { reply_num: SeqNum, } +pub struct Iter<'a, App: ApplicationLayer>(std::slice::Iter<'a, Option>>); +pub struct IterMut<'a, App: ApplicationLayer>(std::slice::IterMut<'a, Option>>); + impl SeqQueue { pub fn new(retry_interval: i64) -> Self { Self { @@ -83,12 +79,7 @@ impl SeqQueue { /// The caller must either cancel sending, abort the connection, or wait until a call to /// `receive` returns `Some` and try again. #[must_use = "The queue might be full causing the packet to not be sent"] - pub fn send_seq( - &mut self, - app: App, - create: impl FnOnce(SeqNum) -> App::SendData, - current_time: i64, - ) -> bool { + pub fn send_seq(&mut self, app: App, create: impl FnOnce(SeqNum) -> App::SendData, current_time: i64) -> bool { let seq_num = self.next_send_seq_num; self.next_send_seq_num += 1; @@ -108,13 +99,7 @@ impl SeqQueue { true } - pub fn receive( - &mut self, - app: App, - seq_num: SeqNum, - reply_num: Option, - packet: impl IntoRecvData, - ) -> Option { + pub fn receive(&mut self, app: App, seq_num: SeqNum, reply_num: Option, packet: impl IntoRecvData) -> Option { let normalized_seq_num = seq_num.wrapping_sub(self.pre_recv_seq_num).wrapping_sub(1); let is_below_range = normalized_seq_num > SeqNum::MAX / 2; let is_above_range = !is_below_range && normalized_seq_num >= self.recv_window.len() as u32; @@ -148,17 +133,9 @@ impl SeqQueue { // This should reliably handle sequence number overflow. self.pre_recv_seq_num = seq_num; let data = reply_num.and_then(|r| self.take_send(r)); - Some(app.process( - packet.as_ref(), - ReplyGuard { app: Some(&app), seq_queue: self, reply_num: seq_num }, - data, - )) + Some(app.process(packet.as_ref(), ReplyGuard { app: Some(&app), seq_queue: self, reply_num: seq_num }, data)) } else { - self.recv_window[i] = Some(RecvEntry { - seq_num, - reply_num, - data: packet.into(), - }); + self.recv_window[i] = Some(RecvEntry { seq_num, reply_num, data: packet.into() }); if let Some(reply_num) = reply_num { self.receive_ack(reply_num); } @@ -234,41 +211,6 @@ impl SeqQueue { IterMut(self.send_window.iter_mut()) } } -macro_rules! iterator { - ($iter:ident, {$( $mut:tt )?}) => { - impl<'a, App: ApplicationLayer> Iterator for $iter<'a, App> { - type Item = &'a $($mut)? App::SendData; - fn next(&mut self) -> Option { - while let Some(entry) = self.0.next() { - if let Some(entry) = entry { - return Some(& $($mut)? entry.data) - } - } - None - } - - fn size_hint(&self) -> (usize, Option) { - (0, Some(self.0.len())) - } - } - impl<'a, App: ApplicationLayer> DoubleEndedIterator for $iter<'a, App> { - fn next_back(&mut self) -> Option { - while let Some(entry) = self.0.next_back() { - if let Some(entry) = entry { - return Some(& $($mut)? entry.data) - } - } - None - } - } - } -} - -pub struct Iter<'a, App: ApplicationLayer> (std::slice::Iter<'a, Option>>); -pub struct IterMut<'a, App: ApplicationLayer> (std::slice::IterMut<'a, Option>>); - -iterator!(Iter, { }); -iterator!(IterMut, {mut}); impl<'a, App: ApplicationLayer> ReplyGuard<'a, App> { pub fn is_full(&self) -> bool { @@ -282,11 +224,7 @@ impl<'a, App: ApplicationLayer> ReplyGuard<'a, App> { /// A packet can only be replied to once. Once a call to `reply` is successful and returns `true`, /// all subsequent calls will return `false`. #[must_use = "The queue might be full causing the packet to not be sent"] - pub fn reply( - &mut self, - create: impl FnOnce(SeqNum, SeqNum) -> App::SendData, - current_time: i64, - ) -> bool { + pub fn reply(&mut self, create: impl FnOnce(SeqNum, SeqNum) -> App::SendData, current_time: i64) -> bool { if let Some(app) = self.app { let seq_queue = &mut self.seq_queue; let seq_num = seq_queue.next_send_seq_num; @@ -320,3 +258,35 @@ impl<'a, App: ApplicationLayer> Drop for ReplyGuard<'a, App> { } } } + +macro_rules! iterator { + ($iter:ident, {$( $mut:tt )?}) => { + impl<'a, App: ApplicationLayer> Iterator for $iter<'a, App> { + type Item = &'a $($mut)? App::SendData; + fn next(&mut self) -> Option { + while let Some(entry) = self.0.next() { + if let Some(entry) = entry { + return Some(& $($mut)? entry.data) + } + } + None + } + + fn size_hint(&self) -> (usize, Option) { + (0, Some(self.0.len())) + } + } + impl<'a, App: ApplicationLayer> DoubleEndedIterator for $iter<'a, App> { + fn next_back(&mut self) -> Option { + while let Some(entry) = self.0.next_back() { + if let Some(entry) = entry { + return Some(& $($mut)? entry.data) + } + } + None + } + } + } +} +iterator!(Iter, {}); +iterator!(IterMut, {mut});