Skip to content

Add Reader::skip_exact method #19025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/libstd/io/buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ impl<R: Reader> Reader for BufferedReader<R> {
self.pos += nread;
Ok(nread)
}

fn skip_exact(&mut self, n: uint) -> IoResult<()> {
if self.pos + n <= self.cap {
self.pos += n;
}
else {
let skip = n - (self.cap - self.pos);
self.pos = self.cap;
try!(self.inner.skip_exact(skip));
}
Ok(())
}
}

/// Wraps a Writer and buffers output to it
Expand Down Expand Up @@ -356,6 +368,10 @@ impl<S: Stream> Reader for BufferedStream<S> {
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
self.inner.read(buf)
}

fn skip_exact(&mut self, n: uint) -> IoResult<()> {
self.inner.skip_exact(n)
}
}

impl<S: Stream> Writer for BufferedStream<S> {
Expand Down
10 changes: 9 additions & 1 deletion src/libstd/io/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fs::unlink(&path);
use clone::Clone;
use io::standard_error;
use io::{FilePermission, Write, Open, FileAccess, FileMode};
use io::{IoResult, IoError, FileStat, SeekStyle, Seek, Writer, Reader};
use io::{IoResult, IoError, FileStat, SeekStyle, Seek, SeekCur, Writer, Reader};
use io::{Read, Truncate, ReadWrite, Append};
use io::UpdateIoError;
use io;
Expand All @@ -67,6 +67,7 @@ use result::{Err, Ok};
use slice::SlicePrelude;
use string::String;
use vec::Vec;
use num::ToPrimitive;

use sys::fs as fs_imp;
use sys_common;
Expand Down Expand Up @@ -706,6 +707,13 @@ impl Reader for File {
Err(e) => Err(e)
}
}

fn skip_exact(&mut self, n: uint) -> IoResult<()> {
match n.to_i64() {
Some(i) => self.seek(i, SeekCur),
None => Err(io::standard_error(io::InvalidInput)),
}
}
}

impl Writer for File {
Expand Down
26 changes: 26 additions & 0 deletions src/libstd/io/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ impl Reader for MemReader {

return Ok(write_len);
}

#[inline]
fn skip_exact(&mut self, n: uint) -> IoResult<()> {
let remaining = self.buf.len() - self.pos;
if remaining < n {
self.pos = self.buf.len();
Err(io::standard_error(io::EndOfFile))
}
else {
self.pos += n;
Ok(())
}
}
}

impl Seek for MemReader {
Expand Down Expand Up @@ -305,6 +318,19 @@ impl<'a> Reader for BufReader<'a> {

return Ok(write_len);
}

#[inline]
fn skip_exact(&mut self, n: uint) -> IoResult<()> {
let remaining = self.buf.len() - self.pos;
if remaining < n {
self.pos = self.buf.len();
Err(io::standard_error(io::EndOfFile))
}
else {
self.pos += n;
Ok(())
}
}
}

impl<'a> Seek for BufReader<'a> {
Expand Down
27 changes: 25 additions & 2 deletions src/libstd/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,12 @@ pub trait Reader {
fn read_i8(&mut self) -> IoResult<i8> {
self.read_byte().map(|i| i as i8)
}

/// Skip `n` bytes of input.
fn skip_exact(&mut self, n: uint) -> IoResult<()> {
try!(self.read_exact(n));
Ok(())
}
}

/// A reader which can be converted to a RefReader.
Expand Down Expand Up @@ -903,10 +909,21 @@ impl<'a> Reader for Box<Reader+'a> {
let reader: &mut Reader = &mut **self;
reader.read(buf)
}

fn skip_exact(&mut self, n: uint) -> IoResult<()> {
let reader: &mut Reader = &mut **self;
reader.skip_exact(n)
}
}

impl<'a> Reader for &'a mut Reader+'a {
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> { (*self).read(buf) }
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
(*self).read(buf)
}

fn skip_exact(&mut self, n: uint) -> IoResult<()> {
(*self).skip_exact(n)
}
}

/// Returns a slice of `v` between `start` and `end`.
Expand Down Expand Up @@ -963,7 +980,13 @@ pub struct RefReader<'a, R:'a> {
}

impl<'a, R: Reader> Reader for RefReader<'a, R> {
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> { self.inner.read(buf) }
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
self.inner.read(buf)
}

fn skip_exact(&mut self, n: uint) -> IoResult<()> {
self.inner.skip_exact(n)
}
}

impl<'a, R: Buffer> Buffer for RefReader<'a, R> {
Expand Down
5 changes: 5 additions & 0 deletions src/libstd/io/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ impl Reader for ZeroReader {
buf.set_memory(0);
Ok(buf.len())
}

#[inline]
fn skip_exact(&mut self, _: uint) -> io::IoResult<()> {
Ok(())
}
}

impl Buffer for ZeroReader {
Expand Down