From 9498c45d1291c0b2343339bad624d0dc0ca0a934 Mon Sep 17 00:00:00 2001 From: Lizzy Fleckenstein Date: Wed, 15 Feb 2023 22:04:42 +0100 Subject: [PATCH] Use Cow for Pkt --- src/common.rs | 8 ++++---- src/recv.rs | 7 ++++--- src/send.rs | 6 +++--- src/share.rs | 4 ++-- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/common.rs b/src/common.rs index 797ccd1..4e32edc 100644 --- a/src/common.rs +++ b/src/common.rs @@ -2,7 +2,7 @@ use super::*; use async_trait::async_trait; use delegate::delegate; use num_enum::TryFromPrimitive; -use std::{io, sync::Arc}; +use std::{borrow::Cow, io, sync::Arc}; use tokio::sync::mpsc; pub const PROTO_ID: u32 = 0x4f457403; @@ -50,13 +50,13 @@ pub enum CtlType { } #[derive(Debug)] -pub struct Pkt { +pub struct Pkt<'a> { pub unrel: bool, pub chan: u8, - pub data: T, + pub data: Cow<'a, [u8]>, } -pub type InPkt = Result>, error::Error>; +pub type InPkt = Result, Error>; #[derive(Debug)] pub struct RudpReceiver { diff --git a/src/recv.rs b/src/recv.rs index 572b17e..c87a0fe 100644 --- a/src/recv.rs +++ b/src/recv.rs @@ -2,6 +2,7 @@ use super::*; use async_recursion::async_recursion; use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; use std::{ + borrow::Cow, cell::OnceCell, collections::HashMap, io, @@ -114,7 +115,7 @@ impl RecvWorker { Pkt { unrel: true, chan: 0, - data: &[CtlType::Disco as u8], + data: Cow::Borrowed(&[CtlType::Disco as u8]), }, ) .await @@ -210,7 +211,7 @@ impl RecvWorker { self.pkt_tx.send(Ok(Pkt { chan: chan.num, unrel, - data: cursor.remaining_slice().into(), + data: Cow::Owned(cursor.remaining_slice().into()), }))?; } PktType::Split => { @@ -273,7 +274,7 @@ impl RecvWorker { Pkt { unrel: true, chan: chan.num, - data: &ack_data, + data: Cow::Borrowed(&ack_data), }, ) .await?; diff --git a/src/send.rs b/src/send.rs index e0c2fa3..a3a7f03 100644 --- a/src/send.rs +++ b/src/send.rs @@ -6,13 +6,13 @@ use tokio::sync::watch; pub type AckResult = io::Result>>; impl RudpSender { - pub async fn send(&self, pkt: Pkt<&[u8]>) -> AckResult { + pub async fn send(&self, pkt: Pkt<'_>) -> AckResult { self.share.send(PktType::Orig, pkt).await // TODO: splits } } impl RudpShare { - pub async fn send(&self, tp: PktType, pkt: Pkt<&[u8]>) -> AckResult { + pub async fn send(&self, tp: PktType, pkt: Pkt<'_>) -> AckResult { let mut buf = Vec::with_capacity(4 + 2 + 1 + 1 + 2 + 1 + pkt.data.len()); buf.write_u32::(PROTO_ID)?; buf.write_u16::(*self.remote_id.read().await)?; @@ -27,7 +27,7 @@ impl RudpShare { } buf.write_u8(tp as u8)?; - buf.write_all(pkt.data)?; + buf.write_all(pkt.data.as_ref())?; self.send_raw(&buf).await?; diff --git a/src/share.rs b/src/share.rs index e0d2d2b..ad63290 100644 --- a/src/share.rs +++ b/src/share.rs @@ -1,5 +1,5 @@ use super::*; -use std::{collections::HashMap, io, sync::Arc, time::Duration}; +use std::{borrow::Cow, collections::HashMap, io, sync::Arc, time::Duration}; use tokio::{ sync::{mpsc, watch, Mutex, RwLock}, task::JoinSet, @@ -93,7 +93,7 @@ pub async fn new( Pkt { chan: 0, unrel: false, - data: &[CtlType::Ping as u8], + data: Cow::Borrowed(&[CtlType::Ping as u8]), }, ) .await -- 2.44.0