|
| 1 | +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +#![allow(missing_copy_implementations)] |
| 12 | + |
| 13 | +use prelude::*; |
| 14 | + |
| 15 | +use cmp; |
| 16 | +use error::Error; |
| 17 | +use fmt; |
| 18 | +use void::Void; |
| 19 | +use io::{Read, BufferedRead, Write, Seek, SeekPos}; |
| 20 | +use mem; |
| 21 | +use slice; |
| 22 | + |
| 23 | +/// A `Cursor` is a type which wraps another I/O object to provide a `Seek` |
| 24 | +/// implementation. |
| 25 | +/// |
| 26 | +/// Cursors are currently typically used with memory buffer objects in order to |
| 27 | +/// allow `Seek` plus `Read` and `Write` implementations. For example, common |
| 28 | +/// cursor types include: |
| 29 | +/// |
| 30 | +/// * `Cursor<Vec<u8>>` |
| 31 | +/// * `Cursor<&[u8]>` |
| 32 | +/// |
| 33 | +/// Cursors are not currently generic over the type contained within, but may |
| 34 | +/// become so. |
| 35 | +pub struct Cursor<T> { |
| 36 | + pos: u64, |
| 37 | + inner: T, |
| 38 | +} |
| 39 | + |
| 40 | +/// Error returned from primitive seek implementations indicating that a |
| 41 | +/// negative byte position was seeked to. |
| 42 | +#[derive(Show, PartialEq, Clone, Copy)] |
| 43 | +pub struct NegativeSeek; |
| 44 | + |
| 45 | +impl<T> Cursor<T> { |
| 46 | + /// Create a new cursor wrapping the provided underlying I/O object. |
| 47 | + pub fn new(inner: T) -> Cursor<T> { |
| 48 | + Cursor { pos: 0, inner: inner } |
| 49 | + } |
| 50 | + |
| 51 | + /// Consume this cursor, returning the underlying value. |
| 52 | + pub fn into_inner(self) -> T { self.inner } |
| 53 | + |
| 54 | + /// Get a reference to the underlying value in this cursor. |
| 55 | + pub fn get_ref(&self) -> &T { &self.inner } |
| 56 | + |
| 57 | + /// Get a mutable reference to the underlying value in this cursor. |
| 58 | + /// |
| 59 | + /// Care should be taken to avoid modifying the internal I/O state of the |
| 60 | + /// underlying value as it may corrupt this cursor's position. |
| 61 | + pub fn get_mut(&mut self) -> &mut T { &mut self.inner } |
| 62 | + |
| 63 | + /// Returns the current value of this cursor |
| 64 | + pub fn position(&self) -> u64 { self.pos } |
| 65 | + |
| 66 | + /// Sets the value of this cursor |
| 67 | + pub fn set_position(&mut self, pos: u64) { self.pos = pos; } |
| 68 | +} |
| 69 | + |
| 70 | +macro_rules! seek { |
| 71 | + () => { |
| 72 | + fn seek(&mut self, style: SeekPos) -> Result<u64, NegativeSeek> { |
| 73 | + let pos = match style { |
| 74 | + SeekPos::FromStart(n) => n as i64, |
| 75 | + SeekPos::FromEnd(n) => self.inner.len() as i64 + n, |
| 76 | + SeekPos::FromCur(n) => self.pos as i64 + n, |
| 77 | + }; |
| 78 | + |
| 79 | + if pos < 0 { |
| 80 | + Err(NegativeSeek) |
| 81 | + } else { |
| 82 | + self.pos = pos as u64; |
| 83 | + Ok(self.pos) |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl<'a> Seek for Cursor<&'a [u8]> { type Err = NegativeSeek; seek!(); } |
| 90 | +impl<'a> Seek for Cursor<&'a mut [u8]> { type Err = NegativeSeek; seek!(); } |
| 91 | + |
| 92 | +impl<'a> Read for &'a [u8] { |
| 93 | + type Err = Void; |
| 94 | + #[inline] |
| 95 | + fn read(&mut self, buf: &mut [u8]) -> Result<usize, Void> { |
| 96 | + let write_len = cmp::min(buf.len(), self.len()); |
| 97 | + { |
| 98 | + let input = &self[..write_len]; |
| 99 | + let output = &mut buf[.. write_len]; |
| 100 | + slice::bytes::copy_memory(output, input); |
| 101 | + } |
| 102 | + |
| 103 | + *self = &self[write_len..]; |
| 104 | + |
| 105 | + Ok(write_len) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +macro_rules! read { |
| 110 | + () => { |
| 111 | + fn read(&mut self, buf: &mut [u8]) -> Result<usize, Void> { |
| 112 | + if self.pos > self.inner.len() as u64 { return Ok(0) } |
| 113 | + let mut slice = &self.inner[(self.pos as usize)..]; |
| 114 | + let n = try!(slice.read(buf)); |
| 115 | + self.pos += n as u64; |
| 116 | + Ok(n) |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +impl<'a> Read for Cursor<&'a [u8]> { type Err = Void; read!(); } |
| 122 | +impl<'a> Read for Cursor<&'a mut [u8]> { type Err = Void; read!(); } |
| 123 | + |
| 124 | +macro_rules! buffer { |
| 125 | + () => { |
| 126 | + fn fill_buf(&mut self) -> Result<&[u8], Void> { |
| 127 | + if self.pos < (self.inner.len() as u64) { |
| 128 | + Ok(&self.inner[(self.pos as usize)..]) |
| 129 | + } else { |
| 130 | + Ok(&[]) |
| 131 | + } |
| 132 | + } |
| 133 | + fn consume(&mut self, amt: usize) { self.pos += amt as u64; } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +impl<'a> BufferedRead for Cursor<&'a [u8]> { buffer!(); } |
| 138 | +impl<'a> BufferedRead for Cursor<&'a mut [u8]> { buffer!(); } |
| 139 | + |
| 140 | +impl<'a> Write for &'a mut [u8] { |
| 141 | + type Err = Void; |
| 142 | + |
| 143 | + fn write(&mut self, data: &[u8]) -> Result<usize, Void> { |
| 144 | + let dst_len = self.len(); |
| 145 | + let data_len = data.len(); |
| 146 | + if dst_len >= data_len { |
| 147 | + slice::bytes::copy_memory(*self, data); |
| 148 | + // TODO: is this actually safe? |
| 149 | + *self = unsafe { |
| 150 | + let next: &'a mut [u8] = mem::transmute_copy(&*self); |
| 151 | + &mut next[data_len..] |
| 152 | + }; |
| 153 | + Ok(data_len) |
| 154 | + } else { |
| 155 | + slice::bytes::copy_memory(*self, &data[..dst_len]); |
| 156 | + *self = &mut []; |
| 157 | + Ok(dst_len) |
| 158 | + } |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +impl<'a> Write for Cursor<&'a mut [u8]> { |
| 163 | + type Err = Void; |
| 164 | + |
| 165 | + fn write(&mut self, data: &[u8]) -> Result<usize, Void> { |
| 166 | + if self.pos >= self.inner.len() as u64 { return Ok(0) } |
| 167 | + |
| 168 | + let amt = { |
| 169 | + let mut s = &mut self.inner[(self.pos as usize)..]; |
| 170 | + try!(s.write(data)) |
| 171 | + }; |
| 172 | + self.pos += amt as u64; |
| 173 | + Ok(amt) |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +impl fmt::Display for NegativeSeek { |
| 178 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 179 | + "cannot seek to a negative position".fmt(f) |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +impl Error for NegativeSeek { |
| 184 | + fn description(&self) -> &str { "seek error" } |
| 185 | +} |
0 commit comments