From: Ian Jackson Date: Sat, 11 Jul 2020 23:00:31 +0000 (+0100) Subject: updates.rs: code motion X-Git-Tag: otter-0.2.0~1377 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=835cdf7ab7eec3f57a4eab01d16eb61007b2bcda;p=otter.git updates.rs: code motion --- diff --git a/src/updates.rs b/src/updates.rs index 20a94a2b..efe5b886 100644 --- a/src/updates.rs +++ b/src/updates.rs @@ -1,17 +1,29 @@ - // update messages from server to client use crate::imports::*; +// ---------- newtypes, type aliases, basic definitions ---------- + #[derive(Debug,Copy,Clone,Eq,PartialEq,Deserialize,Serialize)] #[serde(transparent)] pub struct ClientSequence(u64); +const RECENT_BUFFER : usize = 50; + +// ---------- prepared updates, queued in memory ---------- + +#[derive(Debug)] +pub struct PlayerUpdates { + pub log : StableIndexVecDeque,sse::UpdateId>, + pub cv : Arc, +} + #[derive(Debug)] pub struct PreparedUpdate { pub gen : Generation, pub us : Vec, } + #[derive(Debug)] pub enum PreparedUpdateEntry { Piece { @@ -22,6 +34,44 @@ pub enum PreparedUpdateEntry { }, Log (Arc), } + +#[derive(Debug,Serialize)] +pub struct PreparedPieceState { + pub pos : Pos, + pub svg : String, + pub held : Option, + pub z : ZCoord, + pub zg : Generation, +} + +// ---------- piece updates ---------- + +#[derive(Debug,Serialize)] +pub enum PieceUpdateOp { + Delete(), + Insert(NS), + Modify(NS), + Move(Pos), + SetZLevel(ZLevel), +} + +// ========== implementation ========== + +// ---------- prepared updates, queued in memory ---------- + +impl Default for PlayerUpdates { + fn default() -> PlayerUpdates { PlayerUpdates { + log : StableIndexVecDeque::with_capacity(RECENT_BUFFER), + cv : Default::default(), + } } +} + +impl PreparedUpdate { + pub fn json_len(&self) -> usize { + self.us.iter().map(|u| 20 + u.json_len()).sum() + } +} + impl PreparedUpdateEntry { pub fn json_len(&self) -> usize { use PreparedUpdateEntry::*; @@ -36,20 +86,9 @@ impl PreparedUpdateEntry { } } } -impl PreparedUpdate { - pub fn json_len(&self) -> usize { - self.us.iter().map(|u| 20 + u.json_len()).sum() - } -} -#[derive(Debug,Serialize)] -pub enum PieceUpdateOp { - Delete(), - Insert(NS), - Modify(NS), - Move(Pos), - SetZLevel(ZLevel), -} +// ---------- piece updates ---------- + impl PieceUpdateOp { pub fn new_state(&self) -> Option<&NS> { use PieceUpdateOp::*; @@ -83,27 +122,3 @@ impl PieceUpdateOp { } } } - -#[derive(Debug)] -pub struct PlayerUpdates { - pub log : StableIndexVecDeque,sse::UpdateId>, - pub cv : Arc, -} - -const RECENT_BUFFER : usize = 50; - -impl Default for PlayerUpdates { - fn default() -> PlayerUpdates { PlayerUpdates { - log : StableIndexVecDeque::with_capacity(RECENT_BUFFER), - cv : Default::default(), - } } -} - -#[derive(Debug,Serialize)] -pub struct PreparedPieceState { - pub pos : Pos, - pub svg : String, - pub held : Option, - pub z : ZCoord, - pub zg : Generation, -}