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
use std::{
    collections::LinkedList,
    io,
    io::{ Read, BufReader }
};

/// Adds extensions onto the std collection: [`LinkedList<T>`].
/// 
/// [`LinkedList<T>`]: https://doc.rust-lang.org/std/collections/struct.LinkedList.html
pub trait LinkedListExt {
    fn to_vec(&self) -> Vec<u8>;
}

/// Adds easy byte reading onto a `Read` instance.
pub trait ReadExt: Read {
    fn read_u8(&mut self) -> io::Result<u8>;
    fn read_i8(&mut self) -> io::Result<i8>;
    fn read_u16(&mut self) -> io::Result<u16>;
    fn read_u24(&mut self) -> io::Result<u32>;
    fn read_i32(&mut self) -> io::Result<i32>;
    fn read_string(&mut self) -> io::Result<String>;
}

impl LinkedListExt for LinkedList<&[u8]> {
    /// Iterates through the `LinkedList<&[u8]>` and copies the bytes into one owned buffer.
    /// 
    /// This function allocates a new buffer that is exactly large enough to hold all the slices 
    /// combined that the `LinkedList` references.
    /// [`copy_from_slice()`] is used for high performance and to keep the amount of copies to a 
    /// minimum.
    /// 
    /// # Examples
    /// 
    /// Detailed example:
    /// 
    /// ```
    /// # use rscache::{ Cache, CacheError };
    /// use std::collections::LinkedList;
    /// use rscache::LinkedListExt;
    /// 
    /// # fn main() -> Result<(), CacheError> {
    /// # let path = "./data/cache";
    /// # let cache = Cache::new(path)?;
    /// # let index_id = 2; // Config index
    /// # let archive_id = 10; // Random archive.
    /// let buffer: LinkedList<&[u8]> = cache.read(index_id, archive_id)?;
    /// let buffer: Vec<u8> = buffer.to_vec();
    /// # Ok(())
    /// # }
    /// ```
    /// 
    /// Which can also be written as:
    /// 
    /// ```
    /// # use rscache::{ Cache, CacheError };
    /// # use std::collections::LinkedList;
    /// # use rscache::LinkedListExt;
    /// # fn main() -> Result<(), CacheError> {
    /// # let path = "./data/cache";
    /// # let cache = Cache::new(path)?;
    /// # let index_id = 2; // Config index
    /// # let archive_id = 10; // Random archive.
    /// let buffer = cache.read(index_id, archive_id)?.to_vec();
    /// # Ok(())
    /// # }
    /// ```
    /// 
    /// [`copy_from_slice()`]: https://doc.rust-lang.org/std/primitive.slice.html#method.copy_from_slice
    #[inline]
	fn to_vec(&self) -> Vec<u8> {
		let size = self.iter()
            .map(|data_block| data_block.len())
            .sum();

        let mut buffer = vec![0; size];
        let mut current = 0;

        for data_block in self {
            buffer[current..current + data_block.len()].copy_from_slice(data_block);
            current += data_block.len();
        }

        buffer
	}
}

impl ReadExt for BufReader<&[u8]> {
    #[inline]
    fn read_u8(&mut self) -> io::Result<u8> {
        let mut buffer = [0; 1];
        self.read_exact(&mut buffer)?;

        Ok(u8::from_be_bytes(buffer))
    }
    
    #[inline]
    fn read_i8(&mut self) -> io::Result<i8> {
        let mut buffer = [0; 1];
        self.read_exact(&mut buffer)?;

        Ok(i8::from_be_bytes(buffer))
    }
    
    #[inline]
    fn read_u16(&mut self) -> io::Result<u16> {
        let mut buffer = [0; 2];
        self.read_exact(&mut buffer)?;

        Ok(u16::from_be_bytes(buffer))
    }
    
    #[inline]
    fn read_u24(&mut self) -> io::Result<u32> {
        let mut buffer = [0; 3];
        self.read_exact(&mut buffer)?;

        Ok(((buffer[0] as u32) << 16) | ((buffer[1] as u32) << 8) | (buffer[2] as u32))
    }
    
    #[inline]
    fn read_i32(&mut self) -> io::Result<i32> {
        let mut buffer = [0; 4];
        self.read_exact(&mut buffer)?;

        Ok(i32::from_be_bytes(buffer))
    }
    
    #[inline]
    fn read_string(&mut self) -> io::Result<String> {
        let mut bytes = Vec::new();
    
        loop {
            let mut buffer = [0; 1];
            self.read_exact(&mut buffer)?;
            let byte = u8::from_be_bytes(buffer);
            if byte != 0 {
                bytes.push(byte);
            } else {
                break;
            }
        }
    
        Ok(String::from_utf8_lossy(&bytes[..]).to_string())
    }
}