Skip to content

Commit 9490546

Browse files
authored
refactor: make hex_as_bytes DRY (#1409)
1 parent 5c4b6ce commit 9490546

16 files changed

+95
-78
lines changed

rust-toolchain.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[toolchain]
2-
channel = "1.72.0"
2+
channel = "1.79.0"
33
components = ["rustfmt", "clippy"]

rust/src/data_structures.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -528,12 +528,15 @@ pub enum DtlsFingerprint {
528528
},
529529
}
530530

531-
fn hex_as_bytes(input: &str, output: &mut [u8]) {
531+
fn hex_as_bytes<const N: usize>(input: &str) -> [u8; N] {
532+
let mut output = [0_u8; N];
532533
for (i, o) in input.split(':').zip(&mut output.iter_mut()) {
533534
*o = u8::from_str_radix(i, 16).unwrap_or_else(|error| {
534535
panic!("Failed to parse value {i} as series of hex bytes: {error}")
535536
});
536537
}
538+
539+
output
537540
}
538541

539542
impl DtlsFingerprint {
@@ -565,45 +568,35 @@ impl DtlsFingerprint {
565568
pub(crate) fn from_fbs(fingerprint: &web_rtc_transport::Fingerprint) -> DtlsFingerprint {
566569
match fingerprint.algorithm {
567570
web_rtc_transport::FingerprintAlgorithm::Sha1 => {
568-
let mut value_result = [0_u8; 20];
569-
570-
hex_as_bytes(fingerprint.value.as_str(), &mut value_result);
571+
let value_result = hex_as_bytes::<20>(fingerprint.value.as_str());
571572

572573
DtlsFingerprint::Sha1 {
573574
value: value_result,
574575
}
575576
}
576577
web_rtc_transport::FingerprintAlgorithm::Sha224 => {
577-
let mut value_result = [0_u8; 28];
578-
579-
hex_as_bytes(fingerprint.value.as_str(), &mut value_result);
578+
let value_result = hex_as_bytes::<28>(fingerprint.value.as_str());
580579

581580
DtlsFingerprint::Sha224 {
582581
value: value_result,
583582
}
584583
}
585584
web_rtc_transport::FingerprintAlgorithm::Sha256 => {
586-
let mut value_result = [0_u8; 32];
587-
588-
hex_as_bytes(fingerprint.value.as_str(), &mut value_result);
585+
let value_result = hex_as_bytes::<32>(fingerprint.value.as_str());
589586

590587
DtlsFingerprint::Sha256 {
591588
value: value_result,
592589
}
593590
}
594591
web_rtc_transport::FingerprintAlgorithm::Sha384 => {
595-
let mut value_result = [0_u8; 48];
596-
597-
hex_as_bytes(fingerprint.value.as_str(), &mut value_result);
592+
let value_result = hex_as_bytes::<48>(fingerprint.value.as_str());
598593

599594
DtlsFingerprint::Sha384 {
600595
value: value_result,
601596
}
602597
}
603598
web_rtc_transport::FingerprintAlgorithm::Sha512 => {
604-
let mut value_result = [0_u8; 64];
605-
606-
hex_as_bytes(fingerprint.value.as_str(), &mut value_result);
599+
let value_result = hex_as_bytes::<64>(fingerprint.value.as_str());
607600

608601
DtlsFingerprint::Sha512 {
609602
value: value_result,

rust/src/ortc.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -681,15 +681,15 @@ pub(crate) fn can_consume(
681681
if caps
682682
.codecs
683683
.iter()
684-
.any(|cap_codec| match_codecs(cap_codec.deref().into(), codec.into(), true).is_ok())
684+
.any(|cap_codec| match_codecs(cap_codec.into(), codec.into(), true).is_ok())
685685
{
686686
matching_codecs.push(codec);
687687
}
688688
}
689689

690690
// Ensure there is at least one media codec.
691691
Ok(matching_codecs
692-
.get(0)
692+
.first()
693693
.map(|codec| !codec.is_rtx())
694694
.unwrap_or_default())
695695
}
@@ -841,7 +841,7 @@ pub(crate) fn get_consumer_rtp_parameters(
841841
// (assume all encodings have the same value).
842842
let mut scalability_mode = consumable_rtp_parameters
843843
.encodings
844-
.get(0)
844+
.first()
845845
.map(|encoding| encoding.scalability_mode.clone())
846846
.unwrap_or_default();
847847

rust/src/ortc/tests.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ fn get_producer_rtp_parameters_mapping_get_consumable_rtp_parameters_get_consume
255255
]
256256
);
257257

258-
assert_eq!(rtp_mapping.encodings.get(0).unwrap().ssrc, Some(11111111));
259-
assert_eq!(rtp_mapping.encodings.get(0).unwrap().rid, None);
258+
assert_eq!(rtp_mapping.encodings.first().unwrap().ssrc, Some(11111111));
259+
assert_eq!(rtp_mapping.encodings.first().unwrap().rid, None);
260260
assert_eq!(rtp_mapping.encodings.get(1).unwrap().ssrc, Some(21111111));
261261
assert_eq!(rtp_mapping.encodings.get(1).unwrap().rid, None);
262262
assert_eq!(rtp_mapping.encodings.get(2).unwrap().ssrc, None);
@@ -303,21 +303,21 @@ fn get_producer_rtp_parameters_mapping_get_consumable_rtp_parameters_get_consume
303303
);
304304

305305
assert_eq!(
306-
consumable_rtp_parameters.encodings.get(0).unwrap().ssrc,
307-
Some(rtp_mapping.encodings.get(0).unwrap().mapped_ssrc),
306+
consumable_rtp_parameters.encodings.first().unwrap().ssrc,
307+
Some(rtp_mapping.encodings.first().unwrap().mapped_ssrc),
308308
);
309309
assert_eq!(
310310
consumable_rtp_parameters
311311
.encodings
312-
.get(0)
312+
.first()
313313
.unwrap()
314314
.max_bitrate,
315315
Some(111111),
316316
);
317317
assert_eq!(
318318
consumable_rtp_parameters
319319
.encodings
320-
.get(0)
320+
.first()
321321
.unwrap()
322322
.scalability_mode,
323323
ScalabilityMode::L1T3,
@@ -489,28 +489,28 @@ fn get_producer_rtp_parameters_mapping_get_consumable_rtp_parameters_get_consume
489489
assert_eq!(consumer_rtp_parameters.encodings.len(), 1);
490490
assert!(consumer_rtp_parameters
491491
.encodings
492-
.get(0)
492+
.first()
493493
.unwrap()
494494
.ssrc
495495
.is_some());
496496
assert!(consumer_rtp_parameters
497497
.encodings
498-
.get(0)
498+
.first()
499499
.unwrap()
500500
.rtx
501501
.is_some());
502502
assert_eq!(
503503
consumer_rtp_parameters
504504
.encodings
505-
.get(0)
505+
.first()
506506
.unwrap()
507507
.scalability_mode,
508508
ScalabilityMode::L3T3,
509509
);
510510
assert_eq!(
511511
consumer_rtp_parameters
512512
.encodings
513-
.get(0)
513+
.first()
514514
.unwrap()
515515
.max_bitrate,
516516
Some(333333),
@@ -566,26 +566,26 @@ fn get_producer_rtp_parameters_mapping_get_consumable_rtp_parameters_get_consume
566566
assert_eq!(pipe_consumer_rtp_parameters.encodings.len(), 3);
567567
assert!(pipe_consumer_rtp_parameters
568568
.encodings
569-
.get(0)
569+
.first()
570570
.unwrap()
571571
.ssrc
572572
.is_some());
573573
assert!(pipe_consumer_rtp_parameters
574574
.encodings
575-
.get(0)
575+
.first()
576576
.unwrap()
577577
.rtx
578578
.is_none());
579579
assert!(pipe_consumer_rtp_parameters
580580
.encodings
581-
.get(0)
581+
.first()
582582
.unwrap()
583583
.max_bitrate
584584
.is_some());
585585
assert_eq!(
586586
pipe_consumer_rtp_parameters
587587
.encodings
588-
.get(0)
588+
.first()
589589
.unwrap()
590590
.scalability_mode,
591591
ScalabilityMode::L1T3,

rust/src/router.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1303,8 +1303,12 @@ impl Router {
13031303
// We've created `DataConsumer` with SCTP above, so this should never panic
13041304
pipe_data_consumer.sctp_stream_parameters().unwrap(),
13051305
);
1306-
producer_options.label = pipe_data_consumer.label().clone();
1307-
producer_options.protocol = pipe_data_consumer.protocol().clone();
1306+
producer_options
1307+
.label
1308+
.clone_from(pipe_data_consumer.label());
1309+
producer_options
1310+
.protocol
1311+
.clone_from(pipe_data_consumer.protocol());
13081312
producer_options.app_data = data_producer.app_data().clone();
13091313

13101314
producer_options

rust/src/router/producer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ impl Producer {
759759
match Notification::from_fbs(notification) {
760760
Ok(notification) => match notification {
761761
Notification::Score(scores) => {
762-
*score.lock() = scores.clone();
762+
score.lock().clone_from(&scores);
763763
handlers.score.call(|callback| {
764764
callback(&scores);
765765
});

rust/src/rtp_parameters.rs

-1
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,6 @@ impl RtpParameters {
919919
})
920920
}
921921

922-
#[allow(dead_code)]
923922
pub(crate) fn into_fbs(self) -> rtp_parameters::RtpParameters {
924923
rtp_parameters::RtpParameters {
925924
mid: self.mid,

rust/src/scalability_modes.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ impl FromStr for ScalabilityMode {
184184
}
185185
}
186186

187-
impl ToString for ScalabilityMode {
188-
fn to_string(&self) -> String {
189-
self.as_str().to_string()
187+
impl std::fmt::Display for ScalabilityMode {
188+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
189+
write!(f, "{}", self.as_str())
190190
}
191191
}
192192

rust/src/worker/common.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ impl<F: Sized + Send + Sync + 'static> EventHandlers<F> {
3535
let index;
3636
{
3737
let mut event_handlers = self.handlers.lock();
38-
let list = event_handlers
39-
.entry(target_id.clone())
40-
.or_insert_with(EventHandlersList::default);
38+
let list = event_handlers.entry(target_id.clone()).or_default();
4139
index = list.index;
4240
list.index += 1;
4341
list.callbacks.insert(index, callback);

rust/src/worker/utils/channel_read_fn.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,18 @@ unsafe extern "C" fn free_vec(message: *mut u8, message_len: u32, message_capaci
88
Vec::from_raw_parts(message, message_len as usize, message_capacity);
99
}
1010

11-
pub(super) struct ChannelReadCallback(
12-
Box<dyn (FnMut(UvAsyncT) -> Option<Vec<u8>>) + Send + 'static>,
13-
);
11+
pub(super) struct ChannelReadCallback {
12+
// Silence clippy warnings
13+
_callback: Box<dyn (FnMut(UvAsyncT) -> Option<Vec<u8>>) + Send + 'static>,
14+
}
15+
16+
impl ChannelReadCallback {
17+
pub(super) fn new(
18+
_callback: Box<dyn (FnMut(UvAsyncT) -> Option<Vec<u8>>) + Send + 'static>,
19+
) -> Self {
20+
Self { _callback }
21+
}
22+
}
1423

1524
pub(crate) struct PreparedChannelRead {
1625
channel_read_fn: ChannelReadFn,
@@ -71,6 +80,6 @@ where
7180
PreparedChannelRead {
7281
channel_read_fn: wrapper::<F>,
7382
channel_read_ctx: ChannelReadCtx(read_callback.as_ref() as *const F as *const c_void),
74-
write_callback: ChannelReadCallback(read_callback),
83+
write_callback: ChannelReadCallback::new(read_callback),
7584
}
7685
}

rust/src/worker/utils/channel_write_fn.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,19 @@ pub(super) use mediasoup_sys::{ChannelWriteCtx, ChannelWriteFn};
22
use std::os::raw::c_void;
33
use std::slice;
44

5-
#[allow(clippy::type_complexity)]
6-
pub(super) struct ChannelReadCallback(Box<dyn FnMut(&[u8]) + Send + 'static>);
5+
/// TypeAlias to silience clippy::type_complexity warnings
6+
type CallbackType = Box<dyn FnMut(&[u8]) + Send + 'static>;
7+
8+
pub(super) struct ChannelReadCallback {
9+
// Silence clippy warnings
10+
_callback: CallbackType,
11+
}
12+
13+
impl ChannelReadCallback {
14+
pub(super) fn new(_callback: CallbackType) -> Self {
15+
Self { _callback }
16+
}
17+
}
718

819
pub(crate) struct PreparedChannelWrite {
920
channel_write_fn: ChannelWriteFn,
@@ -54,6 +65,6 @@ where
5465
PreparedChannelWrite {
5566
channel_write_fn: wrapper::<F>,
5667
channel_write_ctx: ChannelWriteCtx(read_callback.as_ref() as *const F as *const c_void),
57-
read_callback: ChannelReadCallback(read_callback),
68+
read_callback: ChannelReadCallback::new(read_callback),
5869
}
5970
}

rust/tests/integration/consumer.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,16 @@ fn consumer_device_capabilities() -> RtpCapabilities {
291291
}
292292

293293
// Keeps executor threads running until dropped
294-
struct ExecutorGuard(Vec<async_oneshot::Sender<()>>);
294+
struct ExecutorGuard {
295+
// Silence clippy warnings
296+
_senders: Vec<async_oneshot::Sender<()>>,
297+
}
298+
299+
impl ExecutorGuard {
300+
fn new(_senders: Vec<async_oneshot::Sender<()>>) -> Self {
301+
Self { _senders }
302+
}
303+
}
295304

296305
fn create_executor() -> (ExecutorGuard, Arc<Executor<'static>>) {
297306
let executor = Arc::new(Executor::new());
@@ -318,7 +327,7 @@ fn create_executor() -> (ExecutorGuard, Arc<Executor<'static>>) {
318327
})
319328
.collect();
320329

321-
(ExecutorGuard(senders), executor)
330+
(ExecutorGuard::new(senders), executor)
322331
}
323332

324333
async fn init() -> (
@@ -977,7 +986,7 @@ fn dump_succeeds() {
977986
ssrc: audio_consumer
978987
.rtp_parameters()
979988
.encodings
980-
.get(0)
989+
.first()
981990
.unwrap()
982991
.ssrc,
983992
rid: None,
@@ -1086,13 +1095,13 @@ fn dump_succeeds() {
10861095
ssrc: video_consumer
10871096
.rtp_parameters()
10881097
.encodings
1089-
.get(0)
1098+
.first()
10901099
.unwrap()
10911100
.ssrc,
10921101
rtx: video_consumer
10931102
.rtp_parameters()
10941103
.encodings
1095-
.get(0)
1104+
.first()
10961105
.unwrap()
10971106
.rtx,
10981107
dtx: None,
@@ -1165,7 +1174,7 @@ fn get_stats_succeeds() {
11651174
audio_consumer
11661175
.rtp_parameters()
11671176
.encodings
1168-
.get(0)
1177+
.first()
11691178
.unwrap()
11701179
.ssrc
11711180
.unwrap()
@@ -1214,7 +1223,7 @@ fn get_stats_succeeds() {
12141223
video_consumer
12151224
.rtp_parameters()
12161225
.encodings
1217-
.get(0)
1226+
.first()
12181227
.unwrap()
12191228
.ssrc
12201229
.unwrap()

0 commit comments

Comments
 (0)