memberlist-proto 0.1.2

Proto types and traits for the memberlist crate.
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
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
use std::borrow::Cow;

use super::{Data, DataRef, DecodeError, EncodeError, WireType};

#[cfg(feature = "brotli")]
macro_rules! num_to_enum {
  (
    $(#[$meta:meta])*
    $name:ident($inner:ident in [$min:expr, $max:literal]):$exp:literal:$short:literal {
      $(
        $(#[$value_meta:meta])*
        $value:literal
      ), +$(,)?
    }
  ) => {
    paste::paste! {
      $(#[$meta])*
      #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, derive_more::Display, derive_more::IsVariant)]
      #[cfg_attr(any(feature = "arbitrary", test), derive(arbitrary::Arbitrary))]
      #[non_exhaustive]
      #[repr($inner)]
      pub enum $name {
        $(
          #[doc = $exp " " $value "."]
          $(#[$value_meta])*
          [< $short:camel $value >] = $value,
        )*
      }

      impl $name {
        /// Returns the maximum value.
        #[inline]
        pub const fn max() -> Self {
          Self::[< $short:camel $max >]
        }

        /// Returns the minimum value.
        #[inline]
        pub const fn min() -> Self {
          Self::[< $short:camel $min >]
        }
      }

      impl $name {
        #[allow(unused_comparisons)]
        pub(super) const fn [< from_ $inner>](value: $inner) -> Self {
          match value {
            $(
              $value => Self::[< $short:camel $value >],
            )*
            val if val > $max => Self::[< $short:camel $max >],
            val if val < $min => Self::[< $short:camel $min >],
            _ => Self::[< $short:camel $max >],
          }
        }
      }

      impl From<$inner> for $name {
        fn from(value: $inner) -> Self {
          Self::from_u8(value as $inner)
        }
      }

      impl From<$name> for $inner {
        fn from(value: $name) -> Self {
          value as $inner
        }
      }

      #[cfg(feature = "serde")]
      const _: () = {
        use serde::{Deserialize, Serialize};

        impl Serialize for $name {
          fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
          where
            S: serde::Serializer,
          {
            serializer.[< serialize_ $inner>](*self as $inner)
          }
        }

        impl<'de> Deserialize<'de> for $name {
          fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
          where
            D: serde::Deserializer<'de> {
            <$inner>::deserialize(deserializer).map(Self::[< from_ $inner >])
          }
        }
      };
    }
  };
}

#[allow(unused)]
const ZSTD_TAG: u8 = 1;
#[allow(unused)]
const BROTLI_TAG: u8 = 2;
#[allow(unused)]
const LZ4_TAG: u8 = 3;
#[allow(unused)]
const SNAPPY_TAG: u8 = 4;

#[cfg(feature = "brotli")]
mod brotli_impl;
mod error;
mod from_str;
#[cfg(feature = "zstd")]
mod zstd_impl;

#[cfg(feature = "brotli")]
pub use brotli_impl::*;
pub use error::*;
pub use from_str::*;

#[cfg(feature = "zstd")]
pub use zstd_impl::*;

#[cfg(any(feature = "quickcheck", test))]
mod quickcheck_impl;

#[cfg(feature = "brotli")]
const BROTLI_BUFFER_SIZE: usize = 4096;

#[cfg(feature = "brotli")]
const _: () = {
  impl CompressionError {
    #[inline]
    const fn brotli_compress_error(err: std::io::Error) -> Self {
      Self::Compress(CompressError::Brotli(err))
    }

    #[inline]
    const fn brotli_decompress_error(err: std::io::Error) -> Self {
      Self::Decompress(DecompressError::Brotli(err))
    }
  }
};

#[cfg(feature = "snappy")]
const _: () = {
  impl CompressionError {
    #[inline]
    const fn snappy_compress_error(err: snap::Error) -> Self {
      Self::Compress(CompressError::Snappy(err))
    }

    #[inline]
    const fn snappy_decompress_error(err: snap::Error) -> Self {
      Self::Decompress(DecompressError::Snappy(err))
    }
  }
};

#[cfg(feature = "lz4")]
const _: () = {
  impl CompressionError {
    #[inline]
    const fn lz4_decompress_error(err: lz4_flex::block::DecompressError) -> Self {
      Self::Decompress(DecompressError::Lz4(err))
    }

    #[inline]
    const fn lz4_compress_error(err: lz4_flex::block::CompressError) -> Self {
      Self::Compress(CompressError::Lz4(err))
    }
  }
};

#[cfg(feature = "zstd")]
const _: () = {
  impl CompressionError {
    #[inline]
    const fn zstd_compress_error(err: std::io::Error) -> Self {
      Self::Compress(CompressError::Zstd(err))
    }

    #[inline]
    const fn zstd_decompress_error(err: std::io::Error) -> Self {
      Self::Decompress(DecompressError::Zstd(err))
    }
  }
};

/// The compressioned algorithm used to compression the message.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, derive_more::IsVariant, derive_more::Display)]
#[non_exhaustive]
pub enum CompressAlgorithm {
  /// Brotli
  #[display("brotli{_0}")]
  #[cfg(feature = "brotli")]
  #[cfg_attr(docsrs, doc(cfg(feature = "brotli")))]
  Brotli(BrotliAlgorithm),
  /// LZ4
  #[display("lz4")]
  #[cfg(feature = "lz4")]
  #[cfg_attr(docsrs, doc(cfg(feature = "lz4")))]
  Lz4,
  /// Snappy
  #[display("snappy")]
  #[cfg(feature = "snappy")]
  #[cfg_attr(docsrs, doc(cfg(feature = "snappy")))]
  Snappy,
  /// Zstd
  #[display("zstd({_0})")]
  #[cfg(feature = "zstd")]
  #[cfg_attr(docsrs, doc(cfg(feature = "zstd")))]
  Zstd(ZstdCompressionLevel),
  /// Unknwon compressioned algorithm
  #[display("unknown({_0})")]
  Unknown(u8),
}

#[cfg(any(feature = "quickcheck", test))]
const _: () = {
  use quickcheck::Arbitrary;

  impl CompressAlgorithm {
    const MAX: u8 = SNAPPY_TAG;
    const MIN: u8 = ZSTD_TAG;
  }

  impl Arbitrary for CompressAlgorithm {
    fn arbitrary(g: &mut quickcheck::Gen) -> Self {
      let val = (u8::arbitrary(g) % Self::MAX) + Self::MIN;
      match val {
        #[cfg(feature = "zstd")]
        ZSTD_TAG => Self::Zstd(ZstdCompressionLevel::arbitrary(g)),
        #[cfg(feature = "brotli")]
        BROTLI_TAG => Self::Brotli(BrotliAlgorithm::arbitrary(g)),
        #[cfg(feature = "lz4")]
        LZ4_TAG => Self::Lz4,
        #[cfg(feature = "snappy")]
        SNAPPY_TAG => Self::Snappy,
        _ => unreachable!(),
      }
    }
  }
};

impl Default for CompressAlgorithm {
  fn default() -> Self {
    cfg_if::cfg_if! {
      if #[cfg(feature = "snappy")] {
        Self::Snappy
      } else if #[cfg(feature = "lz4")] {
        Self::Lz4
      } else if #[cfg(feature = "brotli")] {
        Self::Brotli(BrotliAlgorithm::default())
      } else if #[cfg(feature = "zstd")] {
        Self::Zstd(ZstdCompressionLevel::default())
      } else {
        Self::Unknown(u8::MAX)
      }
    }
  }
}

impl CompressAlgorithm {
  /// Decompresses the given buffer.
  pub fn decompress_to(&self, src: &[u8], dst: &mut [u8]) -> Result<usize, CompressionError> {
    match self {
      #[cfg(feature = "brotli")]
      Self::Brotli(_) => {
        let mut reader = brotli::Decompressor::new(src, BROTLI_BUFFER_SIZE);
        std::io::copy(&mut reader, &mut std::io::Cursor::new(dst))
          .map(|bytes| bytes as usize)
          .map_err(CompressionError::brotli_decompress_error)
      }
      #[cfg(feature = "lz4")]
      Self::Lz4 => {
        lz4_flex::decompress_into(src, dst).map_err(CompressionError::lz4_decompress_error)
      }
      #[cfg(feature = "snappy")]
      Self::Snappy => snap::raw::Decoder::new()
        .decompress(src, dst)
        .map_err(CompressionError::snappy_decompress_error),
      #[cfg(feature = "zstd")]
      Self::Zstd(_) => {
        let mut decoder = zstd::Decoder::new(std::io::Cursor::new(src))
          .map_err(CompressionError::zstd_decompress_error)?;
        std::io::copy(&mut decoder, &mut std::io::Cursor::new(dst))
          .map(|bytes| bytes as usize)
          .map_err(CompressionError::zstd_decompress_error)
      }
      algo => Err(CompressionError::UnknownAlgorithm(*algo)),
    }
  }

  /// Returns the maximum compressed length of the given buffer.
  ///
  /// This is useful when you want to pre-allocate the buffer before compressing.
  pub fn max_compress_len(&self, input_size: usize) -> Result<usize, CompressionError> {
    Ok(match self {
      #[cfg(feature = "brotli")]
      Self::Brotli(_) => {
        // TODO(al8n): The brotli::enc::BrotliEncoderMaxCompressedSize is not working as expected.
        // In fuzzy tests, it is returning a value less than the actual compressed size.
        let num_large_blocks = (input_size >> 4) + 12;
        let overhead = 2 + (4 * num_large_blocks) + 3 + 1;
        let result = input_size + overhead;
        if input_size == 0 {
          2
        } else if result < input_size {
          input_size
        } else {
          result
        }
      }
      #[cfg(feature = "lz4")]
      Self::Lz4 => lz4_flex::block::get_maximum_output_size(input_size),
      #[cfg(feature = "snappy")]
      Self::Snappy => snap::raw::max_compress_len(input_size),
      #[cfg(feature = "zstd")]
      Self::Zstd(_) => zstd::zstd_safe::compress_bound(input_size),
      Self::Unknown(_) => return Err(CompressionError::UnknownAlgorithm(*self)),
    })
  }

  /// Compresses the given buffer.
  ///
  /// The `dst` buffer should be pre-allocated with the [`max_compress_len`](Self::max_compress_len) method.
  pub fn compress_to(&self, src: &[u8], dst: &mut [u8]) -> Result<usize, CompressionError> {
    match self {
      #[cfg(feature = "brotli")]
      CompressAlgorithm::Brotli(_algo) => {
        use std::io::Write;

        let mut compressor = brotli::CompressorWriter::new(
          std::io::Cursor::new(dst),
          BROTLI_BUFFER_SIZE,
          _algo.quality() as u8 as u32,
          _algo.window() as u8 as u32,
        );

        compressor
          .write_all(src)
          .map(|_| compressor.into_inner().position() as usize)
          .map_err(CompressionError::brotli_compress_error)
      }
      #[cfg(feature = "lz4")]
      CompressAlgorithm::Lz4 => {
        lz4_flex::compress_into(src, dst).map_err(CompressionError::lz4_compress_error)
      }
      #[cfg(feature = "snappy")]
      CompressAlgorithm::Snappy => {
        let mut encoder = snap::raw::Encoder::new();
        encoder
          .compress(src, dst)
          .map_err(CompressionError::snappy_compress_error)
      }
      #[cfg(feature = "zstd")]
      CompressAlgorithm::Zstd(_level) => {
        let mut cursor = std::io::Cursor::new(dst);
        zstd::stream::copy_encode(src, &mut cursor, _level.level() as i32)
          .map(|_| cursor.position() as usize)
          .map_err(CompressionError::zstd_compress_error)
      }
      algo => Err(CompressionError::UnknownAlgorithm(*algo)),
    }
  }

  /// Returns the string representation of the algorithm.
  #[inline]
  pub fn as_str(&self) -> Cow<'_, str> {
    match self {
      #[cfg(feature = "brotli")]
      Self::Brotli(algo) => return Cow::Owned(format!("brotli{algo}")),
      #[cfg(feature = "lz4")]
      Self::Lz4 => "lz4",
      #[cfg(feature = "snappy")]
      Self::Snappy => "snappy",
      #[cfg(feature = "zstd")]
      Self::Zstd(val) => return Cow::Owned(format!("zstd({})", val.level())),
      Self::Unknown(val) => return Cow::Owned(format!("unknown({val})")),
    }
    .into()
  }

  /// Encodes the algorithm into a u16.
  /// First byte is the algorithm tag.
  /// Second byte stores additional configuration if any.
  #[inline]
  pub(super) const fn encode_to_u16(&self) -> u16 {
    let (tag, extra) = match self {
      #[cfg(feature = "brotli")]
      Self::Brotli(algo) => (BROTLI_TAG, algo.encode()),
      #[cfg(feature = "lz4")]
      Self::Lz4 => (LZ4_TAG, 0),
      #[cfg(feature = "snappy")]
      Self::Snappy => (SNAPPY_TAG, 0),
      #[cfg(feature = "zstd")]
      Self::Zstd(algo) => (ZSTD_TAG, algo.level() as u8),
      Self::Unknown(v) => (*v, 0),
    };
    ((tag as u16) << 8) | (extra as u16)
  }

  /// Creates a CompressAlgorithm from a u16.
  /// First byte determines the algorithm type.
  /// Second byte contains additional configuration if any.
  #[inline]
  pub(super) const fn decode_from_u16(value: u16) -> Self {
    let tag = (value >> 8) as u8;
    #[cfg(any(feature = "brotli", feature = "zstd"))]
    let extra = value as u8;

    match tag {
      #[cfg(feature = "brotli")]
      BROTLI_TAG => Self::Brotli(BrotliAlgorithm::decode(extra)),
      #[cfg(feature = "lz4")]
      LZ4_TAG => Self::Lz4,
      #[cfg(feature = "snappy")]
      SNAPPY_TAG => Self::Snappy,
      #[cfg(feature = "zstd")]
      ZSTD_TAG => Self::Zstd(ZstdCompressionLevel::with_level(extra as i8)),
      v => Self::Unknown(v),
    }
  }

  pub(crate) const fn unknown_or_disabled(&self) -> Option<CompressionError> {
    match *self {
      #[cfg(feature = "brotli")]
      Self::Brotli(_) => None,
      #[cfg(feature = "lz4")]
      Self::Lz4 => None,
      #[cfg(feature = "snappy")]
      Self::Snappy => None,
      #[cfg(feature = "zstd")]
      Self::Zstd(_) => None,
      Self::Unknown(value) => Some({
        #[cfg(not(all(
          feature = "brotli",
          feature = "lz4",
          feature = "snappy",
          feature = "zstd"
        )))]
        {
          match value {
            #[cfg(feature = "brotli")]
            BROTLI_TAG => CompressionError::disabled(*self, "brotli"),
            #[cfg(feature = "lz4")]
            LZ4_TAG => CompressionError::disabled(*self, "lz4"),
            #[cfg(feature = "snappy")]
            SNAPPY_TAG => CompressionError::disabled(*self, "snappy"),
            #[cfg(feature = "zstd")]
            ZSTD_TAG => CompressionError::disabled(*self, "zstd"),
            _ => CompressionError::UnknownAlgorithm(*self),
          }
        }

        #[cfg(all(
          feature = "brotli",
          feature = "lz4",
          feature = "snappy",
          feature = "zstd"
        ))]
        CompressionError::UnknownAlgorithm(Self::Unknown(value))
      }),
    }
  }
}

impl From<u16> for CompressAlgorithm {
  fn from(value: u16) -> Self {
    Self::decode_from_u16(value)
  }
}

impl<'a> DataRef<'a, Self> for CompressAlgorithm {
  fn decode(buf: &'a [u8]) -> Result<(usize, Self), DecodeError>
  where
    Self: Sized,
  {
    <u16 as DataRef<u16>>::decode(buf).map(|(bytes_read, value)| (bytes_read, Self::from(value)))
  }
}

impl Data for CompressAlgorithm {
  const WIRE_TYPE: WireType = WireType::Varint;

  type Ref<'a> = Self;

  fn from_ref(val: Self::Ref<'_>) -> Result<Self, DecodeError>
  where
    Self: Sized,
  {
    Ok(val)
  }

  fn encoded_len(&self) -> usize {
    self.encode_to_u16().encoded_len()
  }

  fn encode(&self, buf: &mut [u8]) -> Result<usize, EncodeError> {
    self.encode_to_u16().encode(buf)
  }
}

#[inline]
#[allow(unused)]
fn parse_or_default<I, O>(s: &str) -> Result<O, I::Err>
where
  I: core::str::FromStr,
  O: Default + From<I>,
{
  match s {
    "" | "-" | "_" => Ok(O::default()),
    val => val.parse::<I>().map(Into::into),
  }
}

#[cfg(feature = "serde")]
const _: () = {
  use serde::{Deserialize, Deserializer, Serialize, Serializer};

  #[derive(serde::Serialize, serde::Deserialize)]
  #[serde(rename_all = "snake_case")]
  enum CompressAlgorithmHelper {
    /// Brotli
    #[cfg(feature = "brotli")]
    Brotli(BrotliAlgorithm),
    /// LZ4
    #[cfg(feature = "lz4")]
    Lz4,
    /// Snappy
    #[cfg(feature = "snappy")]
    Snappy,
    /// Zstd
    #[cfg(feature = "zstd")]
    Zstd(ZstdCompressionLevel),
    /// Unknwon compressioned algorithm
    Unknown(u8),
  }

  impl From<CompressAlgorithm> for CompressAlgorithmHelper {
    fn from(algo: CompressAlgorithm) -> Self {
      match algo {
        #[cfg(feature = "brotli")]
        CompressAlgorithm::Brotli(algo) => Self::Brotli(algo),
        #[cfg(feature = "lz4")]
        CompressAlgorithm::Lz4 => Self::Lz4,
        #[cfg(feature = "snappy")]
        CompressAlgorithm::Snappy => Self::Snappy,
        #[cfg(feature = "zstd")]
        CompressAlgorithm::Zstd(algo) => Self::Zstd(algo),
        CompressAlgorithm::Unknown(v) => Self::Unknown(v),
      }
    }
  }

  impl From<CompressAlgorithmHelper> for CompressAlgorithm {
    fn from(helper: CompressAlgorithmHelper) -> Self {
      match helper {
        #[cfg(feature = "brotli")]
        CompressAlgorithmHelper::Brotli(algo) => Self::Brotli(algo),
        #[cfg(feature = "lz4")]
        CompressAlgorithmHelper::Lz4 => Self::Lz4,
        #[cfg(feature = "snappy")]
        CompressAlgorithmHelper::Snappy => Self::Snappy,
        #[cfg(feature = "zstd")]
        CompressAlgorithmHelper::Zstd(algo) => Self::Zstd(algo),
        CompressAlgorithmHelper::Unknown(v) => Self::Unknown(v),
      }
    }
  }

  impl Serialize for CompressAlgorithm {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
      S: Serializer,
    {
      if serializer.is_human_readable() {
        CompressAlgorithmHelper::from(*self).serialize(serializer)
      } else {
        serializer.serialize_u16(self.encode_to_u16())
      }
    }
  }

  impl<'de> Deserialize<'de> for CompressAlgorithm {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
      D: Deserializer<'de>,
    {
      if deserializer.is_human_readable() {
        CompressAlgorithmHelper::deserialize(deserializer).map(Into::into)
      } else {
        u16::deserialize(deserializer).map(Self::decode_from_u16)
      }
    }
  }
};

#[cfg(test)]
mod tests {
  use super::CompressAlgorithm;

  #[quickcheck_macros::quickcheck]
  #[cfg(feature = "serde")]
  fn compress_algorithm_serde(algo: CompressAlgorithm) -> bool {
    use bincode::config::standard;

    let Ok(serialized) = serde_json::to_string(&algo) else {
      return false;
    };
    let Ok(deserialized) = serde_json::from_str(&serialized) else {
      return false;
    };
    if algo != deserialized {
      return false;
    }

    let Ok(serialized) = bincode::serde::encode_to_vec(algo, standard()) else {
      return false;
    };

    let Ok((deserialized, _)) = bincode::serde::decode_from_slice(&serialized, standard()) else {
      return false;
    };

    algo == deserialized
  }

  #[quickcheck_macros::quickcheck]
  #[cfg(feature = "lz4")]
  fn lz4(data: Vec<u8>) -> bool {
    let algo = CompressAlgorithm::Lz4;

    let uncompressed_data_len = data.len();
    let max_compress_len = algo.max_compress_len(uncompressed_data_len).unwrap();
    let mut buffer = vec![0; max_compress_len];
    let written = algo.compress_to(&data, &mut buffer).unwrap();
    assert!(written <= max_compress_len);
    let mut orig = vec![0; uncompressed_data_len];
    let decompressed = algo.decompress_to(&buffer[..written], &mut orig).unwrap();
    data == orig && uncompressed_data_len == decompressed
  }

  #[quickcheck_macros::quickcheck]
  #[cfg(feature = "brotli")]
  fn brotli(data: Vec<u8>) -> bool {
    let algo = CompressAlgorithm::Brotli(Default::default());
    let uncompressed_data_len = data.len();
    let max_compress_len = algo.max_compress_len(uncompressed_data_len).unwrap();
    let mut buffer = vec![0; max_compress_len];
    let written = algo.compress_to(&data, &mut buffer).unwrap();
    assert!(written <= max_compress_len);
    let mut orig = vec![0; uncompressed_data_len];
    let decompressed = algo.decompress_to(&buffer[..written], &mut orig).unwrap();
    data == orig && uncompressed_data_len == decompressed
  }

  #[quickcheck_macros::quickcheck]
  #[cfg(feature = "zstd")]
  fn zstd(data: Vec<u8>) -> bool {
    let algo = CompressAlgorithm::Zstd(Default::default());
    let uncompressed_data_len = data.len();
    let max_compress_len = algo.max_compress_len(uncompressed_data_len).unwrap();
    let mut buffer = vec![0; max_compress_len];
    let written = algo.compress_to(&data, &mut buffer).unwrap();
    assert!(written <= max_compress_len);
    let mut orig = vec![0; uncompressed_data_len];
    let decompressed = algo.decompress_to(&buffer[..written], &mut orig).unwrap();
    data == orig && uncompressed_data_len == decompressed
  }

  #[quickcheck_macros::quickcheck]
  #[cfg(feature = "snappy")]
  fn snappy(data: Vec<u8>) -> bool {
    let algo = CompressAlgorithm::Snappy;
    let uncompressed_data_len = data.len();
    let max_compress_len = algo.max_compress_len(uncompressed_data_len).unwrap();
    let mut buffer = vec![0; max_compress_len];
    let written = algo.compress_to(&data, &mut buffer).unwrap();
    assert!(written <= max_compress_len);
    let mut orig = vec![0; uncompressed_data_len];
    let decompressed = algo.decompress_to(&buffer[..written], &mut orig).unwrap();
    data == orig && uncompressed_data_len == decompressed
  }

  #[cfg(feature = "snappy")]
  #[test]
  fn max_snappy_output_size() {
    let algo = CompressAlgorithm::Snappy;
    let max_compress_len = algo.max_compress_len(4096).unwrap();
    println!("max_compress_len: {}", max_compress_len);
    assert!(max_compress_len >= 4096);
  }

  #[cfg(feature = "lz4")]
  #[test]
  fn max_lz4_output_size() {
    let algo = CompressAlgorithm::Lz4;
    let max_compress_len = algo.max_compress_len(4096).unwrap();
    println!("max_compress_len: {}", max_compress_len);
    assert!(max_compress_len >= 4096);
  }

  #[cfg(feature = "brotli")]
  #[test]
  fn max_brotli_output_size() {
    let algo = CompressAlgorithm::Brotli(Default::default());
    let max_compress_len = algo.max_compress_len(4096).unwrap();
    println!("max_compress_len: {}", max_compress_len);
    assert!(max_compress_len >= 4096);
  }

  #[cfg(feature = "zstd")]
  #[test]
  fn max_zstd_output_size() {
    let algo = CompressAlgorithm::Zstd(Default::default());
    let max_compress_len = algo.max_compress_len(4096).unwrap();
    println!("max_compress_len: {}", max_compress_len);
    assert!(max_compress_len >= 4096);
  }
}