From a6c82e6ca17ba5afc6e0aa849acb54bbb32d7e8e Mon Sep 17 00:00:00 2001 From: Toralf Wittner Date: Tue, 4 Sep 2018 16:12:58 +0200 Subject: [PATCH] Use `Bytes` instead of `BytesMut` in mplex. (#456) --- muxers/mplex/src/codec.rs | 18 +++++++++--------- muxers/mplex/src/lib.rs | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/muxers/mplex/src/codec.rs b/muxers/mplex/src/codec.rs index b99bba4e..e7106a58 100644 --- a/muxers/mplex/src/codec.rs +++ b/muxers/mplex/src/codec.rs @@ -20,7 +20,7 @@ use std::io::{Error as IoError, ErrorKind as IoErrorKind}; use std::mem; -use bytes::{BufMut, BytesMut}; +use bytes::{BufMut, Bytes, BytesMut}; use core::Endpoint; use tokio_io::codec::{Decoder, Encoder}; use unsigned_varint::{codec, encode}; @@ -33,7 +33,7 @@ const MAX_FRAME_SIZE: usize = 32 * 1024 * 1024; #[derive(Debug, Clone)] pub enum Elem { Open { substream_id: u32 }, - Data { substream_id: u32, endpoint: Endpoint, data: BytesMut }, + Data { substream_id: u32, endpoint: Endpoint, data: Bytes }, Close { substream_id: u32, endpoint: Endpoint }, Reset { substream_id: u32, endpoint: Endpoint }, } @@ -137,8 +137,8 @@ impl Decoder for Codec { let substream_id = (header >> 3) as u32; let out = match header & 7 { 0 => Elem::Open { substream_id }, - 1 => Elem::Data { substream_id, endpoint: Endpoint::Listener, data: buf }, - 2 => Elem::Data { substream_id, endpoint: Endpoint::Dialer, data: buf }, + 1 => Elem::Data { substream_id, endpoint: Endpoint::Listener, data: buf.freeze() }, + 2 => Elem::Data { substream_id, endpoint: Endpoint::Dialer, data: buf.freeze() }, 3 => Elem::Close { substream_id, endpoint: Endpoint::Listener }, 4 => Elem::Close { substream_id, endpoint: Endpoint::Dialer }, 5 => Elem::Reset { substream_id, endpoint: Endpoint::Listener }, @@ -168,7 +168,7 @@ impl Encoder for Codec { fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> { let (header, data) = match item { Elem::Open { substream_id } => { - ((substream_id as u64) << 3, BytesMut::new()) + ((substream_id as u64) << 3, Bytes::new()) }, Elem::Data { substream_id, endpoint: Endpoint::Listener, data } => { ((substream_id as u64) << 3 | 1, data) @@ -177,16 +177,16 @@ impl Encoder for Codec { ((substream_id as u64) << 3 | 2, data) }, Elem::Close { substream_id, endpoint: Endpoint::Listener } => { - ((substream_id as u64) << 3 | 3, BytesMut::new()) + ((substream_id as u64) << 3 | 3, Bytes::new()) }, Elem::Close { substream_id, endpoint: Endpoint::Dialer } => { - ((substream_id as u64) << 3 | 4, BytesMut::new()) + ((substream_id as u64) << 3 | 4, Bytes::new()) }, Elem::Reset { substream_id, endpoint: Endpoint::Listener } => { - ((substream_id as u64) << 3 | 5, BytesMut::new()) + ((substream_id as u64) << 3 | 5, Bytes::new()) }, Elem::Reset { substream_id, endpoint: Endpoint::Dialer } => { - ((substream_id as u64) << 3 | 6, BytesMut::new()) + ((substream_id as u64) << 3 | 6, Bytes::new()) }, }; diff --git a/muxers/mplex/src/lib.rs b/muxers/mplex/src/lib.rs index 7f748c07..9ce50f74 100644 --- a/muxers/mplex/src/lib.rs +++ b/muxers/mplex/src/lib.rs @@ -423,7 +423,7 @@ where C: AsyncRead + AsyncWrite // We're in a loop, so all we need to do is set `substream.current_data` to the data we // just read and wait for the next iteration. match next_data_poll { - Ok(Async::Ready(Some(data))) => substream.current_data = data.freeze(), + Ok(Async::Ready(Some(data))) => substream.current_data = data, Ok(Async::Ready(None)) => return Ok(0), Ok(Async::NotReady) => { // There was no data packet in the buffer about this substream ; maybe it's