summaryrefslogtreecommitdiffstats
path: root/buffered-reader
diff options
context:
space:
mode:
authorNeal H. Walfield <neal@pep.foundation>2018-10-04 12:13:01 +0200
committerNeal H. Walfield <neal@pep.foundation>2018-10-04 12:13:01 +0200
commit723459a0c13f19fdc6ca41264959fc5ccdd6e52a (patch)
treefa69680ddec933c57ca7fe3abc82b69b0d299e75 /buffered-reader
parent86ce32cb007db22e0e04f1c7fe49eedaa0c6bf92 (diff)
buffered-reader: Provide default implementations for more methods
- Since it is possible to provide an efficient default implementation of data_consume and data_consume_hard, we should do so.
Diffstat (limited to 'buffered-reader')
-rw-r--r--buffered-reader/src/lib.rs18
1 files changed, 16 insertions, 2 deletions
diff --git a/buffered-reader/src/lib.rs b/buffered-reader/src/lib.rs
index 59493309..c8e30866 100644
--- a/buffered-reader/src/lib.rs
+++ b/buffered-reader/src/lib.rs
@@ -532,15 +532,29 @@ pub trait BufferedReader<C> : io::Read + fmt::Debug {
/// # Ok(()) }
/// ```
fn data_consume(&mut self, amount: usize)
- -> Result<&[u8], std::io::Error>;
+ -> Result<&[u8], std::io::Error> {
+ let amount = cmp::min(amount, self.data(amount)?.len());
+ let buffer = self.consume(amount);
+ assert!(buffer.len() >= amount);
+ Ok(buffer)
+ }
/// A convenience function that effectively combines `data_hard()`
/// and `consume()`.
///
/// This function is identical to `data_consume()`, but internally
/// uses `data_hard()` instead of `data()`.
- fn data_consume_hard(&mut self, amount: usize) -> Result<&[u8], io::Error>;
+ fn data_consume_hard(&mut self, amount: usize)
+ -> Result<&[u8], io::Error>
+ {
+ let len = self.data_hard(amount)?.len();
+ assert!(len >= amount);
+
+ let buffer = self.consume(amount);
+ assert!(buffer.len() >= amount);
+ Ok(buffer)
+ }
/// A convenience function for reading a 16-bit unsigned integer
/// in big endian format.