]> git.lizzy.rs Git - mt_rudp.git/commitdiff
Use Cow for Pkt
authorLizzy Fleckenstein <eliasfleckenstein@web.de>
Wed, 15 Feb 2023 21:04:42 +0000 (22:04 +0100)
committerLizzy Fleckenstein <eliasfleckenstein@web.de>
Wed, 15 Feb 2023 21:04:42 +0000 (22:04 +0100)
src/common.rs
src/recv.rs
src/send.rs
src/share.rs

index 797ccd1501fb205b11b1541d564aa5d943ad4da5..4e32edcce127c15236e7b8c6c63f24faa6d172c6 100644 (file)
@@ -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<T> {
+pub struct Pkt<'a> {
     pub unrel: bool,
     pub chan: u8,
-    pub data: T,
+    pub data: Cow<'a, [u8]>,
 }
 
-pub type InPkt = Result<Pkt<Vec<u8>>, error::Error>;
+pub type InPkt = Result<Pkt<'static>, Error>;
 
 #[derive(Debug)]
 pub struct RudpReceiver<S: UdpSender> {
index 572b17ea3ea45b1067065858eb6be0e338efe11f..c87a0fe156da5c5e86b78ad70fecab246bb3a5ee 100644 (file)
@@ -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<R: UdpReceiver, S: UdpSender> RecvWorker<R, S> {
                                                                Pkt {
                                                                        unrel: true,
                                                                        chan: 0,
-                                                                       data: &[CtlType::Disco as u8],
+                                                                       data: Cow::Borrowed(&[CtlType::Disco as u8]),
                                                                },
                                                        )
                                                        .await
@@ -210,7 +211,7 @@ impl<R: UdpReceiver, S: UdpSender> RecvWorker<R, S> {
                 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<R: UdpReceiver, S: UdpSender> RecvWorker<R, S> {
                         Pkt {
                             unrel: true,
                             chan: chan.num,
-                            data: &ack_data,
+                            data: Cow::Borrowed(&ack_data),
                         },
                     )
                     .await?;
index e0c2fa3a4407f9992ea15ab5f48864181ec0ed82..a3a7f036e5e2ea4639ed6267cbacf5e4b7441b25 100644 (file)
@@ -6,13 +6,13 @@ use tokio::sync::watch;
 pub type AckResult = io::Result<Option<watch::Receiver<bool>>>;
 
 impl<S: UdpSender> RudpSender<S> {
-    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<S: UdpSender> RudpShare<S> {
-    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::<BigEndian>(PROTO_ID)?;
         buf.write_u16::<BigEndian>(*self.remote_id.read().await)?;
@@ -27,7 +27,7 @@ impl<S: UdpSender> RudpShare<S> {
         }
 
         buf.write_u8(tp as u8)?;
-        buf.write_all(pkt.data)?;
+        buf.write_all(pkt.data.as_ref())?;
 
         self.send_raw(&buf).await?;
 
index e0d2d2b96e338fa1d9265e1143a08d9750d058f8..ad63290c9efc96da75f17eab8129e9d022dbb51d 100644 (file)
@@ -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<S: UdpSender, R: UdpReceiver>(
                         Pkt {
                             chan: 0,
                             unrel: false,
-                            data: &[CtlType::Ping as u8],
+                            data: Cow::Borrowed(&[CtlType::Ping as u8]),
                         },
                     )
                     .await