Skip to content

Commit

Permalink
Update LimitReader to take the Reader to wrap by value
Browse files Browse the repository at this point in the history
  • Loading branch information
Palmer Cox committed Feb 15, 2014
1 parent d4dd4c6 commit 4c233d1
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/libstd/io/util.rs
Expand Up @@ -14,19 +14,20 @@ use io;
use vec::bytes::MutableByteVector;

/// Wraps a `Reader`, limiting the number of bytes that can be read from it.
pub struct LimitReader<'a, R> {
pub struct LimitReader<R> {
priv limit: uint,
priv inner: &'a mut R
priv inner: R
}

impl<'a, R: Reader> LimitReader<'a, R> {
impl<R: Reader> LimitReader<R> {
/// Creates a new `LimitReader`
pub fn new<'a>(r: &'a mut R, limit: uint) -> LimitReader<'a, R> {
pub fn new(r: R, limit: uint) -> LimitReader<R> {
LimitReader { limit: limit, inner: r }
}
pub fn unwrap(self) -> R { self.inner }
}

impl<'a, R: Reader> Reader for LimitReader<'a, R> {
impl<R: Reader> Reader for LimitReader<R> {
fn read(&mut self, buf: &mut [u8]) -> io::IoResult<uint> {
if self.limit == 0 {
return Err(io::standard_error(io::EndOfFile));
Expand Down Expand Up @@ -192,7 +193,7 @@ mod test {
fn test_bounded_reader_unlimited() {
let mut r = MemReader::new(~[0, 1, 2]);
{
let mut r = LimitReader::new(&mut r, 4);
let mut r = LimitReader::new(r.by_ref(), 4);
assert_eq!(~[0, 1, 2], r.read_to_end().unwrap());
}
}
Expand All @@ -201,7 +202,7 @@ mod test {
fn test_bound_reader_limited() {
let mut r = MemReader::new(~[0, 1, 2]);
{
let mut r = LimitReader::new(&mut r, 2);
let mut r = LimitReader::new(r.by_ref(), 2);
assert_eq!(~[0, 1], r.read_to_end().unwrap());
}
assert_eq!(~[2], r.read_to_end().unwrap());
Expand Down

5 comments on commit 4c233d1

@bors
Copy link
Contributor

@bors bors commented on 4c233d1 Feb 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from sfackler
at DaGenix@4c233d1

@bors
Copy link
Contributor

@bors bors commented on 4c233d1 Feb 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging DaGenix/rust/io-decorator-changes = 4c233d1 into auto

@bors
Copy link
Contributor

@bors bors commented on 4c233d1 Feb 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DaGenix/rust/io-decorator-changes = 4c233d1 merged ok, testing candidate = c9f13b4

@bors
Copy link
Contributor

@bors bors commented on 4c233d1 Feb 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 4c233d1 Feb 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = c9f13b4

Please sign in to comment.