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
//! Generic data structure decompression framework.

use std::io::{Read, Result, Write};

pub trait Decompressor {
    fn new() -> Self;

    fn copy<R: Read, W: Write>(&self, source: R, dest: W) -> Result<u64>;

    fn from_reader<R: Read, V: Decompress>(&self, reader: R) -> Result<V>
    where
        Self: Sized;

    fn from_vec<V: Decompress>(&self, bytes: Vec<u8>) -> Result<V>
    where
        Self: Sized,
    {
        let reader = std::io::Cursor::new(bytes);
        self.from_reader(reader)
    }
}

pub trait Decompress {
    fn from_reader<R: Read>(reader: R) -> Result<Self>
    where
        Self: Sized;

    fn from_vec(&self, bytes: Vec<u8>) -> Result<Self>
    where
        Self: Sized,
    {
        let reader = std::io::Cursor::new(bytes);
        Self::from_reader(reader)
    }
}

// #[cfg(feature = "std")]
// impl Decompress for String {
//     fn from_reader<R: Read>(mut reader: R) -> Result<Self>
//     where
//         Self: Sized,
//     {
//         let mut string = String::new();
//         reader.read_to_string(&mut string)?;
//         Ok(string)
//     }
// }