orcinus 0.3.0

async-ready mysql protocol implementation / wrapper libraries
Documentation
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
//! Protocol Serialization Format Fragments

use std::io::Read;

use futures_util::{future::BoxFuture, FutureExt, TryFutureExt};
use tokio::io::AsyncRead;

use crate::async_utils::{ReadBytesF, ReadF, ReadFixedBytesF, ReadLengthEncodedIntegerF, ReadNullTerminatedStringF};

/// A fragment of Protocol Format
///
/// Most protocols are constructed as group of Format Fragments.
/// This allows unified implementation of readers both synchronous and asynchronous.
pub trait ProtocolFormatFragment {
    /// Output value type of the format
    type Output;

    /// Reads a value
    fn read_sync(self, reader: impl Read) -> std::io::Result<Self::Output>;

    /// `(<$>)` operator
    #[inline]
    fn map<F, R>(self, mapper: F) -> Mapped<Self, F>
    where
        Self: Sized,
        F: FnOnce(Self::Output) -> R,
    {
        Mapped(self, mapper)
    }

    /// Asserts the value using [`assert_eq!`]
    #[inline]
    fn assert_eq(self, right: Self::Output) -> AssertEq<Self>
    where
        Self: Sized,
        Self::Output: std::fmt::Debug + Eq,
    {
        AssertEq(self, right)
    }
}

/// Asynchronous reader implementation for Format Fragments
pub trait AsyncProtocolFormatFragment<'r, R: 'r + Send>: ProtocolFormatFragment {
    /// Future type of reading the format
    type ReaderF: std::future::Future<Output = std::io::Result<Self::Output>> + Send + 'r;

    /// Read a value asynchronously
    fn read_format(self, reader: R) -> Self::ReaderF;
}

/// u8(byte) format
pub struct U8;
impl ProtocolFormatFragment for U8 {
    type Output = u8;

    #[inline]
    fn read_sync(self, mut reader: impl Read) -> std::io::Result<Self::Output> {
        let mut b = [0u8; 1];
        reader.read_exact(&mut b)?;
        Ok(b[0])
    }
}
impl<'r, R> AsyncProtocolFormatFragment<'r, R> for U8
where
    R: AsyncRead + Send + Unpin + 'r,
{
    type ReaderF = ReadF<1, R, u8>;

    #[inline]
    fn read_format(self, reader: R) -> Self::ReaderF {
        ReadF::new(reader)
    }
}

// u16(unsigned short) format
pub struct U16;
impl ProtocolFormatFragment for U16 {
    type Output = u16;

    #[inline]
    fn read_sync(self, mut reader: impl Read) -> std::io::Result<Self::Output> {
        let mut b = [0u8; 2];
        reader.read_exact(&mut b)?;
        Ok(u16::from_le_bytes(b))
    }
}
impl<'r, R> AsyncProtocolFormatFragment<'r, R> for U16
where
    R: AsyncRead + Send + Unpin + 'r,
{
    type ReaderF = ReadF<2, R, u16>;

    #[inline]
    fn read_format(self, reader: R) -> Self::ReaderF {
        ReadF::new(reader)
    }
}

/// u32(unsigned int) format
pub struct U32;
impl ProtocolFormatFragment for U32 {
    type Output = u32;

    #[inline]
    fn read_sync(self, mut reader: impl Read) -> std::io::Result<Self::Output> {
        let mut b = [0u8; 4];
        reader.read_exact(&mut b)?;
        Ok(u32::from_le_bytes(b))
    }
}
impl<'r, R> AsyncProtocolFormatFragment<'r, R> for U32
where
    R: AsyncRead + Send + Unpin + 'r,
{
    type ReaderF = ReadF<4, R, u32>;

    #[inline]
    fn read_format(self, reader: R) -> Self::ReaderF {
        ReadF::new(reader)
    }
}

/// Length-encoded integer format
pub struct LengthEncodedInteger;
impl ProtocolFormatFragment for LengthEncodedInteger {
    type Output = u64;

    #[inline]
    fn read_sync(self, reader: impl Read) -> std::io::Result<Self::Output> {
        super::LengthEncodedInteger::read_sync(reader).map(|x| x.0)
    }
}
impl<'r, R> AsyncProtocolFormatFragment<'r, R> for LengthEncodedInteger
where
    R: AsyncRead + Send + Unpin + 'r,
{
    type ReaderF = ReadLengthEncodedIntegerF<R>;

    #[inline]
    fn read_format(self, reader: R) -> Self::ReaderF {
        ReadLengthEncodedIntegerF::new(reader)
    }
}

/// Length-encoded integer format with prefetched first byte
pub struct LengthEncodedIntegerAhead(pub u8);
impl ProtocolFormatFragment for LengthEncodedIntegerAhead {
    type Output = u64;

    #[inline]
    fn read_sync(self, reader: impl Read) -> std::io::Result<Self::Output> {
        super::LengthEncodedInteger::read_ahead_sync(self.0, reader).map(|x| x.0)
    }
}
impl<'r, R> AsyncProtocolFormatFragment<'r, R> for LengthEncodedIntegerAhead
where
    R: AsyncRead + Send + Unpin + 'r,
{
    type ReaderF = ReadLengthEncodedIntegerF<R>;

    #[inline]
    fn read_format(self, reader: R) -> Self::ReaderF {
        ReadLengthEncodedIntegerF::new_ahead(self.0, reader)
    }
}

/// Compile-time determined fixed length byte string format
pub struct FixedBytes<const L: usize>;
impl<const L: usize> ProtocolFormatFragment for FixedBytes<L> {
    type Output = [u8; L];

    #[inline]
    fn read_sync(self, mut reader: impl Read) -> std::io::Result<Self::Output> {
        let mut b = [0u8; L];
        reader.read_exact(&mut b)?;
        Ok(b)
    }
}
impl<'r, const L: usize, R: 'r + AsyncRead + Send + Unpin> AsyncProtocolFormatFragment<'r, R> for FixedBytes<L> {
    type ReaderF = ReadFixedBytesF<L, R>;

    #[inline]
    fn read_format(self, reader: R) -> Self::ReaderF {
        ReadFixedBytesF::new(reader)
    }
}

/// Runtime determined fixed length byte string format
pub struct Bytes(pub usize);
impl ProtocolFormatFragment for Bytes {
    type Output = Vec<u8>;

    #[inline]
    fn read_sync(self, mut reader: impl Read) -> std::io::Result<Self::Output> {
        let mut b = Vec::with_capacity(self.0);
        unsafe {
            b.set_len(self.0);
        }
        reader.read_exact(&mut b)?;
        Ok(b)
    }
}
impl<'r, R: 'r + AsyncRead + Send + Unpin> AsyncProtocolFormatFragment<'r, R> for Bytes {
    type ReaderF = ReadBytesF<R>;

    #[inline]
    fn read_format(self, reader: R) -> Self::ReaderF {
        ReadBytesF::new(reader, self.0)
    }
}

/// Runtime determined fixed length byte string format with prefetched first byte
pub struct BytesAhead(pub u8, pub usize);
impl ProtocolFormatFragment for BytesAhead {
    type Output = Vec<u8>;

    #[inline]
    fn read_sync(self, mut reader: impl Read) -> std::io::Result<Self::Output> {
        let mut b = Vec::with_capacity(self.1);
        unsafe {
            b.set_len(self.1);
        }
        b[0] = self.0;
        reader.read_exact(&mut b[1..])?;
        Ok(b)
    }
}
impl<'r, R: 'r + AsyncRead + Send + Unpin> AsyncProtocolFormatFragment<'r, R> for BytesAhead {
    type ReaderF = ReadBytesF<R>;

    #[inline]
    fn read_format(self, reader: R) -> Self::ReaderF {
        ReadBytesF::new_ahead(reader, self.0, self.1)
    }
}

/// Null terminated string format
pub struct NullTerminatedString;
impl ProtocolFormatFragment for NullTerminatedString {
    type Output = String;

    #[inline]
    fn read_sync(self, mut reader: impl Read) -> std::io::Result<Self::Output> {
        let mut collected = Vec::new();
        let mut rb = [0u8; 1];

        loop {
            reader.read_exact(&mut rb)?;
            if rb[0] == 0 {
                return Ok(unsafe { String::from_utf8_unchecked(collected) });
            } else {
                collected.push(rb[0]);
            }
        }
    }
}
impl<'r, R: 'r + AsyncRead + Send + Unpin> AsyncProtocolFormatFragment<'r, R> for NullTerminatedString {
    type ReaderF = ReadNullTerminatedStringF<R>;

    #[inline]
    fn read_format(self, reader: R) -> Self::ReaderF {
        ReadNullTerminatedStringF::new(reader)
    }
}

/// Runtime determined fixed length string format
pub struct FixedLengthString(pub usize);
impl ProtocolFormatFragment for FixedLengthString {
    type Output = String;

    #[inline]
    fn read_sync(self, mut reader: impl Read) -> std::io::Result<Self::Output> {
        let mut b = Vec::with_capacity(self.0);
        unsafe {
            b.set_len(self.0);
        }
        reader.read_exact(&mut b)?;
        Ok(unsafe { String::from_utf8_unchecked(b) })
    }
}
impl<'r, R: 'r + AsyncRead + Send + Unpin> AsyncProtocolFormatFragment<'r, R> for FixedLengthString {
    type ReaderF = futures_util::future::MapOk<ReadBytesF<R>, fn(Vec<u8>) -> String>;

    #[inline]
    fn read_format(self, reader: R) -> Self::ReaderF {
        ReadBytesF::new(reader, self.0).map_ok(unsafe_recover_string_from_u8s)
    }
}
fn unsafe_recover_string_from_u8s(v: Vec<u8>) -> String {
    unsafe { String::from_utf8_unchecked(v) }
}

/// Variable string format preceded with Length-Encoded integer as its length
pub struct LengthEncodedString;
impl ProtocolFormatFragment for LengthEncodedString {
    type Output = String;

    #[inline]
    fn read_sync(self, mut reader: impl Read) -> std::io::Result<Self::Output> {
        let len = LengthEncodedInteger.read_sync(&mut reader)?;
        FixedLengthString(len as _).read_sync(reader)
    }
}
impl<'r, R: 'r + AsyncRead + Send + Unpin> AsyncProtocolFormatFragment<'r, R> for LengthEncodedString {
    // TODO: できればBoxつかいたくない
    type ReaderF = BoxFuture<'r, std::io::Result<String>>;

    #[inline]
    fn read_format(self, mut reader: R) -> Self::ReaderF {
        async move {
            let len = LengthEncodedInteger.read_format(&mut reader).await?;
            FixedLengthString(len as _).read_format(&mut reader).await
        }
        .boxed()
    }
}

/// Packet header format
pub struct PacketHeader;
impl ProtocolFormatFragment for PacketHeader {
    type Output = super::PacketHeader;

    #[inline]
    fn read_sync(self, mut reader: impl Read) -> std::io::Result<Self::Output> {
        let mut ph = [0u8; 4];
        reader.read_exact(&mut ph)?;

        Ok(super::PacketHeader::from_fixed_bytes(ph))
    }
}
impl<'r, R: 'r + AsyncRead + Send + Unpin> AsyncProtocolFormatFragment<'r, R> for PacketHeader {
    type ReaderF = futures_util::future::MapOk<ReadFixedBytesF<4, R>, fn([u8; 4]) -> super::PacketHeader>;

    #[inline]
    fn read_format(self, reader: R) -> Self::ReaderF {
        ReadFixedBytesF::new(reader).map_ok(super::PacketHeader::from_fixed_bytes)
    }
}

/// Mapped format; Applies conversion after reading the value
pub struct Mapped<PF, F>(pub PF, pub F);
impl<PF, F, R> ProtocolFormatFragment for Mapped<PF, F>
where
    PF: ProtocolFormatFragment,
    F: FnOnce(PF::Output) -> R,
{
    type Output = R;

    fn read_sync(self, reader: impl Read) -> std::io::Result<Self::Output> {
        self.0.read_sync(reader).map(self.1)
    }
}
impl<'r, R: 'r, PF, F, Ret: 'static> AsyncProtocolFormatFragment<'r, R> for Mapped<PF, F>
where
    PF: AsyncProtocolFormatFragment<'r, R>,
    PF::Output: 'static,
    F: FnOnce(<PF as ProtocolFormatFragment>::Output) -> Ret + Send + 'r,
    R: AsyncRead + Send,
{
    type ReaderF = futures_util::future::MapOk<PF::ReaderF, F>;

    fn read_format(self, reader: R) -> Self::ReaderF {
        self.0.read_format(reader).map_ok(self.1)
    }
}

/// AssertEq format; Assertion the value by [`assert_eq!`] after reading
pub struct AssertEq<PF: ProtocolFormatFragment>(PF, PF::Output);
impl<PF> ProtocolFormatFragment for AssertEq<PF>
where
    PF: ProtocolFormatFragment,
    PF::Output: std::fmt::Debug + Eq,
{
    type Output = PF::Output;

    fn read_sync(self, reader: impl Read) -> std::io::Result<Self::Output> {
        let v = self.0.read_sync(reader)?;
        assert_eq!(v, self.1);
        Ok(v)
    }
}
impl<'r, R: 'r, PF> AsyncProtocolFormatFragment<'r, R> for AssertEq<PF>
where
    PF: AsyncProtocolFormatFragment<'r, R> + Send + 'r,
    R: AsyncRead + Send + Unpin,
    PF::Output: std::fmt::Debug + Eq + Send + 'r,
{
    type ReaderF = BoxFuture<'r, std::io::Result<PF::Output>>;

    fn read_format(self, reader: R) -> Self::ReaderF {
        async move {
            let v = self.0.read_format(reader).await?;
            assert_eq!(v, self.1);
            Ok(v)
        }
        .boxed()
    }
}

macro_rules! ProtocolFormatFragmentGroup {
    ($($a: ident: $n: tt),+) => {
        impl<$($a),+> ProtocolFormatFragment for ($($a),+) where $($a: ProtocolFormatFragment),+ {
            type Output = ($($a::Output),+);

            #[inline]
            fn read_sync(self, mut reader: impl Read) -> std::io::Result<Self::Output> {
                #![allow(non_snake_case)]
                $(let $a = self.$n.read_sync(&mut reader)?;)+

                Ok(($($a),+))
            }
        }
        // 実装できないやつ
        /*impl<'r, Reader, $($a),+> AsyncProtocolFormatFragment<'r, Reader> for ($($a),+)
        where
            Reader: AsyncRead + Send + Unpin + ?Sized + 'r,
            $($a: AsyncProtocolFormatFragment<'r, Reader>,)+
            $($a::Output: Send + 'static,)+
            $($a: Send + 'static,)+
        {
            type ReaderF = BoxFuture<'r, std::io::Result<($($a::Output),+)>>;

            #[inline]
            fn read_format(self, reader: &'r mut Reader) -> Self::ReaderF {
                async move {
                    #![allow(non_snake_case)]
                    $crate::ReadAsync!(reader => { $($a <- self.$n),* });

                    Ok(($($a),+))
                }.boxed()
            }
        }*/
    }
}

ProtocolFormatFragmentGroup!(A: 0, B: 1);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3, E: 4);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5, G: 6);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5, G: 6, H: 7);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5, G: 6, H: 7, I: 8);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5, G: 6, H: 7, I: 8, J: 9);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5, G: 6, H: 7, I: 8, J: 9, K: 10);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5, G: 6, H: 7, I: 8, J: 9, K: 10, L: 11);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5, G: 6, H: 7, I: 8, J: 9, K: 10, L: 11, M: 12);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5, G: 6, H: 7, I: 8, J: 9, K: 10, L: 11, M: 12, N: 13);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5, G: 6, H: 7, I: 8, J: 9, K: 10, L: 11, M: 12, N: 13, O: 14);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5, G: 6, H: 7, I: 8, J: 9, K: 10, L: 11, M: 12, N: 13, O: 14, P: 15);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5, G: 6, H: 7, I: 8, J: 9, K: 10, L: 11, M: 12, N: 13, O: 14, P: 15, Q: 16);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5, G: 6, H: 7, I: 8, J: 9, K: 10, L: 11, M: 12, N: 13, O: 14, P: 15, Q: 16, R: 17);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5, G: 6, H: 7, I: 8, J: 9, K: 10, L: 11, M: 12, N: 13, O: 14, P: 15, Q: 16, R: 17, S: 18);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5, G: 6, H: 7, I: 8, J: 9, K: 10, L: 11, M: 12, N: 13, O: 14, P: 15, Q: 16, R: 17, S: 18, T: 19);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5, G: 6, H: 7, I: 8, J: 9, K: 10, L: 11, M: 12, N: 13, O: 14, P: 15, Q: 16, R: 17, S: 18, T: 19, U: 20);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5, G: 6, H: 7, I: 8, J: 9, K: 10, L: 11, M: 12, N: 13, O: 14, P: 15, Q: 16, R: 17, S: 18, T: 19, U: 20, V: 21);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5, G: 6, H: 7, I: 8, J: 9, K: 10, L: 11, M: 12, N: 13, O: 14, P: 15, Q: 16, R: 17, S: 18, T: 19, U: 20, V: 21, W: 22);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5, G: 6, H: 7, I: 8, J: 9, K: 10, L: 11, M: 12, N: 13, O: 14, P: 15, Q: 16, R: 17, S: 18, T: 19, U: 20, V: 21, W: 22, X: 23);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5, G: 6, H: 7, I: 8, J: 9, K: 10, L: 11, M: 12, N: 13, O: 14, P: 15, Q: 16, R: 17, S: 18, T: 19, U: 20, V: 21, W: 22, X: 23, Y: 24);
ProtocolFormatFragmentGroup!(A: 0, B: 1, C: 2, D: 3, E: 4, F: 5, G: 6, H: 7, I: 8, J: 9, K: 10, L: 11, M: 12, N: 13, O: 14, P: 15, Q: 16, R: 17, S: 18, T: 19, U: 20, V: 21, W: 22, X: 23, Y: 24, Z: 25);

/// Read multiple values synchronously
///
/// ## Example
///
/// ```
/// ReadSync!(reader => {
///     hdr <- PacketHeader,
///     value1 <- U8,
///     value2 <- U16
/// });
/// ```
#[macro_export]
macro_rules! ReadSync {
    ($reader: expr => { $($val: ident <- $fmt: expr),* }) => {
        $(
            let $val = $fmt.read_sync(&mut $reader)?;
        )*
    }
}

/// Read multiple values asynchronously
///
/// ## Example
///
/// ```
/// ReadAsync!(reader => {
///     hdr <- PacketHeader,
///     value1 <- U8,
///     value2 <- U16
/// });
/// ```
#[macro_export]
macro_rules! ReadAsync {
    ($reader: expr => { $($val: ident <- $fmt: expr),* }) => {
        $(
            let $val = $fmt.read_format(&mut $reader).await?;
        )*
    }
}

/// Defines Format Fragment structure
///
/// ## Example
///
/// ```
/// DefFormatStruct!(pub RawPacketAStruct(RawPacketAStructFormat) {
///     hdr(PacketHeader) <- format::PacketHeader,
///     v1(u8) <- format::U8,
///     v2([u8; 4]) <- format::FixedBytes::<4>
/// });
/// ```
#[macro_export]
macro_rules! DefFormatStruct {
    ($struct_name: ident($pf_name: ident) { $($val: ident($vty: ty) <- $fmt: expr),* }) => {
        struct $struct_name {
            $($val: $vty),*
        }

        $crate::DefProtocolFormat!($pf_name for $struct_name { $($val <- $fmt),* });
    };
    ($vis: vis $struct_name: ident($pf_name: ident) { $($val: ident($vty: ty) <- $fmt: expr),* }) => {
        $vis struct $struct_name {
            $($val: $vty),*
        }

        $crate::DefProtocolFormat!($vis $pf_name for $struct_name { $($val <- $fmt),* });
    }
}
/// Defines Format Fragment implementation for a structure
#[macro_export]
macro_rules! DefProtocolFormat {
    ($pf_name: ident for $struct_name: ident { $($val: ident <- $fmt: expr),* }) => {
        struct $pf_name;
        impl $crate::mysql::protos::format::ProtocolFormatFragment for $pf_name {
            type Output = $struct_name;

            #[inline]
            fn read_sync(self, mut reader: impl Read) -> std::io::Result<Self::Output> {
                $crate::ReadSync!(reader => { $($val <- $fmt),* });

                Ok($struct_name { $($val),* })
            }
        }
        impl<'r, R: 'r> $crate::mysql::protos::format::AsyncProtocolFormatFragment<'r, R> for $pf_name
        where
            R: tokio::io::AsyncRead + Send + Unpin
        {
            type ReaderF = futures_util::future::BoxFuture<'r, std::io::Result<$struct_name>>;

            #[inline]
            fn read_format(self, mut reader: R) -> Self::ReaderF {
                use futures_util::future::FutureExt;

                async move {
                    $crate::ReadAsync!(reader => { $($val <- $fmt),* });

                    Ok($struct_name { $($val),* })
                }.boxed()
            }
        }
    };
    ($vis: vis $pf_name: ident for $struct_name: ident { $($val: ident <- $fmt: expr),* }) => {
        $vis struct $pf_name;
        impl $crate::mysql::protos::format::ProtocolFormatFragment for $pf_name {
            type Output = $struct_name;

            #[inline]
            fn read_sync(self, mut reader: impl Read) -> std::io::Result<Self::Output> {
                $crate::ReadSync!(reader => { $($val <- $fmt),* });

                Ok($struct_name { $($val),* })
            }
        }
        impl<'r, R: 'r> $crate::mysql::protos::format::AsyncProtocolFormatFragment<'r, R> for $pf_name
        where
            R: tokio::io::AsyncRead + Send + Unpin
        {
            type ReaderF = futures_util::future::BoxFuture<'r, std::io::Result<$struct_name>>;

            #[inline]
            fn read_format(self, mut reader: R) -> Self::ReaderF {
                use futures_util::future::FutureExt;

                async move {
                    $crate::ReadAsync!(reader => { $($val <- $fmt),* });

                    Ok($struct_name { $($val),* })
                }.boxed()
            }
        }
    }
}