From 2c4d3cf53457c94388c832ed4ebe519194ab0c34 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Fri, 2 Apr 2021 22:14:23 -0500 Subject: [PATCH] add BoundedVec generic struct --- crates/ptth_relay/src/relay_state.rs | 35 ++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/crates/ptth_relay/src/relay_state.rs b/crates/ptth_relay/src/relay_state.rs index 96d3a98..f718e84 100644 --- a/crates/ptth_relay/src/relay_state.rs +++ b/crates/ptth_relay/src/relay_state.rs @@ -70,13 +70,6 @@ impl Default for ServerStatus { } } -#[derive (Clone)] -pub struct RejectedServer { - pub name: String, - pub tripcode: blake3::Hash, - pub seen: DateTime , -} - pub struct RelayState { pub config: RwLock , @@ -94,6 +87,34 @@ pub struct RelayState { pub unregistered_servers: RwLock >, } +pub struct BoundedVec { + bound: usize, + v: RwLock >, +} + +impl BoundedVec { + pub async fn to_vec (&self) -> Vec { + let guard = self.v.read ().await; + (*guard).clone () + } + + pub async fn push (&mut self, x: T) { + let mut guard = self.v.write ().await; + guard.push (x); + + while guard.len () > self.bound { + guard.remove (0); + } + } +} + +#[derive (Clone)] +pub struct RejectedServer { + pub name: String, + pub tripcode: blake3::Hash, + pub seen: DateTime , +} + impl TryFrom for RelayState { type Error = RelayError;