1
#![doc(
2
    html_logo_url = "https://raw.githubusercontent.com/sevki/jetstream/main/logo/JetStream.png"
3
)]
4
#![doc(
5
    html_favicon_url = "https://raw.githubusercontent.com/sevki/jetstream/main/logo/JetStream.png"
6
)]
7
#![cfg_attr(docsrs, feature(doc_cfg))]
8
#[cfg(feature = "std")]
9
use std::collections::{BTreeMap, BinaryHeap};
10
// Copyright (c) 2024, Sevki <s@sevki.io>
11
// Copyright 2018 The ChromiumOS Authors
12
// Use of this source code is governed by a BSD-style license that can be
13
// found in the LICENSE file.
14
use std::{
15
    collections::{BTreeSet, VecDeque},
16
    ffi::{CStr, CString, OsStr},
17
    fmt,
18
    hash::Hash,
19
    io::{self, ErrorKind, Read, Write},
20
    marker::PhantomData,
21
    mem,
22
    ops::{Deref, DerefMut},
23
    string::String,
24
    vec::Vec,
25
};
26

            
27
use bytes::Buf;
28
use hashbrown::{HashMap, HashSet};
29
pub use jetstream_macros::JetStreamWireFormat;
30
use zerocopy::LittleEndian;
31

            
32
pub mod wire_format_extensions;
33

            
34
#[cfg(target_arch = "wasm32")]
35
pub mod wasm;
36

            
37
/// A type that can be encoded on the wire using the 9P protocol.
38
#[cfg(not(target_arch = "wasm32"))]
39
pub trait WireFormat: Send {
40
    /// Returns the number of bytes necessary to fully encode `self`.
41
    fn byte_size(&self) -> u32;
42

            
43
    /// Encodes `self` into `writer`.
44
    fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()>
45
    where
46
        Self: Sized;
47

            
48
    /// Decodes `Self` from `reader`.
49
    fn decode<R: Read>(reader: &mut R) -> io::Result<Self>
50
    where
51
        Self: Sized;
52
}
53

            
54
/// A type that can be encoded on the wire using the 9P protocol.
55
/// WebAssembly doesn't fully support Send, so we don't require it.
56
#[cfg(target_arch = "wasm32")]
57
pub trait WireFormat: std::marker::Sized {
58
    /// Returns the number of bytes necessary to fully encode `self`.
59
    fn byte_size(&self) -> u32;
60

            
61
    /// Encodes `self` into `writer`.
62
    fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()>;
63

            
64
    /// Decodes `Self` from `reader`.
65
    fn decode<R: Read>(reader: &mut R) -> io::Result<Self>;
66
}
67

            
68
/// A 9P protocol string.
69
///
70
/// The string is always valid UTF-8 and 65535 bytes or less (enforced by `P9String::new()`).
71
///
72
/// It is represented as a C string with a terminating 0 (NUL) character to allow it to be passed
73
/// directly to libc functions.
74
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
75
pub struct P9String {
76
    cstr: CString,
77
}
78

            
79
impl P9String {
80
    pub fn new(string_bytes: impl Into<Vec<u8>>) -> io::Result<Self> {
81
        let string_bytes: Vec<u8> = string_bytes.into();
82

            
83
        if string_bytes.len() > u16::MAX as usize {
84
            return Err(io::Error::new(
85
                ErrorKind::InvalidInput,
86
                "string is too long",
87
            ));
88
        }
89

            
90
        // 9p strings must be valid UTF-8.
91
        let _check_utf8 = std::str::from_utf8(&string_bytes)
92
            .map_err(|e| io::Error::new(ErrorKind::InvalidInput, e))?;
93

            
94
        let cstr = CString::new(string_bytes)
95
            .map_err(|e| io::Error::new(ErrorKind::InvalidInput, e))?;
96

            
97
        Ok(P9String { cstr })
98
    }
99

            
100
    pub fn len(&self) -> usize {
101
        self.cstr.as_bytes().len()
102
    }
103

            
104
    pub fn is_empty(&self) -> bool {
105
        self.cstr.as_bytes().is_empty()
106
    }
107

            
108
    pub fn as_c_str(&self) -> &CStr {
109
        self.cstr.as_c_str()
110
    }
111

            
112
    pub fn as_bytes(&self) -> &[u8] {
113
        self.cstr.as_bytes()
114
    }
115

            
116
    #[cfg(not(target_arch = "wasm32"))]
117
    /// Returns a raw pointer to the string's storage.
118
    ///
119
    /// The string bytes are always followed by a NUL terminator ('\0'), so the pointer can be
120
    /// passed directly to libc functions that expect a C string.
121
    pub fn as_ptr(&self) -> *const libc::c_char {
122
        self.cstr.as_ptr()
123
    }
124

            
125
    #[cfg(target_arch = "wasm32")]
126
    /// Returns a raw pointer to the string's storage.
127
    ///
128
    /// The string bytes are always followed by a NUL terminator ('\0').
129
    /// Note: In WebAssembly, returns a raw pointer but libc is not available.
130
    pub fn as_ptr(&self) -> *const std::os::raw::c_char {
131
        self.cstr.as_ptr()
132
    }
133
}
134

            
135
impl PartialEq<&str> for P9String {
136
    fn eq(&self, other: &&str) -> bool {
137
        self.cstr.as_bytes() == other.as_bytes()
138
    }
139
}
140

            
141
impl TryFrom<&OsStr> for P9String {
142
    type Error = io::Error;
143

            
144
    fn try_from(value: &OsStr) -> io::Result<Self> {
145
        let string_bytes = value.as_encoded_bytes();
146
        Self::new(string_bytes)
147
    }
148
}
149

            
150
// The 9P protocol requires that strings are UTF-8 encoded.  The wire format is a u16
151
// count |N|, encoded in little endian, followed by |N| bytes of UTF-8 data.
152
impl WireFormat for P9String {
153
    fn byte_size(&self) -> u32 {
154
        (mem::size_of::<u16>() + self.len()) as u32
155
    }
156

            
157
    fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
158
        (self.len() as u16).encode(writer)?;
159
        writer.write_all(self.cstr.as_bytes())
160
    }
161

            
162
    fn decode<R: Read>(reader: &mut R) -> io::Result<Self> {
163
        let len: u16 = WireFormat::decode(reader)?;
164
        let mut string_bytes = vec![0u8; usize::from(len)];
165
        reader.read_exact(&mut string_bytes)?;
166
        Self::new(string_bytes)
167
    }
168
}
169

            
170
// This doesn't really _need_ to be a macro but unfortunately there is no trait bound to
171
// express "can be casted to another type", which means we can't write `T as u8` in a trait
172
// based implementation.  So instead we have this macro, which is implemented for all the
173
// stable unsigned types with the added benefit of not being implemented for the signed
174
// types which are not allowed by the protocol.
175
macro_rules! uint_wire_format_impl {
176
    ($Ty:ty) => {
177
        impl WireFormat for $Ty {
178
180
            fn byte_size(&self) -> u32 {
179
180
                mem::size_of::<$Ty>() as u32
180
180
            }
181

            
182
8596
            fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
183
8596
                writer.write_all(&self.to_le_bytes())
184
8596
            }
185

            
186
11986
            fn decode<R: Read>(reader: &mut R) -> io::Result<Self> {
187
11986
                let mut buf = [0; mem::size_of::<$Ty>()];
188
11986
                reader.read_exact(&mut buf)?;
189
                paste::expr! {
190
11986
                    let num: zerocopy::[<$Ty:snake:upper>]<LittleEndian> =  zerocopy::byteorder::[<$Ty:snake:upper>]::from_bytes(buf);
191
11986
                    Ok(num.get())
192
                }
193
11986
            }
194
        }
195
    };
196
}
197
// unsigned integers
198
uint_wire_format_impl!(u16);
199
uint_wire_format_impl!(u32);
200
uint_wire_format_impl!(u64);
201
uint_wire_format_impl!(u128);
202
// signed integers
203
uint_wire_format_impl!(i16);
204
uint_wire_format_impl!(i32);
205
uint_wire_format_impl!(i64);
206
uint_wire_format_impl!(i128);
207

            
208
macro_rules! float_wire_format_impl {
209
    ($Ty:ty) => {
210
        impl WireFormat for $Ty {
211
            fn byte_size(&self) -> u32 {
212
                mem::size_of::<$Ty>() as u32
213
            }
214

            
215
            fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
216
                paste::expr! {
217
                    writer.write_all(&self.to_le_bytes())
218
                }
219
            }
220

            
221
            fn decode<R: Read>(reader: &mut R) -> io::Result<Self> {
222
                let mut buf = [0; mem::size_of::<$Ty>()];
223
                reader.read_exact(&mut buf)?;
224
                paste::expr! {
225
                    let num: zerocopy::[<$Ty:snake:upper>]<LittleEndian> =  zerocopy::byteorder::[<$Ty:snake:upper>]::from_bytes(buf);
226
                    Ok(num.get())
227
                }
228
            }
229
        }
230
    };
231
}
232

            
233
float_wire_format_impl!(f32);
234
float_wire_format_impl!(f64);
235

            
236
macro_rules! tuple_wire_format_impl {
237
    ($( $name:ident ),+) => {
238
        #[allow(non_snake_case)]
239
        impl<$( $name ),+> WireFormat for ( $( $name ),+ )
240
        where
241
            $( $name: WireFormat ),+
242
        {
243
            fn byte_size(&self) -> u32 {
244
                let ( $( $name ),+ ) = self;
245
                0 $( + $name.byte_size() )+
246
            }
247

            
248
            fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
249
                let ( $( $name ),+ ) = self;
250
                $( $name.encode(writer)?; )+
251
                Ok(())
252
            }
253

            
254
            fn decode<R: Read>(reader: &mut R) -> io::Result<Self> {
255
                Ok((
256
                    $( $name::decode(reader)? ),+
257
                ))
258
            }
259
        }
260
    };
261
}
262

            
263
// Single-element tuple needs a hand-written impl because the macro expands
264
// `(A)` (parenthesized value) instead of `(A,)` (1-tuple).
265
#[allow(non_snake_case)]
266
impl<A: WireFormat> WireFormat for (A,) {
267
    fn byte_size(&self) -> u32 {
268
        self.0.byte_size()
269
    }
270

            
271
    fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
272
        self.0.encode(writer)
273
    }
274

            
275
    fn decode<R: Read>(reader: &mut R) -> io::Result<Self> {
276
        Ok((A::decode(reader)?,))
277
    }
278
}
279

            
280
tuple_wire_format_impl!(A, B);
281
tuple_wire_format_impl!(A, B, C);
282
tuple_wire_format_impl!(A, B, C, D);
283
tuple_wire_format_impl!(A, B, C, D, E);
284
tuple_wire_format_impl!(A, B, C, D, E, F);
285
tuple_wire_format_impl!(A, B, C, D, E, F, G);
286
tuple_wire_format_impl!(A, B, C, D, E, F, G, H);
287
tuple_wire_format_impl!(A, B, C, D, E, F, G, H, I);
288
tuple_wire_format_impl!(A, B, C, D, E, F, G, H, I, J);
289
tuple_wire_format_impl!(A, B, C, D, E, F, G, H, I, J, K);
290
tuple_wire_format_impl!(A, B, C, D, E, F, G, H, I, J, K, L);
291

            
292
impl WireFormat for u8 {
293
    fn byte_size(&self) -> u32 {
294
        1
295
    }
296

            
297
3463
    fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
298
3463
        writer.write_all(&[*self])
299
3463
    }
300

            
301
26
    fn decode<R: Read>(reader: &mut R) -> io::Result<Self> {
302
26
        let mut byte = [0u8; 1];
303
26
        reader.read_exact(&mut byte)?;
304
26
        Ok(byte[0])
305
26
    }
306
}
307

            
308
impl WireFormat for usize {
309
    fn byte_size(&self) -> u32 {
310
        mem::size_of::<usize>() as u32
311
    }
312

            
313
8
    fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
314
8
        writer.write_all(&self.to_le_bytes())
315
8
    }
316

            
317
8
    fn decode<R: Read>(reader: &mut R) -> io::Result<Self> {
318
8
        let mut buf = [0; mem::size_of::<usize>()];
319
8
        reader.read_exact(&mut buf)?;
320
8
        Ok(usize::from_le_bytes(buf))
321
8
    }
322
}
323

            
324
impl WireFormat for isize {
325
    fn byte_size(&self) -> u32 {
326
        mem::size_of::<isize>() as u32
327
    }
328

            
329
    fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
330
        writer.write_all(&self.to_le_bytes())
331
    }
332

            
333
    fn decode<R: Read>(reader: &mut R) -> io::Result<Self> {
334
        let mut buf = [0; mem::size_of::<isize>()];
335
        reader.read_exact(&mut buf)?;
336
        Ok(isize::from_le_bytes(buf))
337
    }
338
}
339

            
340
// The 9P protocol requires that strings are UTF-8 encoded.  The wire format is a u16
341
// count |N|, encoded in little endian, followed by |N| bytes of UTF-8 data.
342
impl WireFormat for String {
343
2196
    fn byte_size(&self) -> u32 {
344
2196
        (mem::size_of::<u16>() + self.len()) as u32
345
2196
    }
346

            
347
1722
    fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
348
1722
        if self.len() > u16::MAX as usize {
349
            return Err(io::Error::new(
350
                ErrorKind::InvalidInput,
351
                "string is too long",
352
            ));
353
1722
        }
354

            
355
1722
        (self.len() as u16).encode(writer)?;
356
1722
        writer.write_all(self.as_bytes())
357
1722
    }
358

            
359
1724
    fn decode<R: Read>(reader: &mut R) -> io::Result<Self> {
360
1724
        let len: u16 = WireFormat::decode(reader)?;
361
1724
        let mut result = String::with_capacity(len as usize);
362
1724
        reader.take(len as u64).read_to_string(&mut result)?;
363
1724
        Ok(result)
364
1724
    }
365
}
366

            
367
// The wire format for repeated types is similar to that of strings: a little endian
368
// encoded u16 |N|, followed by |N| instances of the given type.
369
impl<T: WireFormat> WireFormat for Vec<T> {
370
34
    fn byte_size(&self) -> u32 {
371
34
        mem::size_of::<u16>() as u32
372
34
            + self.iter().map(|elem| elem.byte_size()).sum::<u32>()
373
34
    }
374

            
375
8
    fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
376
8
        if self.len() > u16::MAX as usize {
377
            return Err(std::io::Error::new(
378
                std::io::ErrorKind::InvalidInput,
379
                "too many elements in vector",
380
            ));
381
8
        }
382

            
383
8
        (self.len() as u16).encode(writer)?;
384
22
        for elem in self {
385
18
            elem.encode(writer)?;
386
        }
387

            
388
8
        Ok(())
389
8
    }
390

            
391
6
    fn decode<R: Read>(reader: &mut R) -> io::Result<Self> {
392
6
        let len: u16 = WireFormat::decode(reader)?;
393
6
        let mut result = Vec::with_capacity(len as usize);
394

            
395
6
        for _ in 0..len {
396
10
            result.push(WireFormat::decode(reader)?);
397
        }
398

            
399
6
        Ok(result)
400
6
    }
401
}
402

            
403
/// A type that encodes an arbitrary number of bytes of data.  Typically used for Rread
404
/// Twrite messages.  This differs from a `Vec<u8>` in that it encodes the number of bytes
405
/// using a `u32` instead of a `u16`.
406
#[derive(PartialEq, Eq, Clone)]
407
#[repr(transparent)]
408
#[cfg_attr(feature = "testing", derive(serde::Serialize, serde::Deserialize))]
409
pub struct Data(pub Vec<u8>);
410

            
411
// The maximum length of a data buffer that we support.  In practice the server's max message
412
// size should prevent us from reading too much data so this check is mainly to ensure a
413
// malicious client cannot trick us into allocating massive amounts of memory.
414
const MAX_DATA_LENGTH: u32 = 32 * 1024 * 1024;
415

            
416
impl fmt::Debug for Data {
417
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
418
        // There may be a lot of data and we don't want to spew it all out in a trace.  Instead
419
        // just print out the number of bytes in the buffer.
420
        write!(f, "Data({} bytes)", self.len())
421
    }
422
}
423

            
424
// Implement Deref and DerefMut so that we don't have to use self.0 everywhere.
425
impl Deref for Data {
426
    type Target = Vec<u8>;
427

            
428
    fn deref(&self) -> &Self::Target {
429
        &self.0
430
    }
431
}
432
impl DerefMut for Data {
433
    fn deref_mut(&mut self) -> &mut Self::Target {
434
        &mut self.0
435
    }
436
}
437

            
438
// Same as Vec<u8> except that it encodes the length as a u32 instead of a u16.
439
impl WireFormat for Data {
440
    fn byte_size(&self) -> u32 {
441
        mem::size_of::<u32>() as u32
442
            + self.iter().map(|elem| elem.byte_size()).sum::<u32>()
443
    }
444

            
445
    fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
446
        if self.len() > u32::MAX as usize {
447
            return Err(std::io::Error::new(
448
                std::io::ErrorKind::InvalidInput,
449
                "data is too large",
450
            ));
451
        }
452
        (self.len() as u32).encode(writer)?;
453
        writer.write_all(self)
454
    }
455

            
456
    fn decode<R: Read>(reader: &mut R) -> io::Result<Self> {
457
        let len: u32 = WireFormat::decode(reader)?;
458
        if len > MAX_DATA_LENGTH {
459
            return Err(std::io::Error::new(
460
                std::io::ErrorKind::InvalidData,
461
                format!("data length ({} bytes) is too large", len),
462
            ));
463
        }
464

            
465
        let mut buf = Vec::with_capacity(len as usize);
466
        reader.take(len as u64).read_to_end(&mut buf)?;
467

            
468
        if buf.len() == len as usize {
469
            Ok(Data(buf))
470
        } else {
471
            Err(io::Error::new(
472
                std::io::ErrorKind::UnexpectedEof,
473
                format!(
474
                    "unexpected end of data: want: {} bytes, got: {} bytes",
475
                    len,
476
                    buf.len()
477
                ),
478
            ))
479
        }
480
    }
481
}
482

            
483
impl<T> WireFormat for Option<T>
484
where
485
    T: WireFormat,
486
{
487
51
    fn byte_size(&self) -> u32 {
488
51
        1 + match self {
489
34
            None => 0,
490
17
            Some(value) => value.byte_size(),
491
        }
492
51
    }
493

            
494
6
    fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
495
6
        match self {
496
4
            None => WireFormat::encode(&0u8, writer),
497
2
            Some(value) => {
498
2
                WireFormat::encode(&1u8, writer)?;
499
2
                WireFormat::encode(value, writer)
500
            }
501
        }
502
6
    }
503

            
504
6
    fn decode<R: Read>(reader: &mut R) -> io::Result<Self> {
505
6
        let tag: u8 = WireFormat::decode(reader)?;
506
6
        match tag {
507
4
            0 => Ok(None),
508
2
            1 => Ok(Some(WireFormat::decode(reader)?)),
509
            _ => Err(io::Error::new(
510
                io::ErrorKind::InvalidData,
511
                format!("Invalid Option tag: {}", tag),
512
            )),
513
        }
514
6
    }
515
}
516

            
517
impl WireFormat for () {
518
18
    fn byte_size(&self) -> u32 {
519
18
        0
520
18
    }
521

            
522
2
    fn encode<W: Write>(&self, _writer: &mut W) -> io::Result<()> {
523
2
        Ok(())
524
2
    }
525

            
526
2
    fn decode<R: Read>(_reader: &mut R) -> io::Result<Self> {
527
2
        Ok(())
528
2
    }
529
}
530

            
531
impl WireFormat for bool {
532
    fn byte_size(&self) -> u32 {
533
        1
534
    }
535

            
536
    fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
537
        writer.write_all(&[*self as u8])
538
    }
539

            
540
    fn decode<R: Read>(reader: &mut R) -> io::Result<Self> {
541
        let mut byte = [0u8; 1];
542
        reader.read_exact(&mut byte)?;
543
        match byte[0] {
544
            0 => Ok(false),
545
            1 => Ok(true),
546
            _ => Err(io::Error::new(
547
                io::ErrorKind::InvalidData,
548
                "invalid byte for bool",
549
            )),
550
        }
551
    }
552
}
553

            
554
impl io::Read for Data {
555
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
556
        self.0.reader().read(buf)
557
    }
558
}
559

            
560
#[repr(transparent)]
561
pub struct Wrapped<T, I>(pub T, PhantomData<I>);
562

            
563
impl<T, I> Wrapped<T, I> {
564
    pub fn new(value: T) -> Self {
565
        Wrapped(value, PhantomData)
566
    }
567
}
568

            
569
#[cfg(not(target_arch = "wasm32"))]
570
impl<T, I> WireFormat for Wrapped<T, I>
571
where
572
    T: Send + std::convert::AsRef<I>,
573
    I: WireFormat + std::convert::Into<T>,
574
{
575
    fn byte_size(&self) -> u32 {
576
        AsRef::<I>::as_ref(&self.0).byte_size()
577
    }
578

            
579
    fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
580
        AsRef::<I>::as_ref(&self.0).encode(writer)
581
    }
582

            
583
    fn decode<R: Read>(reader: &mut R) -> io::Result<Self> {
584
        let inner = I::decode(reader)?;
585
        Ok(Wrapped(inner.into(), PhantomData))
586
    }
587
}
588

            
589
#[cfg(target_arch = "wasm32")]
590
impl<T, I> WireFormat for Wrapped<T, I>
591
where
592
    T: std::convert::AsRef<I>,
593
    I: WireFormat + std::convert::Into<T>,
594
{
595
    fn byte_size(&self) -> u32 {
596
        AsRef::<I>::as_ref(&self.0).byte_size()
597
    }
598

            
599
    fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()> {
600
        AsRef::<I>::as_ref(&self.0).encode(writer)
601
    }
602

            
603
    fn decode<R: Read>(reader: &mut R) -> io::Result<Self> {
604
        let inner = I::decode(reader)?;
605
        Ok(Wrapped(inner.into(), PhantomData))
606
    }
607
}
608

            
609
impl<T: WireFormat> WireFormat for Box<T> {
610
17
    fn byte_size(&self) -> u32 {
611
17
        (**self).byte_size()
612
17
    }
613

            
614
2
    fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()>
615
2
    where
616
2
        Self: Sized,
617
    {
618
2
        (**self).encode(writer)
619
2
    }
620

            
621
    fn decode<R: Read>(reader: &mut R) -> io::Result<Self>
622
    where
623
        Self: Sized,
624
    {
625
        let inner = T::decode(reader)?;
626
        Ok(Box::new(inner))
627
    }
628
}
629

            
630
impl<T: WireFormat + Send + Sync + Eq + Hash> WireFormat for HashSet<T> {
631
    fn byte_size(&self) -> u32 {
632
        self.iter().fold(0, |acc, v| acc + v.byte_size()) + 2
633
    }
634

            
635
    fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()>
636
    where
637
        Self: Sized,
638
    {
639
        if self.len() > u16::MAX as usize {
640
            return Err(io::Error::new(io::ErrorKind::Other, "Set too large"));
641
        }
642
        (self.len() as u16).encode(writer)?;
643
        for v in self.iter() {
644
            v.encode(writer)?;
645
        }
646
        Ok(())
647
    }
648

            
649
    fn decode<R: Read>(reader: &mut R) -> io::Result<Self>
650
    where
651
        Self: Sized,
652
    {
653
        let len: u16 = WireFormat::decode(reader)?;
654
        let mut set = Self::with_capacity(len as usize);
655
        for _ in 0..len {
656
            let v = T::decode(reader)?;
657
            set.insert(v);
658
        }
659
        Ok(set)
660
    }
661
}
662

            
663
impl<
664
        K: WireFormat + Send + Sync + Ord + Eq + Hash,
665
        V: WireFormat + Send + Sync,
666
    > WireFormat for HashMap<K, V>
667
{
668
    fn byte_size(&self) -> u32 {
669
        self.iter()
670
            .fold(0, |acc, (k, v)| acc + k.byte_size() + v.byte_size())
671
            + 2
672
    }
673

            
674
    fn encode<W: io::Write>(&self, writer: &mut W) -> io::Result<()>
675
    where
676
        Self: Sized,
677
    {
678
        if self.len() > u16::MAX as usize {
679
            return Err(io::Error::new(
680
                io::ErrorKind::InvalidInput,
681
                "Map too large",
682
            ));
683
        }
684
        (self.len() as u16).encode(writer)?;
685
        for (k, v) in self {
686
            k.encode(writer)?;
687
            v.encode(writer)?;
688
        }
689
        Ok(())
690
    }
691

            
692
    fn decode<R: io::Read>(reader: &mut R) -> io::Result<Self>
693
    where
694
        Self: Sized,
695
    {
696
        let len: u16 = WireFormat::decode(reader)?;
697
        let mut map = HashMap::new();
698
        for _ in 0..len {
699
            let k = K::decode(reader)?;
700
            let v = V::decode(reader)?;
701
            map.insert(k, v);
702
        }
703
        Ok(map)
704
    }
705
}
706

            
707
#[cfg(feature = "std")]
708
impl<K: WireFormat + Send + Sync + Ord, V: WireFormat + Send + Sync> WireFormat
709
    for BTreeMap<K, V>
710
{
711
    fn byte_size(&self) -> u32 {
712
        self.iter()
713
            .fold(0, |acc, (k, v)| acc + k.byte_size() + v.byte_size())
714
            + 2
715
    }
716

            
717
    fn encode<W: io::Write>(&self, writer: &mut W) -> io::Result<()>
718
    where
719
        Self: Sized,
720
    {
721
        if self.len() > u16::MAX as usize {
722
            return Err(io::Error::new(
723
                io::ErrorKind::InvalidInput,
724
                "Map too large",
725
            ));
726
        }
727
        let len = self.len() as u16;
728
        len.encode(writer)?;
729
        for (k, v) in self {
730
            k.encode(writer)?;
731
            v.encode(writer)?;
732
        }
733
        Ok(())
734
    }
735

            
736
    fn decode<R: io::Read>(reader: &mut R) -> io::Result<Self>
737
    where
738
        Self: Sized,
739
    {
740
        let len: u16 = WireFormat::decode(reader)?;
741
        let mut map = BTreeMap::new();
742
        for _ in 0..len {
743
            let k = K::decode(reader)?;
744
            let v = V::decode(reader)?;
745
            map.insert(k, v);
746
        }
747
        Ok(map)
748
    }
749
}
750

            
751
#[cfg(feature = "std")]
752
impl<V: WireFormat + Send + Sync + Ord> WireFormat for BinaryHeap<V> {
753
    fn byte_size(&self) -> u32 {
754
        self.as_slice()
755
            .iter()
756
            .fold(0, |acc, elem| acc + elem.byte_size())
757
            + 2
758
    }
759

            
760
    fn encode<W: io::Write>(&self, writer: &mut W) -> io::Result<()>
761
    where
762
        Self: Sized,
763
    {
764
        if self.len() > u16::MAX as usize {
765
            return Err(io::Error::new(
766
                io::ErrorKind::InvalidInput,
767
                "Map too large",
768
            ));
769
        }
770
        let len = self.len() as u16;
771
        len.encode(writer)?;
772
        for elem in self {
773
            elem.encode(writer)?;
774
        }
775
        Ok(())
776
    }
777

            
778
    fn decode<R: io::Read>(reader: &mut R) -> io::Result<Self>
779
    where
780
        Self: Sized,
781
    {
782
        let len: u16 = WireFormat::decode(reader)?;
783
        let mut heap = BinaryHeap::new();
784
        for _ in 0..len {
785
            let elem = V::decode(reader)?;
786
            heap.push(elem);
787
        }
788
        Ok(heap)
789
    }
790
}
791

            
792
impl<V: WireFormat + Send + Sync + Ord> WireFormat for VecDeque<V> {
793
    fn byte_size(&self) -> u32 {
794
        self.iter().fold(0, |acc, elem| acc + elem.byte_size()) + 2
795
    }
796

            
797
    fn encode<W: io::Write>(&self, writer: &mut W) -> io::Result<()>
798
    where
799
        Self: Sized,
800
    {
801
        if self.len() > u16::MAX as usize {
802
            return Err(io::Error::new(
803
                io::ErrorKind::InvalidInput,
804
                "Map too large",
805
            ));
806
        }
807
        let len = self.len() as u16;
808
        len.encode(writer)?;
809
        for elem in self {
810
            elem.encode(writer)?;
811
        }
812
        Ok(())
813
    }
814

            
815
    fn decode<R: io::Read>(reader: &mut R) -> io::Result<Self>
816
    where
817
        Self: Sized,
818
    {
819
        let len: u16 = WireFormat::decode(reader)?;
820
        let mut deque = VecDeque::with_capacity(len as usize);
821
        for _ in 0..len {
822
            let elem = V::decode(reader)?;
823
            deque.push_back(elem);
824
        }
825
        Ok(deque)
826
    }
827
}
828

            
829
impl<V: WireFormat + Send + Sync + Ord> WireFormat for BTreeSet<V> {
830
    fn byte_size(&self) -> u32 {
831
        self.iter().fold(0, |acc, elem| acc + elem.byte_size()) + 2
832
    }
833

            
834
    fn encode<W: io::Write>(&self, writer: &mut W) -> io::Result<()>
835
    where
836
        Self: Sized,
837
    {
838
        if self.len() > u16::MAX as usize {
839
            return Err(io::Error::new(
840
                io::ErrorKind::InvalidInput,
841
                "Map too large",
842
            ));
843
        }
844
        let len = self.len() as u16;
845
        len.encode(writer)?;
846
        for elem in self {
847
            elem.encode(writer)?;
848
        }
849
        Ok(())
850
    }
851

            
852
    fn decode<R: io::Read>(reader: &mut R) -> io::Result<Self>
853
    where
854
        Self: Sized,
855
    {
856
        let len: u16 = WireFormat::decode(reader)?;
857
        let mut set = BTreeSet::new();
858
        for _ in 0..len {
859
            let elem = V::decode(reader)?;
860
            set.insert(elem);
861
        }
862
        Ok(set)
863
    }
864
}
865

            
866
impl<T: WireFormat> WireFormat for PhantomData<T> {
867
    fn byte_size(&self) -> u32 {
868
        0
869
    }
870

            
871
    fn encode<W: io::Write>(&self, _writer: &mut W) -> io::Result<()>
872
    where
873
        Self: Sized,
874
    {
875
        Ok(())
876
    }
877

            
878
    fn decode<R: io::Read>(_reader: &mut R) -> io::Result<Self>
879
    where
880
        Self: Sized,
881
    {
882
        Ok(PhantomData)
883
    }
884
}
885

            
886
impl WireFormat for url::Url {
887
    fn byte_size(&self) -> u32 {
888
        self.to_string().byte_size()
889
    }
890

            
891
    fn encode<W: Write>(&self, writer: &mut W) -> io::Result<()>
892
    where
893
        Self: Sized,
894
    {
895
        self.to_string().encode(writer)
896
    }
897

            
898
    fn decode<R: Read>(reader: &mut R) -> io::Result<Self>
899
    where
900
        Self: Sized,
901
    {
902
        let string = String::decode(reader)?;
903
        url::Url::parse(&string)
904
            .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
905
    }
906
}