Skip to content

Commit 90f0c86

Browse files
Apply suggestions from code review
Co-authored-by: Dmitry Markin <dmitry@markin.tech>
1 parent c9a0140 commit 90f0c86

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/multistream_select/dialer_select.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::{
3333
types::protocol::ProtocolName,
3434
};
3535

36-
use bytes::BytesMut;
36+
use bytes::{Bytes, BytesMut};
3737
use futures::prelude::*;
3838
use std::{
3939
convert::TryFrom as _,
@@ -369,9 +369,11 @@ impl WebRtcDialerState {
369369
error::NegotiationError::ParseError(ParseError::InvalidData)
370370
})?;
371371

372-
let payload = tail[..len].to_vec();
373-
374-
let message = Message::decode(payload.into());
372+
let len_size = remaining.len() - tail.len();
373+
let bytes = Bytes::from(payload);
374+
let payload = bytes.slice(len_size..len_size + len);
375+
let remaining = bytes.slice(len_size + len..);
376+
let message = Message::decode(payload);
375377

376378
tracing::trace!(
377379
target: LOG_TARGET,
@@ -396,7 +398,7 @@ impl WebRtcDialerState {
396398
Err(_) => return Err(error::NegotiationError::ParseError(ParseError::InvalidData)),
397399
};
398400

399-
match drain_trailing_protocols(tail, len) {
401+
match drain_trailing_protocols(remaining) {
400402
Ok(protos) => protocols.extend(protos),
401403
Err(error) => return Err(error),
402404
}
@@ -439,18 +441,16 @@ impl WebRtcDialerState {
439441
}
440442

441443
fn drain_trailing_protocols(
442-
tail: &[u8],
443-
len: usize,
444+
mut remaining: Bytes,
444445
) -> Result<Vec<Protocol>, error::NegotiationError> {
445446
let mut protocols = vec![];
446-
let mut remaining = &tail[len..];
447447

448448
loop {
449449
if remaining.is_empty() {
450450
break;
451451
}
452452

453-
let (len, tail) = unsigned_varint::decode::usize(remaining).map_err(|error| {
453+
let (len, tail) = unsigned_varint::decode::usize(&remaining).map_err(|error| {
454454
tracing::debug!(
455455
target: LOG_TARGET,
456456
?error,
@@ -471,9 +471,10 @@ fn drain_trailing_protocols(
471471
return Err(error::NegotiationError::ParseError(ParseError::InvalidData));
472472
}
473473

474-
let payload = tail[..len].to_vec();
474+
let len_size = remaining.len() - tail.len();
475+
let payload = remaining.slice(len_size..len_size + len);
475476

476-
match Message::decode(payload.into()) {
477+
match Message::decode(payload) {
477478
Ok(Message::Protocol(protocol)) => protocols.push(protocol),
478479
Err(error) => {
479480
tracing::debug!(
@@ -487,7 +488,7 @@ fn drain_trailing_protocols(
487488
_ => return Err(error::NegotiationError::StateMismatch),
488489
}
489490

490-
remaining = &tail[len..];
491+
remaining = remaining.slice(len_size + len..);
491492
}
492493

493494
Ok(protocols)

0 commit comments

Comments
 (0)