1818// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919// DEALINGS IN THE SOFTWARE.
2020
21- use libp2p_core:: Endpoint ;
22- use futures_codec:: { Decoder , Encoder } ;
23- use std:: io:: { Error as IoError , ErrorKind as IoErrorKind } ;
24- use std:: { fmt, mem} ;
2521use bytes:: { BufMut , Bytes , BytesMut } ;
22+ use futures_codec:: { Decoder , Encoder } ;
23+ use libp2p_core:: Endpoint ;
24+ use std:: { fmt, hash:: { Hash , Hasher } , io, mem} ;
2625use unsigned_varint:: { codec, encode} ;
2726
2827// Maximum size for a packet: 1MB as per the spec.
@@ -46,7 +45,7 @@ pub(crate) const MAX_FRAME_SIZE: usize = 1024 * 1024;
4645/// > we initiated the stream, so the local ID has the role `Endpoint::Dialer`.
4746/// > Conversely, when receiving a frame with a flag identifying the remote as a "sender",
4847/// > the corresponding local ID has the role `Endpoint::Listener`.
49- #[ derive( Copy , Clone , PartialEq , Eq , Hash , Debug ) ]
48+ #[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
5049pub struct LocalStreamId {
5150 num : u32 ,
5251 role : Endpoint ,
@@ -61,6 +60,14 @@ impl fmt::Display for LocalStreamId {
6160 }
6261}
6362
63+ impl Hash for LocalStreamId {
64+ fn hash < H : Hasher > ( & self , state : & mut H ) {
65+ state. write_u32 ( self . num ) ;
66+ }
67+ }
68+
69+ impl nohash_hasher:: IsEnabled for LocalStreamId { }
70+
6471/// A unique identifier used by the remote node for a substream.
6572///
6673/// `RemoteStreamId`s are received with frames from the remote
@@ -161,7 +168,7 @@ impl Codec {
161168
162169impl Decoder for Codec {
163170 type Item = Frame < RemoteStreamId > ;
164- type Error = IoError ;
171+ type Error = io :: Error ;
165172
166173 fn decode ( & mut self , src : & mut BytesMut ) -> Result < Option < Self :: Item > , Self :: Error > {
167174 loop {
@@ -182,7 +189,7 @@ impl Decoder for Codec {
182189 Some ( len) => {
183190 if len as usize > MAX_FRAME_SIZE {
184191 let msg = format ! ( "Mplex frame length {} exceeds maximum" , len) ;
185- return Err ( IoError :: new ( IoErrorKind :: InvalidData , msg) ) ;
192+ return Err ( io :: Error :: new ( io :: ErrorKind :: InvalidData , msg) ) ;
186193 }
187194
188195 self . decoder_state = CodecDecodeState :: HasHeaderAndLen ( header, len as usize ) ;
@@ -213,7 +220,7 @@ impl Decoder for Codec {
213220 6 => Frame :: Reset { stream_id : RemoteStreamId :: dialer ( num) } ,
214221 _ => {
215222 let msg = format ! ( "Invalid mplex header value 0x{:x}" , header) ;
216- return Err ( IoError :: new ( IoErrorKind :: InvalidData , msg) ) ;
223+ return Err ( io :: Error :: new ( io :: ErrorKind :: InvalidData , msg) ) ;
217224 } ,
218225 } ;
219226
@@ -222,7 +229,7 @@ impl Decoder for Codec {
222229 } ,
223230
224231 CodecDecodeState :: Poisoned => {
225- return Err ( IoError :: new ( IoErrorKind :: InvalidData , "Mplex codec poisoned" ) ) ;
232+ return Err ( io :: Error :: new ( io :: ErrorKind :: InvalidData , "Mplex codec poisoned" ) ) ;
226233 }
227234 }
228235 }
@@ -231,7 +238,7 @@ impl Decoder for Codec {
231238
232239impl Encoder for Codec {
233240 type Item = Frame < LocalStreamId > ;
234- type Error = IoError ;
241+ type Error = io :: Error ;
235242
236243 fn encode ( & mut self , item : Self :: Item , dst : & mut BytesMut ) -> Result < ( ) , Self :: Error > {
237244 let ( header, data) = match item {
@@ -266,7 +273,7 @@ impl Encoder for Codec {
266273 let data_len_bytes = encode:: usize ( data_len, & mut data_buf) ;
267274
268275 if data_len > MAX_FRAME_SIZE {
269- return Err ( IoError :: new ( IoErrorKind :: InvalidData , "data size exceed maximum" ) ) ;
276+ return Err ( io :: Error :: new ( io :: ErrorKind :: InvalidData , "data size exceed maximum" ) ) ;
270277 }
271278
272279 dst. reserve ( header_bytes. len ( ) + data_len_bytes. len ( ) + data_len) ;
0 commit comments