From d0234fd01ce447a363a1c84a453df03feb1bbaec Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Wed, 20 Jul 2022 13:22:33 +0400 Subject: [PATCH 1/3] change DataChannel::accept 3rd argument to be a ref --- src/data_channel/data_channel_test.rs | 12 ++++++------ src/data_channel/mod.rs | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/data_channel/data_channel_test.rs b/src/data_channel/data_channel_test.rs index 9b3ae0b..7a9b0d7 100644 --- a/src/data_channel/data_channel_test.rs +++ b/src/data_channel/data_channel_test.rs @@ -188,7 +188,7 @@ async fn pr_ordered_unordered_test(channel_type: ChannelType, is_ordered: bool) let dc0 = DataChannel::dial(&a0, 100, cfg.clone()).await?; bridge_process_at_least_one(&br).await; - let dc1 = DataChannel::accept(&a1, Config::default(), Vec::new()).await?; + let dc1 = DataChannel::accept(&a1, Config::default(), &Vec::new()).await?; bridge_process_at_least_one(&br).await; assert_eq!(dc0.config, cfg, "local config should match"); @@ -274,7 +274,7 @@ async fn test_data_channel_channel_type_reliable_ordered() -> Result<()> { let dc0 = DataChannel::dial(&a0, 100, cfg.clone()).await?; bridge_process_at_least_one(&br).await; - let dc1 = DataChannel::accept(&a1, Config::default(), Vec::new()).await?; + let dc1 = DataChannel::accept(&a1, Config::default(), &Vec::new()).await?; bridge_process_at_least_one(&br).await; assert_eq!(dc0.config, cfg, "local config should match"); @@ -336,7 +336,7 @@ async fn test_data_channel_channel_type_reliable_unordered() -> Result<()> { let dc0 = DataChannel::dial(&a0, 100, cfg.clone()).await?; bridge_process_at_least_one(&br).await; - let dc1 = DataChannel::accept(&a1, Config::default(), Vec::new()).await?; + let dc1 = DataChannel::accept(&a1, Config::default(), &Vec::new()).await?; bridge_process_at_least_one(&br).await; assert_eq!(dc0.config, cfg, "local config should match"); @@ -434,7 +434,7 @@ async fn test_data_channel_buffered_amount() -> Result<()> { ); bridge_process_at_least_one(&br).await; - let dc1 = Arc::new(DataChannel::accept(&a1, Config::default(), Vec::new()).await?); + let dc1 = Arc::new(DataChannel::accept(&a1, Config::default(), &Vec::new()).await?); bridge_process_at_least_one(&br).await; while dc0.buffered_amount() > 0 { @@ -535,7 +535,7 @@ async fn test_stats() -> Result<()> { let dc0 = DataChannel::dial(&a0, 100, cfg.clone()).await?; bridge_process_at_least_one(&br).await; - let dc1 = DataChannel::accept(&a1, Config::default(), Vec::new()).await?; + let dc1 = DataChannel::accept(&a1, Config::default(), &Vec::new()).await?; bridge_process_at_least_one(&br).await; let mut bytes_sent = 0; @@ -628,7 +628,7 @@ async fn test_poll_data_channel() -> Result<()> { let dc0 = Arc::new(DataChannel::dial(&a0, 100, cfg.clone()).await?); bridge_process_at_least_one(&br).await; - let dc1 = Arc::new(DataChannel::accept(&a1, Config::default(), Vec::new()).await?); + let dc1 = Arc::new(DataChannel::accept(&a1, Config::default(), &Vec::new()).await?); bridge_process_at_least_one(&br).await; let mut poll_dc0 = PollDataChannel::new(dc0); diff --git a/src/data_channel/mod.rs b/src/data_channel/mod.rs index c1fd558..96ff599 100644 --- a/src/data_channel/mod.rs +++ b/src/data_channel/mod.rs @@ -81,7 +81,7 @@ impl DataChannel { pub async fn accept( association: &Arc, config: Config, - existing_channels: Vec, + existing_channels: &[DataChannel], ) -> Result { let stream = association .accept_stream() @@ -661,6 +661,6 @@ impl fmt::Debug for PollDataChannel { impl AsRef for PollDataChannel { fn as_ref(&self) -> &DataChannel { - &*self.data_channel + &self.data_channel } } From c1ba10cfb2b45eaf61e1d82c20676f5a7e062efc Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Wed, 20 Jul 2022 13:37:55 +0400 Subject: [PATCH 2/3] change existing_channels to contain Arc this is more in line with current API where association is also Arc --- src/data_channel/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/data_channel/mod.rs b/src/data_channel/mod.rs index 96ff599..a8f91d2 100644 --- a/src/data_channel/mod.rs +++ b/src/data_channel/mod.rs @@ -81,7 +81,7 @@ impl DataChannel { pub async fn accept( association: &Arc, config: Config, - existing_channels: &[DataChannel], + existing_channels: &[Arc], ) -> Result { let stream = association .accept_stream() @@ -93,7 +93,7 @@ impl DataChannel { channel .stream .set_default_payload_type(PayloadProtocolIdentifier::Binary); - return Ok(channel.to_owned()); + return Ok(channel.as_ref().to_owned()); } } From 5fbccb021e3de598dc474cf7acca0207f63b75ba Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Wed, 20 Jul 2022 16:11:09 +0400 Subject: [PATCH 3/3] accept any type in DataChannel::accept as long as you can borrow DataChannel --- src/data_channel/data_channel_test.rs | 18 ++++++++++++------ src/data_channel/mod.rs | 18 +++++++++++------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/data_channel/data_channel_test.rs b/src/data_channel/data_channel_test.rs index 7a9b0d7..8763ea9 100644 --- a/src/data_channel/data_channel_test.rs +++ b/src/data_channel/data_channel_test.rs @@ -188,7 +188,8 @@ async fn pr_ordered_unordered_test(channel_type: ChannelType, is_ordered: bool) let dc0 = DataChannel::dial(&a0, 100, cfg.clone()).await?; bridge_process_at_least_one(&br).await; - let dc1 = DataChannel::accept(&a1, Config::default(), &Vec::new()).await?; + let existing_data_channels: Vec = Vec::new(); + let dc1 = DataChannel::accept(&a1, Config::default(), &existing_data_channels).await?; bridge_process_at_least_one(&br).await; assert_eq!(dc0.config, cfg, "local config should match"); @@ -274,7 +275,8 @@ async fn test_data_channel_channel_type_reliable_ordered() -> Result<()> { let dc0 = DataChannel::dial(&a0, 100, cfg.clone()).await?; bridge_process_at_least_one(&br).await; - let dc1 = DataChannel::accept(&a1, Config::default(), &Vec::new()).await?; + let existing_data_channels: Vec = Vec::new(); + let dc1 = DataChannel::accept(&a1, Config::default(), &existing_data_channels).await?; bridge_process_at_least_one(&br).await; assert_eq!(dc0.config, cfg, "local config should match"); @@ -336,7 +338,8 @@ async fn test_data_channel_channel_type_reliable_unordered() -> Result<()> { let dc0 = DataChannel::dial(&a0, 100, cfg.clone()).await?; bridge_process_at_least_one(&br).await; - let dc1 = DataChannel::accept(&a1, Config::default(), &Vec::new()).await?; + let existing_data_channels: Vec = Vec::new(); + let dc1 = DataChannel::accept(&a1, Config::default(), &existing_data_channels).await?; bridge_process_at_least_one(&br).await; assert_eq!(dc0.config, cfg, "local config should match"); @@ -434,7 +437,8 @@ async fn test_data_channel_buffered_amount() -> Result<()> { ); bridge_process_at_least_one(&br).await; - let dc1 = Arc::new(DataChannel::accept(&a1, Config::default(), &Vec::new()).await?); + let existing_data_channels: Vec = Vec::new(); + let dc1 = Arc::new(DataChannel::accept(&a1, Config::default(), &existing_data_channels).await?); bridge_process_at_least_one(&br).await; while dc0.buffered_amount() > 0 { @@ -535,7 +539,8 @@ async fn test_stats() -> Result<()> { let dc0 = DataChannel::dial(&a0, 100, cfg.clone()).await?; bridge_process_at_least_one(&br).await; - let dc1 = DataChannel::accept(&a1, Config::default(), &Vec::new()).await?; + let existing_data_channels: Vec = Vec::new(); + let dc1 = DataChannel::accept(&a1, Config::default(), &existing_data_channels).await?; bridge_process_at_least_one(&br).await; let mut bytes_sent = 0; @@ -628,7 +633,8 @@ async fn test_poll_data_channel() -> Result<()> { let dc0 = Arc::new(DataChannel::dial(&a0, 100, cfg.clone()).await?); bridge_process_at_least_one(&br).await; - let dc1 = Arc::new(DataChannel::accept(&a1, Config::default(), &Vec::new()).await?); + let existing_data_channels: Vec = Vec::new(); + let dc1 = Arc::new(DataChannel::accept(&a1, Config::default(), &existing_data_channels).await?); bridge_process_at_least_one(&br).await; let mut poll_dc0 = PollDataChannel::new(dc0); diff --git a/src/data_channel/mod.rs b/src/data_channel/mod.rs index a8f91d2..1731440 100644 --- a/src/data_channel/mod.rs +++ b/src/data_channel/mod.rs @@ -14,6 +14,7 @@ use util::marshal::*; use bytes::{Buf, Bytes}; use derive_builder::Builder; +use std::borrow::Borrow; use std::fmt; use std::future::Future; use std::io; @@ -78,22 +79,25 @@ impl DataChannel { } /// Accept is used to accept incoming data channels over SCTP - pub async fn accept( + pub async fn accept( association: &Arc, config: Config, - existing_channels: &[Arc], - ) -> Result { + existing_channels: &[T], + ) -> Result + where + T: Borrow, + { let stream = association .accept_stream() .await .ok_or(Error::ErrStreamClosed)?; - for channel in existing_channels.iter() { + for channel in existing_channels.iter().map(|ch| ch.borrow()) { if channel.stream_identifier() == stream.stream_identifier() { - channel - .stream + let ch = channel.to_owned(); + ch.stream .set_default_payload_type(PayloadProtocolIdentifier::Binary); - return Ok(channel.as_ref().to_owned()); + return Ok(ch); } }