add BoundedVec generic struct
parent
6ee9b6e7c9
commit
2c4d3cf534
|
@ -70,13 +70,6 @@ impl Default for ServerStatus {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive (Clone)]
|
||||
pub struct RejectedServer {
|
||||
pub name: String,
|
||||
pub tripcode: blake3::Hash,
|
||||
pub seen: DateTime <Utc>,
|
||||
}
|
||||
|
||||
pub struct RelayState {
|
||||
pub config: RwLock <Config>,
|
||||
|
||||
|
@ -94,6 +87,34 @@ pub struct RelayState {
|
|||
pub unregistered_servers: RwLock <Vec <RejectedServer>>,
|
||||
}
|
||||
|
||||
pub struct BoundedVec <T: Clone> {
|
||||
bound: usize,
|
||||
v: RwLock <Vec <T>>,
|
||||
}
|
||||
|
||||
impl <T: Clone> BoundedVec <T> {
|
||||
pub async fn to_vec (&self) -> Vec <T> {
|
||||
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 <Utc>,
|
||||
}
|
||||
|
||||
impl TryFrom <Config> for RelayState {
|
||||
type Error = RelayError;
|
||||
|
||||
|
|
Loading…
Reference in New Issue