1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
//! Attributes that are defined in [RFC 5766 -- 14. New STUN Attributes].
//!
//! [RFC 5766 -- 14. New STUN Attributes]: https://tools.ietf.org/html/rfc5766#section-14

use bytecodec::bytes::{BytesEncoder, RemainingBytesDecoder};
use bytecodec::fixnum::{
    U32beDecoder, U32beEncoder, U64beDecoder, U64beEncoder, U8Decoder, U8Encoder,
};
use bytecodec::null::{NullDecoder, NullEncoder};
use bytecodec::{ByteCount, Decode, Encode, Eos, ErrorKind, Result, SizedEncode, TryTaggedDecode};
use std::net::SocketAddr;
use std::time::Duration;

use attribute::{Attribute, AttributeType};
use message::Message;
use net::{socket_addr_xor, SocketAddrDecoder, SocketAddrEncoder};

macro_rules! impl_decode {
    ($decoder:ty, $item:ident, $and_then:expr) => {
        impl Decode for $decoder {
            type Item = $item;

            fn decode(&mut self, buf: &[u8], eos: Eos) -> Result<usize> {
                track!(self.0.decode(buf, eos))
            }

            fn finish_decoding(&mut self) -> Result<Self::Item> {
                track!(self.0.finish_decoding()).and_then($and_then)
            }

            fn requiring_bytes(&self) -> ByteCount {
                self.0.requiring_bytes()
            }

            fn is_idle(&self) -> bool {
                self.0.is_idle()
            }
        }
        impl TryTaggedDecode for $decoder {
            type Tag = AttributeType;

            fn try_start_decoding(&mut self, attr_type: Self::Tag) -> Result<bool> {
                Ok(attr_type.as_u16() == $item::CODEPOINT)
            }
        }
    };
}

macro_rules! impl_encode {
    ($encoder:ty, $item:ty, $map_from:expr) => {
        impl Encode for $encoder {
            type Item = $item;

            fn encode(&mut self, buf: &mut [u8], eos: Eos) -> Result<usize> {
                track!(self.0.encode(buf, eos))
            }

            fn start_encoding(&mut self, item: Self::Item) -> Result<()> {
                track!(self.0.start_encoding($map_from(item)))
            }

            fn requiring_bytes(&self) -> ByteCount {
                self.0.requiring_bytes()
            }

            fn is_idle(&self) -> bool {
                self.0.is_idle()
            }
        }
        impl SizedEncode for $encoder {
            fn exact_requiring_bytes(&self) -> u64 {
                self.0.exact_requiring_bytes()
            }
        }
    };
}

/// `CHANNEL-NUMBER` attribute.
///
/// See [RFC 5766 -- 14.1. CHANNEL-NUMBER] about this attribute.
///
/// [RFC 5766 -- 14.1. CHANNEL-NUMBER]: https://tools.ietf.org/html/rfc5766#section-14.1
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ChannelNumber(u16);
impl ChannelNumber {
    /// The codepoint of the type of the attribute.
    pub const CODEPOINT: u16 = 0x000C;

    /// Minimum channel number.
    pub const MIN: u16 = 0x4000;

    /// Maximum channel number.
    pub const MAX: u16 = 0x7FFF;

    /// Makes a new `ChannelNumber` instance.
    ///
    ///
    /// # Errors
    ///
    /// If `n` is not a number between `ChannelNumber::MIN` and `ChannelNumber::MAX`,
    /// this will return an `ErrorKind::InvalidInput` error.
    pub fn new(n: u16) -> Result<Self> {
        track_assert!(n >= Self::MIN, ErrorKind::InvalidInput; n);
        track_assert!(n <= Self::MAX, ErrorKind::InvalidInput; n);
        Ok(ChannelNumber(n))
    }

    /// Returns the channel number indicated by the attribute.
    pub fn value(self) -> u16 {
        self.0
    }
}
impl Attribute for ChannelNumber {
    type Decoder = ChannelNumberDecoder;
    type Encoder = ChannelNumberEncoder;

    fn get_type(&self) -> AttributeType {
        AttributeType::new(Self::CODEPOINT)
    }
}

/// [`ChannelNumber`] decoder.
///
/// [`ChannelNumber`]: ./struct.ChannelNumber.html
#[derive(Debug, Default)]
pub struct ChannelNumberDecoder(U32beDecoder);
impl ChannelNumberDecoder {
    /// Makes a new `ChannelNumberDecoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_decode!(ChannelNumberDecoder, ChannelNumber, |item| track!(
    ChannelNumber::new((item >> 16) as u16)
));

/// [`ChannelNumber`] encoder.
///
/// [`ChannelNumber`]: ./struct.ChannelNumber.html
#[derive(Debug, Default)]
pub struct ChannelNumberEncoder(U32beEncoder);
impl ChannelNumberEncoder {
    /// Makes a new `ChannelNumberEncoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_encode!(
    ChannelNumberEncoder,
    ChannelNumber,
    |item: Self::Item| u32::from(item.0) << 16
);

/// `LIFETIME` attribute.
///
/// See [RFC 5766 -- 14.2. LIFETIME] about this attribute.
///
/// [RFC 5766 -- 14.2. LIFETIME]: https://tools.ietf.org/html/rfc5766#section-14.2
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Lifetime(Duration);
impl Lifetime {
    /// The codepoint of the type of the attribute.
    pub const CODEPOINT: u16 = 0x000D;

    /// Makes a new `Lifetime` instance.
    ///
    /// Note that the nanoseconds part of `lifetime` is ignored and always set to `0`.
    ///
    /// # Errors
    ///
    /// If the seconds part of `lifetime` is greater than `0xFFFF_FFFF`,
    /// this function will return an `ErrorKind::InvalidInput` error.
    pub fn new(lifetime: Duration) -> Result<Self> {
        let lifetime_seconds = lifetime.as_secs();
        track_assert!(lifetime_seconds <= 0xFFFF_FFFF, ErrorKind::InvalidInput);
        Ok(Lifetime(Duration::from_secs(lifetime_seconds)))
    }

    /// Makes a new `Lifetime` instance from `u32` value.
    pub fn from_u32(lifetime_seconds: u32) -> Self {
        Lifetime(Duration::from_secs(u64::from(lifetime_seconds)))
    }

    /// Returns the lifetime indicated by the attribute.
    pub fn lifetime(&self) -> Duration {
        self.0
    }
}
impl Attribute for Lifetime {
    type Decoder = LifetimeDecoder;
    type Encoder = LifetimeEncoder;

    fn get_type(&self) -> AttributeType {
        AttributeType::new(Self::CODEPOINT)
    }
}

/// [`Lifetime`] decoder.
///
/// [`Lifetime`]: ./struct.Lifetime.html
#[derive(Debug, Default)]
pub struct LifetimeDecoder(U32beDecoder);
impl LifetimeDecoder {
    /// Makes a new `LifetimeDecoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_decode!(LifetimeDecoder, Lifetime, |item| Ok(Lifetime(
    Duration::from_secs(u64::from(item))
)));

/// [`Lifetime`] encoder.
///
/// [`Lifetime`]: ./struct.Lifetime.html
#[derive(Debug, Default)]
pub struct LifetimeEncoder(U32beEncoder);
impl LifetimeEncoder {
    /// Makes a new `LifetimeEncoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_encode!(
    LifetimeEncoder,
    Lifetime,
    |item: Self::Item| item.0.as_secs() as u32
);

/// `XOR-PEER-ADDRESS` attribute.
///
/// See [RFC 5766 -- 14.3. XOR-PEER-ADDRESS] about this attribute.
///
/// [RFC 5766 -- 14.3. XOR-PEER-ADDRESS]: https://tools.ietf.org/html/rfc5766#section-14.3
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct XorPeerAddress(SocketAddr);
impl XorPeerAddress {
    /// The codepoint of the type of the attribute.
    pub const CODEPOINT: u16 = 0x0012;

    /// Makes a new `XorPeerAddress` instance.
    pub fn new(addr: SocketAddr) -> Self {
        XorPeerAddress(addr)
    }

    /// Returns the address specified by the attribute.
    pub fn address(&self) -> SocketAddr {
        self.0
    }
}
impl Attribute for XorPeerAddress {
    type Decoder = XorPeerAddressDecoder;
    type Encoder = XorPeerAddressEncoder;

    fn get_type(&self) -> AttributeType {
        AttributeType::new(Self::CODEPOINT)
    }

    fn before_encode<A: Attribute>(&mut self, message: &Message<A>) -> Result<()> {
        self.0 = socket_addr_xor(self.0, message.transaction_id());
        Ok(())
    }

    fn after_decode<A: Attribute>(&mut self, message: &Message<A>) -> Result<()> {
        self.0 = socket_addr_xor(self.0, message.transaction_id());
        Ok(())
    }
}

/// [`XorPeerAddress`] decoder.
///
/// [`XorPeerAddress`]: ./struct.XorPeerAddress.html
#[derive(Debug, Default)]
pub struct XorPeerAddressDecoder(SocketAddrDecoder);
impl XorPeerAddressDecoder {
    /// Makes a new `XorPeerAddressDecoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_decode!(XorPeerAddressDecoder, XorPeerAddress, |item| Ok(
    XorPeerAddress(item)
));

/// [`XorPeerAddress`] encoder.
///
/// [`XorPeerAddress`]: ./struct.XorPeerAddress.html
#[derive(Debug, Default)]
pub struct XorPeerAddressEncoder(SocketAddrEncoder);
impl XorPeerAddressEncoder {
    /// Makes a new `XorPeerAddressEncoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_encode!(XorPeerAddressEncoder, XorPeerAddress, |item: Self::Item| {
    item.0
});

/// `DATA` attribute.
///
/// See [RFC 5766 -- 14.4. DATA] about this attribute.
///
/// [RFC 5766 -- 14.4. DATA]: https://tools.ietf.org/html/rfc5766#section-14.4
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Data(Vec<u8>);
impl Data {
    /// The codepoint of the type of the attribute.
    pub const CODEPOINT: u16 = 0x0013;

    /// Makes a new `Data` instance.
    ///
    /// # Errors
    ///
    /// If the length of `data` is greater than `0xFFFF`,
    /// this function will return an `ErrorKind::InvalidInput` error.
    pub fn new(data: Vec<u8>) -> Result<Self> {
        track_assert!(data.len() <= 0xFFFF, ErrorKind::InvalidInput);
        Ok(Data(data))
    }

    /// Returns a reference the data held by the attribute.
    pub fn data(&self) -> &[u8] {
        &self.0
    }
}
impl Attribute for Data {
    type Decoder = DataDecoder;
    type Encoder = DataEncoder;

    fn get_type(&self) -> AttributeType {
        AttributeType::new(Self::CODEPOINT)
    }
}

/// [`Data`] decoder.
///
/// [`Data`]: ./struct.Data.html
#[derive(Debug, Default)]
pub struct DataDecoder(RemainingBytesDecoder);
impl DataDecoder {
    /// Makes a new `DataDecoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_decode!(DataDecoder, Data, |item| Ok(Data(item)));

/// [`Data`] encoder.
///
/// [`Data`]: ./struct.Data.html
#[derive(Debug, Default)]
pub struct DataEncoder(BytesEncoder);
impl DataEncoder {
    /// Makes a new `DataEncoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_encode!(DataEncoder, Data, |item: Self::Item| item.0);

/// `XOR-RELAY-ADDRESS` attribute.
///
/// See [RFC 5766 -- 14.5. XOR-RELAY-ADDRESS] about this attribute.
///
/// [RFC 5766 -- 14.5. XOR-RELAY-ADDRESS]: https://tools.ietf.org/html/rfc5766#section-14.5
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct XorRelayAddress(SocketAddr);
impl XorRelayAddress {
    /// The codepoint of the type of the attribute.
    pub const CODEPOINT: u16 = 0x0016;

    /// Makes a new `XorRelayAddress` instance.
    pub fn new(addr: SocketAddr) -> Self {
        XorRelayAddress(addr)
    }

    /// Returns the address specified by the attribute.
    pub fn address(&self) -> SocketAddr {
        self.0
    }
}
impl Attribute for XorRelayAddress {
    type Decoder = XorRelayAddressDecoder;
    type Encoder = XorRelayAddressEncoder;

    fn get_type(&self) -> AttributeType {
        AttributeType::new(Self::CODEPOINT)
    }

    fn before_encode<A: Attribute>(&mut self, message: &Message<A>) -> Result<()> {
        self.0 = socket_addr_xor(self.0, message.transaction_id());
        Ok(())
    }

    fn after_decode<A: Attribute>(&mut self, message: &Message<A>) -> Result<()> {
        self.0 = socket_addr_xor(self.0, message.transaction_id());
        Ok(())
    }
}

/// [`XorRelayAddress`] decoder.
///
/// [`XorRelayAddress`]: ./struct.XorRelayAddress.html
#[derive(Debug, Default)]
pub struct XorRelayAddressDecoder(SocketAddrDecoder);
impl XorRelayAddressDecoder {
    /// Makes a new `XorRelayAddressDecoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_decode!(XorRelayAddressDecoder, XorRelayAddress, |item| Ok(
    XorRelayAddress(item)
));

/// [`XorRelayAddress`] encoder.
///
/// [`XorRelayAddress`]: ./struct.XorRelayAddress.html
#[derive(Debug, Default)]
pub struct XorRelayAddressEncoder(SocketAddrEncoder);
impl XorRelayAddressEncoder {
    /// Makes a new `XorRelayAddressEncoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_encode!(
    XorRelayAddressEncoder,
    XorRelayAddress,
    |item: Self::Item| item.0
);

/// `EVEN-PORT` attribute.
///
/// See [RFC 5766 -- 14.6. EVEN-PORT] about this attribute.
///
/// [RFC 5766 -- 14.6. EVEN-PORT]: https://tools.ietf.org/html/rfc5766#section-14.6
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EvenPort(bool);
impl EvenPort {
    /// The codepoint of the type of the attribute.
    pub const CODEPOINT: u16 = 0x0018;

    /// Makes a new `EvenPort` instance.
    pub fn new(is_requested: bool) -> Self {
        EvenPort(is_requested)
    }

    /// Returns whether the client requested that the port in the relayed transport address be even.
    pub fn is_requested(&self) -> bool {
        self.0
    }
}
impl Attribute for EvenPort {
    type Decoder = EvenPortDecoder;
    type Encoder = EvenPortEncoder;

    fn get_type(&self) -> AttributeType {
        AttributeType::new(Self::CODEPOINT)
    }
}

/// [`EvenPort`] decoder.
///
/// [`EvenPort`]: ./struct.EvenPort.html
#[derive(Debug, Default)]
pub struct EvenPortDecoder(U8Decoder);
impl EvenPortDecoder {
    /// Makes a new `EvenPortDecoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_decode!(EvenPortDecoder, EvenPort, |item| Ok(EvenPort(
    (item & 0b1000_0000) != 0
)));

/// [`EvenPort`] encoder.
///
/// [`EvenPort`]: ./struct.EvenPort.html
#[derive(Debug, Default)]
pub struct EvenPortEncoder(U8Encoder);
impl EvenPortEncoder {
    /// Makes a new `EvenPortEncoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_encode!(EvenPortEncoder, EvenPort, |item: Self::Item| u8::from(
    item.0
) << 7);

/// `REQUESTED-TRANSPORT` attribute.
///
/// See [RFC 5766 -- 14.7. REQUESTED-TRANSPORT] about this attribute.
///
/// [RFC 5766 -- 14.7. REQUESTED-TRANSPORT]: https://tools.ietf.org/html/rfc5766#section-14.7
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RequestedTransport(u8);
impl RequestedTransport {
    /// The codepoint of the type of the attribute.
    pub const CODEPOINT: u16 = 0x0019;

    /// Makes a new `RequestedTransport` instance.
    pub fn new(protocol: u8) -> Self {
        RequestedTransport(protocol)
    }

    /// Returns the transport protocol requested by the client.
    pub fn protocol(&self) -> u8 {
        self.0
    }
}
impl Attribute for RequestedTransport {
    type Decoder = RequestedTransportDecoder;
    type Encoder = RequestedTransportEncoder;

    fn get_type(&self) -> AttributeType {
        AttributeType::new(Self::CODEPOINT)
    }
}

/// [`RequestedTransport`] decoder.
///
/// [`RequestedTransport`]: ./struct.RequestedTransport.html
#[derive(Debug, Default)]
pub struct RequestedTransportDecoder(U32beDecoder);
impl RequestedTransportDecoder {
    /// Makes a new `RequestedTransportDecoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_decode!(RequestedTransportDecoder, RequestedTransport, |item| Ok(
    RequestedTransport((item >> 24) as u8)
));

/// [`RequestedTransport`] encoder.
///
/// [`RequestedTransport`]: ./struct.RequestedTransport.html
#[derive(Debug, Default)]
pub struct RequestedTransportEncoder(U32beEncoder);
impl RequestedTransportEncoder {
    /// Makes a new `RequestedTransportEncoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_encode!(
    RequestedTransportEncoder,
    RequestedTransport,
    |item: Self::Item| u32::from(item.0) << 24
);

/// `DONT-FRAGMENT` attribute.
///
/// See [RFC 5766 -- 14.8. DONT-FRAGMENT] about this attribute.
///
/// [RFC 5766 -- 14.8. DONT-FRAGMENT]: https://tools.ietf.org/html/rfc5766#section-14.8
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct DontFragment;
impl DontFragment {
    /// The codepoint of the type of the attribute.
    pub const CODEPOINT: u16 = 0x001A;
}
impl Attribute for DontFragment {
    type Decoder = DontFragmentDecoder;
    type Encoder = DontFragmentEncoder;

    fn get_type(&self) -> AttributeType {
        AttributeType::new(Self::CODEPOINT)
    }
}

/// [`DontFragment`] decoder.
///
/// [`DontFragment`]: ./struct.DontFragment.html
#[derive(Debug, Default)]
pub struct DontFragmentDecoder(NullDecoder);
impl DontFragmentDecoder {
    /// Makes a new `DontFragmentDecoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_decode!(DontFragmentDecoder, DontFragment, |()| Ok(DontFragment));

/// [`DontFragment`] encoder.
///
/// [`DontFragment`]: ./struct.DontFragment.html
#[derive(Debug, Default)]
pub struct DontFragmentEncoder(NullEncoder);
impl DontFragmentEncoder {
    /// Makes a new `DontFragmentEncoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_encode!(DontFragmentEncoder, DontFragment, |_: Self::Item| ());

/// `RESERVATION-TOKEN` attribute.
///
/// See [RFC 5766 -- 14.9. RESERVATION-TOKEN] about this attribute.
///
/// [RFC 5766 -- 14.9. RESERVATION-TOKEN]: https://tools.ietf.org/html/rfc5766#section-14.9
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ReservationToken(u64);
impl ReservationToken {
    /// The codepoint of the type of the attribute.
    pub const CODEPOINT: u16 = 0x0022;

    /// Makes a new `ReservationToken` instance.
    pub fn new(token: u64) -> Self {
        ReservationToken(token)
    }

    /// Returns the token value contained by the attribute.
    pub fn token(&self) -> u64 {
        self.0
    }
}
impl Attribute for ReservationToken {
    type Decoder = ReservationTokenDecoder;
    type Encoder = ReservationTokenEncoder;

    fn get_type(&self) -> AttributeType {
        AttributeType::new(Self::CODEPOINT)
    }
}

/// [`ReservationToken`] decoder.
///
/// [`ReservationToken`]: ./struct.ReservationToken.html
#[derive(Debug, Default)]
pub struct ReservationTokenDecoder(U64beDecoder);
impl ReservationTokenDecoder {
    /// Makes a new `ReservationTokenDecoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_decode!(ReservationTokenDecoder, ReservationToken, |item| Ok(
    ReservationToken(item)
));

/// [`ReservationToken`] encoder.
///
/// [`ReservationToken`]: ./struct.ReservationToken.html
#[derive(Debug, Default)]
pub struct ReservationTokenEncoder(U64beEncoder);
impl ReservationTokenEncoder {
    /// Makes a new `ReservationTokenEncoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_encode!(
    ReservationTokenEncoder,
    ReservationToken,
    |item: Self::Item| item.0
);