cursor.rs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. use core::prelude::v1::*;
  11. use io::prelude::*;
  12. use core::cmp;
  13. use io::{self, SeekFrom, Error, ErrorKind};
  14. /// A `Cursor` wraps another type and provides it with a
  15. /// [`Seek`](trait.Seek.html) implementation.
  16. ///
  17. /// Cursors are typically used with in-memory buffers to allow them to
  18. /// implement `Read` and/or `Write`, allowing these buffers to be used
  19. /// anywhere you might use a reader or writer that does actual I/O.
  20. ///
  21. /// The standard library implements some I/O traits on various types which
  22. /// are commonly used as a buffer, like `Cursor<Vec<u8>>` and `Cursor<&[u8]>`.
  23. ///
  24. /// # Examples
  25. ///
  26. /// We may want to write bytes to a [`File`][file] in our production
  27. /// code, but use an in-memory buffer in our tests. We can do this with
  28. /// `Cursor`:
  29. ///
  30. /// [file]: ../fs/struct.File.html
  31. ///
  32. /// ```no_run
  33. /// use std::io::prelude::*;
  34. /// use std::io::{self, SeekFrom};
  35. /// use std::fs::File;
  36. ///
  37. /// // a library function we've written
  38. /// fn write_ten_bytes_at_end<W: Write + Seek>(writer: &mut W) -> io::Result<()> {
  39. /// try!(writer.seek(SeekFrom::End(-10)));
  40. ///
  41. /// for i in 0..10 {
  42. /// try!(writer.write(&[i]));
  43. /// }
  44. ///
  45. /// // all went well
  46. /// Ok(())
  47. /// }
  48. ///
  49. /// # fn foo() -> io::Result<()> {
  50. /// // Here's some code that uses this library function.
  51. /// //
  52. /// // We might want to use a BufReader here for efficiency, but let's
  53. /// // keep this example focused.
  54. /// let mut file = try!(File::create("foo.txt"));
  55. ///
  56. /// try!(write_ten_bytes_at_end(&mut file));
  57. /// # Ok(())
  58. /// # }
  59. ///
  60. /// // now let's write a test
  61. /// #[test]
  62. /// fn test_writes_bytes() {
  63. /// // setting up a real File is much more slow than an in-memory buffer,
  64. /// // let's use a cursor instead
  65. /// use std::io::Cursor;
  66. /// let mut buff = Cursor::new(vec![0; 15]);
  67. ///
  68. /// write_ten_bytes_at_end(&mut buff).unwrap();
  69. ///
  70. /// assert_eq!(&buff.get_ref()[5..15], &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
  71. /// }
  72. /// ```
  73. #[derive(Clone, Debug)]
  74. pub struct Cursor<T> {
  75. inner: T,
  76. pos: u64,
  77. }
  78. impl<T> Cursor<T> {
  79. /// Creates a new cursor wrapping the provided underlying I/O object.
  80. ///
  81. /// # Examples
  82. ///
  83. /// ```
  84. /// use std::io::Cursor;
  85. ///
  86. /// let buff = Cursor::new(Vec::new());
  87. /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
  88. /// # force_inference(&buff);
  89. /// ```
  90. pub fn new(inner: T) -> Cursor<T> {
  91. Cursor { pos: 0, inner: inner }
  92. }
  93. /// Consumes this cursor, returning the underlying value.
  94. ///
  95. /// # Examples
  96. ///
  97. /// ```
  98. /// use std::io::Cursor;
  99. ///
  100. /// let buff = Cursor::new(Vec::new());
  101. /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
  102. /// # force_inference(&buff);
  103. ///
  104. /// let vec = buff.into_inner();
  105. /// ```
  106. pub fn into_inner(self) -> T { self.inner }
  107. /// Gets a reference to the underlying value in this cursor.
  108. ///
  109. /// # Examples
  110. ///
  111. /// ```
  112. /// use std::io::Cursor;
  113. ///
  114. /// let buff = Cursor::new(Vec::new());
  115. /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
  116. /// # force_inference(&buff);
  117. ///
  118. /// let reference = buff.get_ref();
  119. /// ```
  120. pub fn get_ref(&self) -> &T { &self.inner }
  121. /// Gets a mutable reference to the underlying value in this cursor.
  122. ///
  123. /// Care should be taken to avoid modifying the internal I/O state of the
  124. /// underlying value as it may corrupt this cursor's position.
  125. ///
  126. /// # Examples
  127. ///
  128. /// ```
  129. /// use std::io::Cursor;
  130. ///
  131. /// let mut buff = Cursor::new(Vec::new());
  132. /// # fn force_inference(_: &Cursor<Vec<u8>>) {}
  133. /// # force_inference(&buff);
  134. ///
  135. /// let reference = buff.get_mut();
  136. /// ```
  137. pub fn get_mut(&mut self) -> &mut T { &mut self.inner }
  138. /// Returns the current position of this cursor.
  139. ///
  140. /// # Examples
  141. ///
  142. /// ```
  143. /// use std::io::Cursor;
  144. /// use std::io::prelude::*;
  145. /// use std::io::SeekFrom;
  146. ///
  147. /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
  148. ///
  149. /// assert_eq!(buff.position(), 0);
  150. ///
  151. /// buff.seek(SeekFrom::Current(2)).unwrap();
  152. /// assert_eq!(buff.position(), 2);
  153. ///
  154. /// buff.seek(SeekFrom::Current(-1)).unwrap();
  155. /// assert_eq!(buff.position(), 1);
  156. /// ```
  157. pub fn position(&self) -> u64 { self.pos }
  158. /// Sets the position of this cursor.
  159. ///
  160. /// # Examples
  161. ///
  162. /// ```
  163. /// use std::io::Cursor;
  164. ///
  165. /// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
  166. ///
  167. /// assert_eq!(buff.position(), 0);
  168. ///
  169. /// buff.set_position(2);
  170. /// assert_eq!(buff.position(), 2);
  171. ///
  172. /// buff.set_position(4);
  173. /// assert_eq!(buff.position(), 4);
  174. /// ```
  175. pub fn set_position(&mut self, pos: u64) { self.pos = pos; }
  176. }
  177. impl<T> io::Seek for Cursor<T> where T: AsRef<[u8]> {
  178. fn seek(&mut self, style: SeekFrom) -> io::Result<u64> {
  179. let pos = match style {
  180. SeekFrom::Start(n) => { self.pos = n; return Ok(n) }
  181. SeekFrom::End(n) => self.inner.as_ref().len() as i64 + n,
  182. SeekFrom::Current(n) => self.pos as i64 + n,
  183. };
  184. if pos < 0 {
  185. Err(Error::new(ErrorKind::InvalidInput,
  186. "invalid seek to a negative position"))
  187. } else {
  188. self.pos = pos as u64;
  189. Ok(self.pos)
  190. }
  191. }
  192. }
  193. impl<T> Read for Cursor<T> where T: AsRef<[u8]> {
  194. fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
  195. let n = Read::read(&mut self.fill_buf()?, buf)?;
  196. self.pos += n as u64;
  197. Ok(n)
  198. }
  199. }
  200. impl<T> BufRead for Cursor<T> where T: AsRef<[u8]> {
  201. fn fill_buf(&mut self) -> io::Result<&[u8]> {
  202. let amt = cmp::min(self.pos, self.inner.as_ref().len() as u64);
  203. Ok(&self.inner.as_ref()[(amt as usize)..])
  204. }
  205. fn consume(&mut self, amt: usize) { self.pos += amt as u64; }
  206. }
  207. impl<'a> Write for Cursor<&'a mut [u8]> {
  208. #[inline]
  209. fn write(&mut self, data: &[u8]) -> io::Result<usize> {
  210. let pos = cmp::min(self.pos, self.inner.len() as u64);
  211. let amt = (&mut self.inner[(pos as usize)..]).write(data)?;
  212. self.pos += amt as u64;
  213. Ok(amt)
  214. }
  215. fn flush(&mut self) -> io::Result<()> { Ok(()) }
  216. }
  217. impl Write for Cursor<Vec<u8>> {
  218. fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
  219. // Make sure the internal buffer is as least as big as where we
  220. // currently are
  221. let pos = self.position();
  222. let amt = pos.saturating_sub(self.inner.len() as u64);
  223. // use `resize` so that the zero filling is as efficient as possible
  224. let len = self.inner.len();
  225. self.inner.resize(len + amt as usize, 0);
  226. // Figure out what bytes will be used to overwrite what's currently
  227. // there (left), and what will be appended on the end (right)
  228. {
  229. let pos = pos as usize;
  230. let space = self.inner.len() - pos;
  231. let (left, right) = buf.split_at(cmp::min(space, buf.len()));
  232. self.inner[pos..pos + left.len()].copy_from_slice(left);
  233. self.inner.extend_from_slice(right);
  234. }
  235. // Bump us forward
  236. self.set_position(pos + buf.len() as u64);
  237. Ok(buf.len())
  238. }
  239. fn flush(&mut self) -> io::Result<()> { Ok(()) }
  240. }
  241. impl Write for Cursor<Box<[u8]>> {
  242. #[inline]
  243. fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
  244. let pos = cmp::min(self.pos, self.inner.len() as u64);
  245. let amt = (&mut self.inner[(pos as usize)..]).write(buf)?;
  246. self.pos += amt as u64;
  247. Ok(amt)
  248. }
  249. fn flush(&mut self) -> io::Result<()> { Ok(()) }
  250. }
  251. #[cfg(test)]
  252. mod tests {
  253. use io::prelude::*;
  254. use io::{Cursor, SeekFrom};
  255. #[test]
  256. fn test_vec_writer() {
  257. let mut writer = Vec::new();
  258. assert_eq!(writer.write(&[0]).unwrap(), 1);
  259. assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3);
  260. assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4);
  261. let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7];
  262. assert_eq!(writer, b);
  263. }
  264. #[test]
  265. fn test_mem_writer() {
  266. let mut writer = Cursor::new(Vec::new());
  267. assert_eq!(writer.write(&[0]).unwrap(), 1);
  268. assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3);
  269. assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4);
  270. let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7];
  271. assert_eq!(&writer.get_ref()[..], b);
  272. }
  273. #[test]
  274. fn test_box_slice_writer() {
  275. let mut writer = Cursor::new(vec![0u8; 9].into_boxed_slice());
  276. assert_eq!(writer.position(), 0);
  277. assert_eq!(writer.write(&[0]).unwrap(), 1);
  278. assert_eq!(writer.position(), 1);
  279. assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3);
  280. assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4);
  281. assert_eq!(writer.position(), 8);
  282. assert_eq!(writer.write(&[]).unwrap(), 0);
  283. assert_eq!(writer.position(), 8);
  284. assert_eq!(writer.write(&[8, 9]).unwrap(), 1);
  285. assert_eq!(writer.write(&[10]).unwrap(), 0);
  286. let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8];
  287. assert_eq!(&**writer.get_ref(), b);
  288. }
  289. #[test]
  290. fn test_buf_writer() {
  291. let mut buf = [0 as u8; 9];
  292. {
  293. let mut writer = Cursor::new(&mut buf[..]);
  294. assert_eq!(writer.position(), 0);
  295. assert_eq!(writer.write(&[0]).unwrap(), 1);
  296. assert_eq!(writer.position(), 1);
  297. assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3);
  298. assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4);
  299. assert_eq!(writer.position(), 8);
  300. assert_eq!(writer.write(&[]).unwrap(), 0);
  301. assert_eq!(writer.position(), 8);
  302. assert_eq!(writer.write(&[8, 9]).unwrap(), 1);
  303. assert_eq!(writer.write(&[10]).unwrap(), 0);
  304. }
  305. let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8];
  306. assert_eq!(buf, b);
  307. }
  308. #[test]
  309. fn test_buf_writer_seek() {
  310. let mut buf = [0 as u8; 8];
  311. {
  312. let mut writer = Cursor::new(&mut buf[..]);
  313. assert_eq!(writer.position(), 0);
  314. assert_eq!(writer.write(&[1]).unwrap(), 1);
  315. assert_eq!(writer.position(), 1);
  316. assert_eq!(writer.seek(SeekFrom::Start(2)).unwrap(), 2);
  317. assert_eq!(writer.position(), 2);
  318. assert_eq!(writer.write(&[2]).unwrap(), 1);
  319. assert_eq!(writer.position(), 3);
  320. assert_eq!(writer.seek(SeekFrom::Current(-2)).unwrap(), 1);
  321. assert_eq!(writer.position(), 1);
  322. assert_eq!(writer.write(&[3]).unwrap(), 1);
  323. assert_eq!(writer.position(), 2);
  324. assert_eq!(writer.seek(SeekFrom::End(-1)).unwrap(), 7);
  325. assert_eq!(writer.position(), 7);
  326. assert_eq!(writer.write(&[4]).unwrap(), 1);
  327. assert_eq!(writer.position(), 8);
  328. }
  329. let b: &[_] = &[1, 3, 2, 0, 0, 0, 0, 4];
  330. assert_eq!(buf, b);
  331. }
  332. #[test]
  333. fn test_buf_writer_error() {
  334. let mut buf = [0 as u8; 2];
  335. let mut writer = Cursor::new(&mut buf[..]);
  336. assert_eq!(writer.write(&[0]).unwrap(), 1);
  337. assert_eq!(writer.write(&[0, 0]).unwrap(), 1);
  338. assert_eq!(writer.write(&[0, 0]).unwrap(), 0);
  339. }
  340. #[test]
  341. fn test_mem_reader() {
  342. let mut reader = Cursor::new(vec!(0, 1, 2, 3, 4, 5, 6, 7));
  343. let mut buf = [];
  344. assert_eq!(reader.read(&mut buf).unwrap(), 0);
  345. assert_eq!(reader.position(), 0);
  346. let mut buf = [0];
  347. assert_eq!(reader.read(&mut buf).unwrap(), 1);
  348. assert_eq!(reader.position(), 1);
  349. let b: &[_] = &[0];
  350. assert_eq!(buf, b);
  351. let mut buf = [0; 4];
  352. assert_eq!(reader.read(&mut buf).unwrap(), 4);
  353. assert_eq!(reader.position(), 5);
  354. let b: &[_] = &[1, 2, 3, 4];
  355. assert_eq!(buf, b);
  356. assert_eq!(reader.read(&mut buf).unwrap(), 3);
  357. let b: &[_] = &[5, 6, 7];
  358. assert_eq!(&buf[..3], b);
  359. assert_eq!(reader.read(&mut buf).unwrap(), 0);
  360. }
  361. #[test]
  362. fn test_boxed_slice_reader() {
  363. let mut reader = Cursor::new(vec!(0, 1, 2, 3, 4, 5, 6, 7).into_boxed_slice());
  364. let mut buf = [];
  365. assert_eq!(reader.read(&mut buf).unwrap(), 0);
  366. assert_eq!(reader.position(), 0);
  367. let mut buf = [0];
  368. assert_eq!(reader.read(&mut buf).unwrap(), 1);
  369. assert_eq!(reader.position(), 1);
  370. let b: &[_] = &[0];
  371. assert_eq!(buf, b);
  372. let mut buf = [0; 4];
  373. assert_eq!(reader.read(&mut buf).unwrap(), 4);
  374. assert_eq!(reader.position(), 5);
  375. let b: &[_] = &[1, 2, 3, 4];
  376. assert_eq!(buf, b);
  377. assert_eq!(reader.read(&mut buf).unwrap(), 3);
  378. let b: &[_] = &[5, 6, 7];
  379. assert_eq!(&buf[..3], b);
  380. assert_eq!(reader.read(&mut buf).unwrap(), 0);
  381. }
  382. #[test]
  383. fn read_to_end() {
  384. let mut reader = Cursor::new(vec!(0, 1, 2, 3, 4, 5, 6, 7));
  385. let mut v = Vec::new();
  386. reader.read_to_end(&mut v).unwrap();
  387. assert_eq!(v, [0, 1, 2, 3, 4, 5, 6, 7]);
  388. }
  389. #[test]
  390. fn test_slice_reader() {
  391. let in_buf = vec![0, 1, 2, 3, 4, 5, 6, 7];
  392. let mut reader = &mut &in_buf[..];
  393. let mut buf = [];
  394. assert_eq!(reader.read(&mut buf).unwrap(), 0);
  395. let mut buf = [0];
  396. assert_eq!(reader.read(&mut buf).unwrap(), 1);
  397. assert_eq!(reader.len(), 7);
  398. let b: &[_] = &[0];
  399. assert_eq!(&buf[..], b);
  400. let mut buf = [0; 4];
  401. assert_eq!(reader.read(&mut buf).unwrap(), 4);
  402. assert_eq!(reader.len(), 3);
  403. let b: &[_] = &[1, 2, 3, 4];
  404. assert_eq!(&buf[..], b);
  405. assert_eq!(reader.read(&mut buf).unwrap(), 3);
  406. let b: &[_] = &[5, 6, 7];
  407. assert_eq!(&buf[..3], b);
  408. assert_eq!(reader.read(&mut buf).unwrap(), 0);
  409. }
  410. #[test]
  411. fn test_buf_reader() {
  412. let in_buf = vec![0, 1, 2, 3, 4, 5, 6, 7];
  413. let mut reader = Cursor::new(&in_buf[..]);
  414. let mut buf = [];
  415. assert_eq!(reader.read(&mut buf).unwrap(), 0);
  416. assert_eq!(reader.position(), 0);
  417. let mut buf = [0];
  418. assert_eq!(reader.read(&mut buf).unwrap(), 1);
  419. assert_eq!(reader.position(), 1);
  420. let b: &[_] = &[0];
  421. assert_eq!(buf, b);
  422. let mut buf = [0; 4];
  423. assert_eq!(reader.read(&mut buf).unwrap(), 4);
  424. assert_eq!(reader.position(), 5);
  425. let b: &[_] = &[1, 2, 3, 4];
  426. assert_eq!(buf, b);
  427. assert_eq!(reader.read(&mut buf).unwrap(), 3);
  428. let b: &[_] = &[5, 6, 7];
  429. assert_eq!(&buf[..3], b);
  430. assert_eq!(reader.read(&mut buf).unwrap(), 0);
  431. }
  432. #[test]
  433. fn test_read_char() {
  434. let b = &b"Vi\xE1\xBB\x87t"[..];
  435. let mut c = Cursor::new(b).chars();
  436. assert_eq!(c.next().unwrap().unwrap(), 'V');
  437. assert_eq!(c.next().unwrap().unwrap(), 'i');
  438. assert_eq!(c.next().unwrap().unwrap(), 'ệ');
  439. assert_eq!(c.next().unwrap().unwrap(), 't');
  440. assert!(c.next().is_none());
  441. }
  442. #[test]
  443. fn test_read_bad_char() {
  444. let b = &b"\x80"[..];
  445. let mut c = Cursor::new(b).chars();
  446. assert!(c.next().unwrap().is_err());
  447. }
  448. #[test]
  449. fn seek_past_end() {
  450. let buf = [0xff];
  451. let mut r = Cursor::new(&buf[..]);
  452. assert_eq!(r.seek(SeekFrom::Start(10)).unwrap(), 10);
  453. assert_eq!(r.read(&mut [0]).unwrap(), 0);
  454. let mut r = Cursor::new(vec!(10));
  455. assert_eq!(r.seek(SeekFrom::Start(10)).unwrap(), 10);
  456. assert_eq!(r.read(&mut [0]).unwrap(), 0);
  457. let mut buf = [0];
  458. let mut r = Cursor::new(&mut buf[..]);
  459. assert_eq!(r.seek(SeekFrom::Start(10)).unwrap(), 10);
  460. assert_eq!(r.write(&[3]).unwrap(), 0);
  461. let mut r = Cursor::new(vec![10].into_boxed_slice());
  462. assert_eq!(r.seek(SeekFrom::Start(10)).unwrap(), 10);
  463. assert_eq!(r.write(&[3]).unwrap(), 0);
  464. }
  465. #[test]
  466. fn seek_before_0() {
  467. let buf = [0xff];
  468. let mut r = Cursor::new(&buf[..]);
  469. assert!(r.seek(SeekFrom::End(-2)).is_err());
  470. let mut r = Cursor::new(vec!(10));
  471. assert!(r.seek(SeekFrom::End(-2)).is_err());
  472. let mut buf = [0];
  473. let mut r = Cursor::new(&mut buf[..]);
  474. assert!(r.seek(SeekFrom::End(-2)).is_err());
  475. let mut r = Cursor::new(vec!(10).into_boxed_slice());
  476. assert!(r.seek(SeekFrom::End(-2)).is_err());
  477. }
  478. #[test]
  479. fn test_seekable_mem_writer() {
  480. let mut writer = Cursor::new(Vec::<u8>::new());
  481. assert_eq!(writer.position(), 0);
  482. assert_eq!(writer.write(&[0]).unwrap(), 1);
  483. assert_eq!(writer.position(), 1);
  484. assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3);
  485. assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4);
  486. assert_eq!(writer.position(), 8);
  487. let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7];
  488. assert_eq!(&writer.get_ref()[..], b);
  489. assert_eq!(writer.seek(SeekFrom::Start(0)).unwrap(), 0);
  490. assert_eq!(writer.position(), 0);
  491. assert_eq!(writer.write(&[3, 4]).unwrap(), 2);
  492. let b: &[_] = &[3, 4, 2, 3, 4, 5, 6, 7];
  493. assert_eq!(&writer.get_ref()[..], b);
  494. assert_eq!(writer.seek(SeekFrom::Current(1)).unwrap(), 3);
  495. assert_eq!(writer.write(&[0, 1]).unwrap(), 2);
  496. let b: &[_] = &[3, 4, 2, 0, 1, 5, 6, 7];
  497. assert_eq!(&writer.get_ref()[..], b);
  498. assert_eq!(writer.seek(SeekFrom::End(-1)).unwrap(), 7);
  499. assert_eq!(writer.write(&[1, 2]).unwrap(), 2);
  500. let b: &[_] = &[3, 4, 2, 0, 1, 5, 6, 1, 2];
  501. assert_eq!(&writer.get_ref()[..], b);
  502. assert_eq!(writer.seek(SeekFrom::End(1)).unwrap(), 10);
  503. assert_eq!(writer.write(&[1]).unwrap(), 1);
  504. let b: &[_] = &[3, 4, 2, 0, 1, 5, 6, 1, 2, 0, 1];
  505. assert_eq!(&writer.get_ref()[..], b);
  506. }
  507. #[test]
  508. fn vec_seek_past_end() {
  509. let mut r = Cursor::new(Vec::new());
  510. assert_eq!(r.seek(SeekFrom::Start(10)).unwrap(), 10);
  511. assert_eq!(r.write(&[3]).unwrap(), 1);
  512. }
  513. #[test]
  514. fn vec_seek_before_0() {
  515. let mut r = Cursor::new(Vec::new());
  516. assert!(r.seek(SeekFrom::End(-2)).is_err());
  517. }
  518. }