| // Copyright (C) 2017-2018 Baidu, Inc. All Rights Reserved. |
| // |
| // Redistribution and use in source and binary forms, with or without |
| // modification, are permitted provided that the following conditions |
| // are met: |
| // |
| // * Redistributions of source code must retain the above copyright |
| // notice, this list of conditions and the following disclaimer. |
| // * Redistributions in binary form must reproduce the above copyright |
| // notice, this list of conditions and the following disclaimer in |
| // the documentation and/or other materials provided with the |
| // distribution. |
| // * Neither the name of Baidu, Inc., nor the names of its |
| // contributors may be used to endorse or promote products derived |
| // from this software without specific prior written permission. |
| // |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| |
| use io::prelude::*; |
| use io::{self, Initializer, SeekFrom, Error, ErrorKind}; |
| use core::cmp; |
| |
| /// A `Cursor` wraps another type and provides it with a |
| /// [`Seek`] implementation. |
| /// |
| /// `Cursor`s are typically used with in-memory buffers to allow them to |
| /// implement [`Read`] and/or [`Write`], allowing these buffers to be used |
| /// anywhere you might use a reader or writer that does actual I/O. |
| /// |
| /// The standard library implements some I/O traits on various types which |
| /// are commonly used as a buffer, like `Cursor<`[`Vec`]`<u8>>` and |
| /// `Cursor<`[`&[u8]`][bytes]`>`. |
| /// |
| |
| #[derive(Clone, Debug)] |
| pub struct Cursor<T> { |
| inner: T, |
| pos: u64, |
| } |
| |
| impl<T> Cursor<T> { |
| /// Creates a new cursor wrapping the provided underlying I/O object. |
| /// |
| /// Cursor initial position is `0` even if underlying object (e. |
| /// g. `Vec`) is not empty. So writing to cursor starts with |
| /// overwriting `Vec` content, not with appending to it. |
| /// |
| pub fn new(inner: T) -> Cursor<T> { |
| Cursor { pos: 0, inner: inner } |
| } |
| |
| /// Consumes this cursor, returning the underlying value. |
| pub fn into_inner(self) -> T { self.inner } |
| |
| /// Gets a reference to the underlying value in this cursor. |
| pub fn get_ref(&self) -> &T { &self.inner } |
| |
| /// Gets a mutable reference to the underlying value in this cursor. |
| /// |
| /// Care should be taken to avoid modifying the internal I/O state of the |
| /// underlying value as it may corrupt this cursor's position. |
| pub fn get_mut(&mut self) -> &mut T { &mut self.inner } |
| |
| /// Returns the current position of this cursor. |
| pub fn position(&self) -> u64 { self.pos } |
| |
| /// Sets the position of this cursor. |
| pub fn set_position(&mut self, pos: u64) { self.pos = pos; } |
| } |
| |
| impl<T> io::Seek for Cursor<T> where T: AsRef<[u8]> { |
| fn seek(&mut self, style: SeekFrom) -> io::Result<u64> { |
| let (base_pos, offset) = match style { |
| SeekFrom::Start(n) => { self.pos = n; return Ok(n); } |
| SeekFrom::End(n) => (self.inner.as_ref().len() as u64, n), |
| SeekFrom::Current(n) => (self.pos, n), |
| }; |
| let new_pos = if offset >= 0 { |
| base_pos.checked_add(offset as u64) |
| } else { |
| base_pos.checked_sub((offset.wrapping_neg()) as u64) |
| }; |
| match new_pos { |
| Some(n) => {self.pos = n; Ok(self.pos)} |
| None => Err(Error::new(ErrorKind::InvalidInput, |
| "invalid seek to a negative or overflowing position")) |
| } |
| } |
| } |
| |
| impl<T> Read for Cursor<T> where T: AsRef<[u8]> { |
| fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
| let n = Read::read(&mut self.fill_buf()?, buf)?; |
| self.pos += n as u64; |
| Ok(n) |
| } |
| |
| fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { |
| let n = buf.len(); |
| Read::read_exact(&mut self.fill_buf()?, buf)?; |
| self.pos += n as u64; |
| Ok(()) |
| } |
| |
| #[inline] |
| unsafe fn initializer(&self) -> Initializer { |
| Initializer::nop() |
| } |
| } |
| |
| impl<T> BufRead for Cursor<T> where T: AsRef<[u8]> { |
| fn fill_buf(&mut self) -> io::Result<&[u8]> { |
| let amt = cmp::min(self.pos, self.inner.as_ref().len() as u64); |
| Ok(&self.inner.as_ref()[(amt as usize)..]) |
| } |
| fn consume(&mut self, amt: usize) { self.pos += amt as u64; } |
| } |
| |
| // Non-resizing write implementation |
| fn slice_write(pos_mut: &mut u64, slice: &mut [u8], buf: &[u8]) -> io::Result<usize> { |
| let pos = cmp::min(*pos_mut, slice.len() as u64); |
| let amt = (&mut slice[(pos as usize)..]).write(buf)?; |
| *pos_mut += amt as u64; |
| Ok(amt) |
| } |
| |
| /// Compensate removal of some impls per |
| #[cfg(any(target_pointer_width = "16", |
| target_pointer_width = "32"))] |
| fn try_into(n: u64) -> Result<usize, ()> { |
| if n <= (<usize>::max_value() as u64) { |
| Ok(n as usize) |
| } else { |
| Err(()) |
| } |
| } |
| |
| #[cfg(any(target_pointer_width = "64"))] |
| fn try_into(n: u64) -> Result<usize, ()> { |
| Ok(n as usize) |
| } |
| |
| // Resizing write implementation |
| fn vec_write(pos_mut: &mut u64, vec: &mut Vec<u8>, buf: &[u8]) -> io::Result<usize> { |
| let pos: usize = try_into(*pos_mut).map_err(|_| { |
| Error::new(ErrorKind::InvalidInput, |
| "cursor position exceeds maximum possible vector length") |
| })?; |
| // Make sure the internal buffer is as least as big as where we |
| // currently are |
| let len = vec.len(); |
| if len < pos { |
| // use `resize` so that the zero filling is as efficient as possible |
| vec.resize(pos, 0); |
| } |
| // Figure out what bytes will be used to overwrite what's currently |
| // there (left), and what will be appended on the end (right) |
| { |
| let space = vec.len() - pos; |
| let (left, right) = buf.split_at(cmp::min(space, buf.len())); |
| vec[pos..pos + left.len()].copy_from_slice(left); |
| vec.extend_from_slice(right); |
| } |
| |
| // Bump us forward |
| *pos_mut = (pos + buf.len()) as u64; |
| Ok(buf.len()) |
| } |
| |
| impl<'a> Write for Cursor<&'a mut [u8]> { |
| #[inline] |
| fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| slice_write(&mut self.pos, self.inner, buf) |
| } |
| fn flush(&mut self) -> io::Result<()> { Ok(()) } |
| } |
| |
| impl<'a> Write for Cursor<&'a mut Vec<u8>> { |
| fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| vec_write(&mut self.pos, self.inner, buf) |
| } |
| fn flush(&mut self) -> io::Result<()> { Ok(()) } |
| } |
| |
| impl Write for Cursor<Vec<u8>> { |
| fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| vec_write(&mut self.pos, &mut self.inner, buf) |
| } |
| fn flush(&mut self) -> io::Result<()> { Ok(()) } |
| } |
| |
| impl Write for Cursor<Box<[u8]>> { |
| #[inline] |
| fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| slice_write(&mut self.pos, &mut self.inner, buf) |
| } |
| fn flush(&mut self) -> io::Result<()> { Ok(()) } |
| } |