diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-03-01 11:12:18 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-03-01 11:49:05 +0100 |
commit | d70b710f431cbfaf33cc6a3701b4282209b55780 (patch) | |
tree | bf7c82d6f170eb739940df141a6016d29df096ff | |
parent | ab7d4ce33bdf44855c7295e870ced2fac17f62e4 (diff) |
buffered-reader: Drop BufferedReader prefix.
- For example, `buffered_reader::BufferedReaderMemory` is now called
`buffered_reader::Memory`. This makes the type names less unwieldy.
- Fixes #206.
25 files changed, 305 insertions, 329 deletions
diff --git a/buffered-reader/src/decompress_bzip2.rs b/buffered-reader/src/decompress_bzip2.rs index 4224f686..cd27864e 100644 --- a/buffered-reader/src/decompress_bzip2.rs +++ b/buffered-reader/src/decompress_bzip2.rs @@ -8,11 +8,11 @@ use super::*; /// Decompresses the underlying `BufferedReader` using the bzip2 /// algorithm. -pub struct BufferedReaderBzip<R: BufferedReader<C>, C> { - reader: BufferedReaderGeneric<BzDecoder<R>, C>, +pub struct Bzip<R: BufferedReader<C>, C> { + reader: Generic<BzDecoder<R>, C>, } -impl <R: BufferedReader<()>> BufferedReaderBzip<R, ()> { +impl <R: BufferedReader<()>> Bzip<R, ()> { /// Instantiates a new bzip decompression reader. /// /// `reader` is the source to wrap. @@ -21,40 +21,40 @@ impl <R: BufferedReader<()>> BufferedReaderBzip<R, ()> { } } -impl <R: BufferedReader<C>, C> BufferedReaderBzip<R, C> { +impl <R: BufferedReader<C>, C> Bzip<R, C> { /// Like `new()`, but uses a cookie. /// /// The cookie can be retrieved using the `cookie_ref` and /// `cookie_mut` methods, and set using the `cookie_set` method. pub fn with_cookie(reader: R, cookie: C) -> Self { - BufferedReaderBzip { - reader: BufferedReaderGeneric::with_cookie( + Bzip { + reader: Generic::with_cookie( BzDecoder::new(reader), None, cookie), } } } -impl<R: BufferedReader<C>, C> io::Read for BufferedReaderBzip<R, C> { +impl<R: BufferedReader<C>, C> io::Read for Bzip<R, C> { fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> { self.reader.read(buf) } } -impl<R: BufferedReader<C>, C> fmt::Debug for BufferedReaderBzip<R, C> { +impl<R: BufferedReader<C>, C> fmt::Debug for Bzip<R, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("BufferedReaderBzip") + f.debug_struct("Bzip") .field("reader", &self.get_ref().unwrap()) .finish() } } -impl<R: BufferedReader<C>, C> fmt::Display for BufferedReaderBzip<R, C> { +impl<R: BufferedReader<C>, C> fmt::Display for Bzip<R, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "BufferedReaderBzip") + write!(f, "Bzip") } } -impl<R: BufferedReader<C>, C> BufferedReader<C> for BufferedReaderBzip<R, C> { +impl<R: BufferedReader<C>, C> BufferedReader<C> for Bzip<R, C> { fn buffer(&self) -> &[u8] { return self.reader.buffer(); } diff --git a/buffered-reader/src/decompress_deflate.rs b/buffered-reader/src/decompress_deflate.rs index 42633419..13a9f201 100644 --- a/buffered-reader/src/decompress_deflate.rs +++ b/buffered-reader/src/decompress_deflate.rs @@ -8,11 +8,11 @@ use super::*; /// Decompresses the underlying `BufferedReader` using the deflate /// algorithm. -pub struct BufferedReaderDeflate<R: BufferedReader<C>, C> { - reader: BufferedReaderGeneric<DeflateDecoder<R>, C>, +pub struct Deflate<R: BufferedReader<C>, C> { + reader: Generic<DeflateDecoder<R>, C>, } -impl <R: BufferedReader<()>> BufferedReaderDeflate<R, ()> { +impl <R: BufferedReader<()>> Deflate<R, ()> { /// Instantiates a new deflate decompression reader. /// /// `reader` is the source to wrap. @@ -21,41 +21,41 @@ impl <R: BufferedReader<()>> BufferedReaderDeflate<R, ()> { } } -impl <R: BufferedReader<C>, C> BufferedReaderDeflate<R, C> { +impl <R: BufferedReader<C>, C> Deflate<R, C> { /// Like `new()`, but uses a cookie. /// /// The cookie can be retrieved using the `cookie_ref` and /// `cookie_mut` methods, and set using the `cookie_set` method. pub fn with_cookie(reader: R, cookie: C) -> Self { - BufferedReaderDeflate { - reader: BufferedReaderGeneric::with_cookie( + Deflate { + reader: Generic::with_cookie( DeflateDecoder::new(reader), None, cookie), } } } -impl<R: BufferedReader<C>, C> io::Read for BufferedReaderDeflate<R, C> { +impl<R: BufferedReader<C>, C> io::Read for Deflate<R, C> { fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> { self.reader.read(buf) } } -impl<R: BufferedReader<C>, C> fmt::Display for BufferedReaderDeflate<R, C> { +impl<R: BufferedReader<C>, C> fmt::Display for Deflate<R, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "BufferedReaderDeflate") + write!(f, "Deflate") } } -impl<R: BufferedReader<C>, C> fmt::Debug for BufferedReaderDeflate<R, C> { +impl<R: BufferedReader<C>, C> fmt::Debug for Deflate<R, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("BufferedReaderDeflate") + f.debug_struct("Deflate") .field("reader", &self.get_ref().unwrap()) .finish() } } impl<R: BufferedReader<C>, C> BufferedReader<C> - for BufferedReaderDeflate<R, C> { + for Deflate<R, C> { fn buffer(&self) -> &[u8] { return self.reader.buffer(); } @@ -130,11 +130,11 @@ impl<R: BufferedReader<C>, C> BufferedReader<C> /// Decompresses the underlying `BufferedReader` using the zlib /// algorithm. -pub struct BufferedReaderZlib<R: BufferedReader<C>, C> { - reader: BufferedReaderGeneric<ZlibDecoder<R>, C>, +pub struct Zlib<R: BufferedReader<C>, C> { + reader: Generic<ZlibDecoder<R>, C>, } -impl <R: BufferedReader<()>> BufferedReaderZlib<R, ()> { +impl <R: BufferedReader<()>> Zlib<R, ()> { /// Instantiates a new zlib decompression reader. /// /// `reader` is the source to wrap. @@ -143,41 +143,41 @@ impl <R: BufferedReader<()>> BufferedReaderZlib<R, ()> { } } -impl <R: BufferedReader<C>, C> BufferedReaderZlib<R, C> { +impl <R: BufferedReader<C>, C> Zlib<R, C> { /// Like `new()`, but uses a cookie. /// /// The cookie can be retrieved using the `cookie_ref` and /// `cookie_mut` methods, and set using the `cookie_set` method. pub fn with_cookie(reader: R, cookie: C) -> Self { - BufferedReaderZlib { - reader: BufferedReaderGeneric::with_cookie( + Zlib { + reader: Generic::with_cookie( ZlibDecoder::new(reader), None, cookie), } } } -impl<R: BufferedReader<C>, C> io::Read for BufferedReaderZlib<R, C> { +impl<R: BufferedReader<C>, C> io::Read for Zlib<R, C> { fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> { self.reader.read(buf) } } -impl<R: BufferedReader<C>, C> fmt::Display for BufferedReaderZlib<R, C> { +impl<R: BufferedReader<C>, C> fmt::Display for Zlib<R, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "BufferedReaderZlib") + write!(f, "Zlib") } } -impl<R: BufferedReader<C>, C> fmt::Debug for BufferedReaderZlib<R, C> { +impl<R: BufferedReader<C>, C> fmt::Debug for Zlib<R, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("BufferedReaderZlib") + f.debug_struct("Zlib") .field("reader", &self.get_ref().unwrap()) .finish() } } impl<R: BufferedReader<C>, C> BufferedReader<C> - for BufferedReaderZlib<R, C> { + for Zlib<R, C> { fn buffer(&self) -> &[u8] { return self.reader.buffer(); } @@ -283,8 +283,8 @@ mod test { encoder.try_finish().unwrap(); } - let mut reader = BufferedReaderDeflate::new( - BufferedReaderGeneric::new(&input[..], None)); + let mut reader = Deflate::new( + Generic::new(&input[..], None)); // Gather some stats to make it easier to figure out whether // this test is working. diff --git a/buffered-reader/src/dup.rs b/buffered-reader/src/dup.rs index 123e13d6..edebc373 100644 --- a/buffered-reader/src/dup.rs +++ b/buffered-reader/src/dup.rs @@ -10,7 +10,7 @@ use super::*; /// Note: this will likely cause the underlying stream to buffer as /// much data as you read. Thus, it should only be used for peeking /// at the underlying `BufferedReader`. -pub struct BufferedReaderDup<'a, C> { +pub struct Dup<'a, C> { reader: Box<'a + BufferedReader<C>>, // The number of bytes that have been consumed. @@ -20,24 +20,24 @@ pub struct BufferedReaderDup<'a, C> { cookie: C, } -impl<'a, C> fmt::Display for BufferedReaderDup<'a, C> { +impl<'a, C> fmt::Display for Dup<'a, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "BufferedReaderDup ({} bytes consumed)", + write!(f, "Dup ({} bytes consumed)", self.cursor) } } -impl<'a, C> fmt::Debug for BufferedReaderDup<'a, C> { +impl<'a, C> fmt::Debug for Dup<'a, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("BufferedReaderDup") + f.debug_struct("Dup") .field("cursor", &self.cursor) .field("reader", &self.reader) .finish() } } -impl<'a> BufferedReaderDup<'a, ()> { - /// Instantiates a new `BufferedReaderDup` buffered reader. +impl<'a> Dup<'a, ()> { + /// Instantiates a new `Dup` buffered reader. /// /// `reader` is the `BufferedReader` to duplicate. pub fn new(reader: Box<'a + BufferedReader<()>>) -> Self { @@ -45,13 +45,13 @@ impl<'a> BufferedReaderDup<'a, ()> { } } -impl<'a, C> BufferedReaderDup<'a, C> { +impl<'a, C> Dup<'a, C> { /// Like `new()`, but uses a cookie. /// /// The cookie can be retrieved using the `cookie_ref` and /// `cookie_mut` methods, and set using the `cookie_set` method. pub fn with_cookie(reader: Box<'a + BufferedReader<C>>, cookie: C) -> Self { - BufferedReaderDup { + Dup { reader: reader, cursor: 0, cookie: cookie, @@ -69,7 +69,7 @@ impl<'a, C> BufferedReaderDup<'a, C> { } } -impl<'a, C> io::Read for BufferedReaderDup<'a, C> { +impl<'a, C> io::Read for Dup<'a, C> { fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> { let data = self.reader.data(self.cursor + buf.len())?; assert!(data.len() >= self.cursor); @@ -84,7 +84,7 @@ impl<'a, C> io::Read for BufferedReaderDup<'a, C> { } } -impl<'a, C> BufferedReader<C> for BufferedReaderDup<'a, C> { +impl<'a, C> BufferedReader<C> for Dup<'a, C> { fn buffer(&self) -> &[u8] { let data = self.reader.buffer(); assert!(data.len() >= self.cursor); @@ -156,8 +156,8 @@ mod test { #[test] fn buffered_reader_memory_test () { let data : &[u8] = include_bytes!("buffered-reader-test.txt"); - let reader = BufferedReaderMemory::new(data); - let mut reader = BufferedReaderDup::new(Box::new(reader)); + let reader = Memory::new(data); + let mut reader = Dup::new(Box::new(reader)); buffered_reader_test_data_check(&mut reader); @@ -178,7 +178,7 @@ mod test { // Test that buffer() returns the same data as data(). #[test] fn buffer_test() { - // Test vector. A BufferedReaderDup returns all unconsumed + // Test vector. A Dup returns all unconsumed // data. So, use a relatively small buffer size. let size = DEFAULT_BUF_SIZE; let mut input = Vec::with_capacity(size); @@ -192,8 +192,8 @@ mod test { } } - let reader = BufferedReaderMemory::new(&input[..]); - let mut reader = BufferedReaderDup::new(Box::new(reader)); + let reader = Memory::new(&input[..]); + let mut reader = Dup::new(Box::new(reader)); for i in 0..input.len() { let data = reader.data(DEFAULT_BUF_SIZE + 1).unwrap().to_vec(); diff --git a/buffered-reader/src/eof.rs b/buffered-reader/src/eof.rs index 510dc2df..bf0dc7ef 100644 --- a/buffered-reader/src/eof.rs +++ b/buffered-reader/src/eof.rs @@ -5,48 +5,48 @@ use std::fmt; use BufferedReader; /// Always returns EOF. -pub struct BufferedReaderEOF<C> { +pub struct EOF<C> { cookie: C, } -impl<C> fmt::Display for BufferedReaderEOF<C> { +impl<C> fmt::Display for EOF<C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "BufferedReaderEOF") + write!(f, "EOF") } } -impl<C> fmt::Debug for BufferedReaderEOF<C> { +impl<C> fmt::Debug for EOF<C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("BufferedReaderEOF") + f.debug_struct("EOF") .finish() } } -impl BufferedReaderEOF<()> { - /// Instantiates a new `BufferedReaderEOF`. +impl EOF<()> { + /// Instantiates a new `EOF`. pub fn new() -> Self { - BufferedReaderEOF { + EOF { cookie: (), } } } -impl<C> BufferedReaderEOF<C> { - /// Instantiates a new `BufferedReaderEOF` with a cookie. +impl<C> EOF<C> { + /// Instantiates a new `EOF` with a cookie. pub fn with_cookie(cookie: C) -> Self { - BufferedReaderEOF { + EOF { cookie: cookie, } } } -impl<C> Read for BufferedReaderEOF<C> { +impl<C> Read for EOF<C> { fn read(&mut self, _buf: &mut [u8]) -> Result<usize, io::Error> { return Ok(0); } } -impl<C> BufferedReader<C> for BufferedReaderEOF<C> { +impl<C> BufferedReader<C> for EOF<C> { fn buffer(&self) -> &[u8] { return &b""[..]; } @@ -110,7 +110,7 @@ mod test { #[test] fn basics() { - let mut reader = BufferedReaderEOF::new(); + let mut reader = EOF::new(); assert_eq!(reader.buffer(), &b""[..]); assert_eq!(reader.data(100).unwrap(), &b""[..]); diff --git a/buffered-reader/src/file_generic.rs b/buffered-reader/src/file_generic.rs index c0cc489d..3c83bb73 100644 --- a/buffered-reader/src/file_generic.rs +++ b/buffered-reader/src/file_generic.rs @@ -1,5 +1,5 @@ use std::fmt; -use std::fs::File; +use std::fs; use std::io; use std::path::Path; @@ -9,46 +9,44 @@ use super::*; /// /// This is a generic implementation that may be replaced by /// platform-specific versions. -pub struct BufferedReaderFile<C>(BufferedReaderGeneric<File, C>); +pub struct File<C>(Generic<fs::File, C>); -impl<C> fmt::Display for BufferedReaderFile<C> { +impl<C> fmt::Display for File<C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "BufferedReaderFile") + write!(f, "File") } } -impl<C> fmt::Debug for BufferedReaderFile<C> { +impl<C> fmt::Debug for File<C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_tuple("BufferedReaderFile") + f.debug_tuple("File") .field(&self.0) .finish() } } -impl BufferedReaderFile<()> { +impl File<()> { /// Opens the given file. pub fn open<P: AsRef<Path>>(path: P) -> io::Result<Self> { Self::with_cookie(path, ()) } } -impl<C> BufferedReaderFile<C> { +impl<C> File<C> { /// Like `open()`, but sets a cookie. pub fn with_cookie<P: AsRef<Path>>(path: P, cookie: C) -> io::Result<Self> { - Ok(BufferedReaderFile( - BufferedReaderGeneric::with_cookie(File::open(path)?, - None, - cookie))) + Ok(File(Generic::with_cookie(fs::File::open(path)?, + None, cookie))) } } -impl<C> io::Read for BufferedReaderFile<C> { +impl<C> io::Read for File<C> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { self.0.read(buf) } } -impl<C> BufferedReader<C> for BufferedReaderFile<C> { +impl<C> BufferedReader<C> for File<C> { fn buffer(&self) -> &[u8] { self.0.buffer() } diff --git a/buffered-reader/src/file_unix.rs b/buffered-reader/src/file_unix.rs index bb6f2673..c2e1f912 100644 --- a/buffered-reader/src/file_unix.rs +++ b/buffered-reader/src/file_unix.rs @@ -2,11 +2,11 @@ //! //! On my (Justus) system, this implementation improves the //! performance of the statistics example by ~10% over the -//! BufferedReaderGeneric. +//! Generic. use libc::{c_void, size_t, mmap, munmap, PROT_READ, MAP_PRIVATE}; use std::fmt; -use std::fs::File; +use std::fs; use std::io; use std::os::unix::io::AsRawFd; use std::slice; @@ -24,17 +24,17 @@ const MMAP_THRESHOLD: u64 = 16 * 4096; /// /// This implementation tries to mmap the file, falling back to /// just using a generic reader. -pub struct BufferedReaderFile<'a, C>(Imp<'a, C>); +pub struct File<'a, C>(Imp<'a, C>); -impl<'a, C> fmt::Display for BufferedReaderFile<'a, C> { +impl<'a, C> fmt::Display for File<'a, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.0) } } -impl<'a, C> fmt::Debug for BufferedReaderFile<'a, C> { +impl<'a, C> fmt::Debug for File<'a, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_tuple("BufferedReaderFile") + f.debug_tuple("File") .field(&self.0) .finish() } @@ -42,11 +42,11 @@ impl<'a, C> fmt::Debug for BufferedReaderFile<'a, C> { /// The implementation. enum Imp<'a, C> { - Generic(BufferedReaderGeneric<File, C>), + Generic(Generic<fs::File, C>), MMAP { addr: *mut c_void, length: size_t, - reader: BufferedReaderMemory<'a, C>, + reader: Memory<'a, C>, } } @@ -64,10 +64,10 @@ impl<'a, C> Drop for Imp<'a, C> { impl<'a, C> fmt::Display for Imp<'a, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "BufferedReaderFile(")?; + write!(f, "File(")?; match self { - Imp::Generic(_) => write!(f, "BufferedReaderGeneric")?, - Imp::MMAP { .. } => write!(f, "BufferedReaderMemory")?, + Imp::Generic(_) => write!(f, "Generic")?, + Imp::MMAP { .. } => write!(f, "Memory")?, }; write!(f, ")") } @@ -90,24 +90,24 @@ impl<'a, C> fmt::Debug for Imp<'a, C> { } } -impl<'a> BufferedReaderFile<'a, ()> { +impl<'a> File<'a, ()> { /// Opens the given file. pub fn open<P: AsRef<Path>>(path: P) -> io::Result<Self> { Self::with_cookie(path, ()) } } -impl<'a, C> BufferedReaderFile<'a, C> { +impl<'a, C> File<'a, C> { /// Like `open()`, but sets a cookie. pub fn with_cookie<P: AsRef<Path>>(path: P, cookie: C) -> io::Result<Self> { // As fallback, we use a generic reader. let generic = |file, cookie| { - Ok(BufferedReaderFile( + Ok(File( Imp::Generic( - BufferedReaderGeneric::with_cookie(file, None, cookie)))) + Generic::with_cookie(file, None, cookie)))) }; - let file = File::open(path)?; + let file = fs::File::open(path)?; // For testing and benchmarking purposes, we use the variable // SEQUOIA_DONT_MMAP to turn off mmapping. @@ -142,17 +142,17 @@ impl<'a, C> BufferedReaderFile<'a, C> { slice::from_raw_parts(addr as *const u8, length) }; - Ok(BufferedReaderFile( + Ok(File( Imp::MMAP { addr: addr, length: length, - reader: BufferedReaderMemory::with_cookie(slice, cookie), + reader: Memory::with_cookie(slice, cookie), } )) } } -impl<'a, C> io::Read for BufferedReaderFile<'a, C> { +impl<'a, C> io::Read for File<'a, C> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { match self.0 { Imp::Generic(ref mut reader) => reader.read(buf), @@ -161,7 +161,7 @@ impl<'a, C> io::Read for BufferedReaderFile<'a, C> { } } -impl<'a, C> BufferedReader<C> for BufferedReaderFile<'a, C> { +impl<'a, C> BufferedReader<C> for File<'a, C> { fn buffer(&self) -> &[u8] { match self.0 { Imp::Generic(ref reader) => reader.buffer(), diff --git a/buffered-reader/src/generic.rs b/buffered-reader/src/generic.rs index 882c64e9..1 |