From 18c8ec77dd2b1f1fecbe81e924cd1cbfbd421498 Mon Sep 17 00:00:00 2001 From: Maple Date: Sun, 26 Apr 2026 14:36:42 +0200 Subject: [PATCH] some small things --- rnex-core/Cargo.toml | 9 +++++-- rnex-core/src/util.rs | 61 +++++++++++++++++++++++++++++-------------- 2 files changed, 48 insertions(+), 22 deletions(-) diff --git a/rnex-core/Cargo.toml b/rnex-core/Cargo.toml index ff2b7b3..8d72719 100644 --- a/rnex-core/Cargo.toml +++ b/rnex-core/Cargo.toml @@ -13,7 +13,8 @@ v-byte-helpers = { git = "https://github.com/RusticMaple/VByteMacros", version = simplelog = "0.12.2" chrono = "0.4.39" log = "0.4.25" -rand = "0.8.5" +rand = "0.8.5 +" cfg-if = "1.0.4" hmac = "0.12.1" md-5 = "^0.10.6" @@ -29,13 +30,17 @@ ureq = { version = "3.1.4", features = [ "json" ] } serde = { version = "1.0.228", features = [ "derive" ] } serde_json = "1.0.149" +[dependencies.sqlx] +version = "0.8.6" +optional = true + [dev-dependencies] # criterion = "0.7.0" [features] rmc_struct_header = [] guest_login = [] -friends = ["guest_login"] +friends = ["guest_login", "dep:sqlx"] big_pid = [] v3-8-15 = ["rmc_struct_header"] v4-3-11 = ["v3-8-15"] diff --git a/rnex-core/src/util.rs b/rnex-core/src/util.rs index ff0150c..582c09b 100644 --- a/rnex-core/src/util.rs +++ b/rnex-core/src/util.rs @@ -1,24 +1,26 @@ -use std::ops::Deref; -use std::sync::Arc; -use log::{error, info}; -use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt}; -use tokio::sync::mpsc::{channel, Receiver, Sender}; -use tokio::sync::Notify; -use tokio::task; use crate::reggie::{UnitPacketRead, UnitPacketWrite}; +use log::{error, info}; +use std::iter::FilterMap; +use std::ops::Deref; +use std::sync::{Arc, Weak}; +use std::{slice, vec}; +use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt}; +use tokio::sync::Notify; +use tokio::sync::mpsc::{Receiver, Sender, channel}; +use tokio::task; #[derive(Clone)] pub struct SendingBufferConnection(Sender>, Arc); pub struct SplittableBufferConnection(SendingBufferConnection, Receiver>); -impl AsRef for SplittableBufferConnection{ +impl AsRef for SplittableBufferConnection { fn as_ref(&self) -> &SendingBufferConnection { &self.0 } } -impl Deref for SplittableBufferConnection{ +impl Deref for SplittableBufferConnection { type Target = SendingBufferConnection; fn deref(&self) -> &Self::Target { @@ -26,9 +28,7 @@ impl Deref for SplittableBufferConnection{ } } - - -impl From for SplittableBufferConnection{ +impl From for SplittableBufferConnection { fn from(value: T) -> Self { Self::new(value) } @@ -48,7 +48,6 @@ impl SplittableBufferConnection { let mut recver = inside_recv; let mut stream = stream; - loop { tokio::select! { data = recver.recv() => { @@ -81,7 +80,7 @@ impl SplittableBufferConnection { } } } - if let Err(e) = stream.shutdown().await{ + if let Err(e) = stream.shutdown().await { error!("failed to shut down stream: {}", e); } }); @@ -91,11 +90,11 @@ impl SplittableBufferConnection { } } -impl SendingBufferConnection{ - pub async fn send(&self, buffer: Vec) -> Option<()>{ +impl SendingBufferConnection { + pub async fn send(&self, buffer: Vec) -> Option<()> { self.0.send(buffer).await.ok() } - pub fn is_alive(&self) -> bool{ + pub fn is_alive(&self) -> bool { !self.0.is_closed() } pub async fn disconnect(&self) { @@ -106,12 +105,34 @@ impl SendingBufferConnection{ } } -impl SplittableBufferConnection{ - pub async fn recv(&mut self) -> Option>{ +impl SplittableBufferConnection { + pub async fn recv(&mut self) -> Option> { self.1.recv().await } - pub fn duplicate_sender(&self) -> SendingBufferConnection{ + pub fn duplicate_sender(&self) -> SendingBufferConnection { self.0.clone() } } + +struct WeakVec(Vec>); + +impl WeakVec { + pub fn new() -> Self { + Self(vec![]) + } + + pub fn from_vec(vec: Vec>) -> Self { + Self(vec) + } + + pub fn push(&mut self, val: Weak) { + self.0.retain(|v| v.upgrade().is_some()); + + self.0.push(val); + } + + pub fn iter(&self) -> impl Iterator> { + self.0.iter().filter_map(|w| w.upgrade()) + } +}