2019-02-10 19:23:21 +00:00
|
|
|
use crate::io::prelude::*;
|
|
|
|
|
|
|
|
use crate::cmp;
|
2019-12-22 22:42:04 +00:00
|
|
|
use crate::io::{self, Error, ErrorKind, Initializer, IoSlice, IoSliceMut, SeekFrom};
|
2015-02-01 04:24:36 +00:00
|
|
|
|
2018-06-06 10:54:25 +00:00
|
|
|
use core::convert::TryInto;
|
2015-02-01 04:24:36 +00:00
|
|
|
|
2018-07-19 19:07:40 +00:00
|
|
|
/// A `Cursor` wraps an in-memory buffer and provides it with a
|
2016-10-14 13:34:51 +00:00
|
|
|
/// [`Seek`] implementation.
|
2015-02-01 04:24:36 +00:00
|
|
|
///
|
2018-07-19 19:07:40 +00:00
|
|
|
/// `Cursor`s are used with in-memory buffers, anything implementing
|
2020-08-18 17:36:52 +00:00
|
|
|
/// [`AsRef`]`<[u8]>`, to allow them to implement [`Read`] and/or [`Write`],
|
2018-07-19 19:07:40 +00:00
|
|
|
/// allowing these buffers to be used anywhere you might use a reader or writer
|
|
|
|
/// that does actual I/O.
|
2015-07-20 18:33:36 +00:00
|
|
|
///
|
|
|
|
/// The standard library implements some I/O traits on various types which
|
2016-10-14 13:34:51 +00:00
|
|
|
/// are commonly used as a buffer, like `Cursor<`[`Vec`]`<u8>>` and
|
2016-10-20 23:49:47 +00:00
|
|
|
/// `Cursor<`[`&[u8]`][bytes]`>`.
|
2015-07-20 18:33:36 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2016-10-14 13:34:51 +00:00
|
|
|
/// We may want to write bytes to a [`File`] in our production
|
2015-07-20 18:33:36 +00:00
|
|
|
/// code, but use an in-memory buffer in our tests. We can do this with
|
|
|
|
/// `Cursor`:
|
|
|
|
///
|
2020-08-18 17:36:52 +00:00
|
|
|
/// [bytes]: crate::slice
|
|
|
|
/// [`File`]: crate::fs::File
|
2015-07-20 18:33:36 +00:00
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::io::{self, SeekFrom};
|
|
|
|
/// use std::fs::File;
|
|
|
|
///
|
|
|
|
/// // a library function we've written
|
|
|
|
/// fn write_ten_bytes_at_end<W: Write + Seek>(writer: &mut W) -> io::Result<()> {
|
2016-12-28 09:02:35 +00:00
|
|
|
/// writer.seek(SeekFrom::End(-10))?;
|
2015-07-20 18:33:36 +00:00
|
|
|
///
|
|
|
|
/// for i in 0..10 {
|
2016-12-28 09:02:35 +00:00
|
|
|
/// writer.write(&[i])?;
|
2015-07-20 18:33:36 +00:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // all went well
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// # fn foo() -> io::Result<()> {
|
|
|
|
/// // Here's some code that uses this library function.
|
|
|
|
/// //
|
|
|
|
/// // We might want to use a BufReader here for efficiency, but let's
|
|
|
|
/// // keep this example focused.
|
2016-12-28 09:02:35 +00:00
|
|
|
/// let mut file = File::create("foo.txt")?;
|
2015-07-20 18:33:36 +00:00
|
|
|
///
|
2016-12-28 09:02:35 +00:00
|
|
|
/// write_ten_bytes_at_end(&mut file)?;
|
2015-07-20 18:33:36 +00:00
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
///
|
|
|
|
/// // now let's write a test
|
|
|
|
/// #[test]
|
|
|
|
/// fn test_writes_bytes() {
|
2017-08-16 22:46:30 +00:00
|
|
|
/// // setting up a real File is much slower than an in-memory buffer,
|
2015-07-20 18:33:36 +00:00
|
|
|
/// // let's use a cursor instead
|
|
|
|
/// use std::io::Cursor;
|
|
|
|
/// let mut buff = Cursor::new(vec![0; 15]);
|
|
|
|
///
|
2015-10-22 02:33:24 +00:00
|
|
|
/// write_ten_bytes_at_end(&mut buff).unwrap();
|
2015-07-20 18:33:36 +00:00
|
|
|
///
|
|
|
|
/// assert_eq!(&buff.get_ref()[5..15], &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
|
|
|
/// }
|
|
|
|
/// ```
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-12-11 16:59:32 +00:00
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
2015-02-01 04:24:36 +00:00
|
|
|
pub struct Cursor<T> {
|
|
|
|
inner: T,
|
|
|
|
pos: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Cursor<T> {
|
2018-07-19 19:07:40 +00:00
|
|
|
/// Creates a new cursor wrapping the provided underlying in-memory buffer.
|
2015-07-20 18:33:36 +00:00
|
|
|
///
|
2020-08-18 17:36:52 +00:00
|
|
|
/// Cursor initial position is `0` even if underlying buffer (e.g., [`Vec`])
|
|
|
|
/// is not empty. So writing to cursor starts with overwriting [`Vec`]
|
2018-07-19 19:07:40 +00:00
|
|
|
/// content, not with appending to it.
|
Document Cursor::new position is 0
... even if contained `Vec` is not empty. E. g. for
```
let v = vec![10u8, 20];
let mut c = io::Cursor::new(v);
c.write_all(b"aaaa").unwrap();
println!("{:?}", c.into_inner());
```
result is
```
[97, 97, 97, 97]
```
and not
```
[10, 20, 97, 97, 97, 97]
```
2017-03-24 02:05:34 +00:00
|
|
|
///
|
2015-07-20 18:33:36 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io::Cursor;
|
|
|
|
///
|
|
|
|
/// let buff = Cursor::new(Vec::new());
|
|
|
|
/// # fn force_inference(_: &Cursor<Vec<u8>>) {}
|
|
|
|
/// # force_inference(&buff);
|
|
|
|
/// ```
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-01 04:24:36 +00:00
|
|
|
pub fn new(inner: T) -> Cursor<T> {
|
2020-03-06 18:28:44 +00:00
|
|
|
Cursor { pos: 0, inner }
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
|
|
|
|
2015-04-13 14:21:32 +00:00
|
|
|
/// Consumes this cursor, returning the underlying value.
|
2015-07-20 18:33:36 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io::Cursor;
|
|
|
|
///
|
|
|
|
/// let buff = Cursor::new(Vec::new());
|
|
|
|
/// # fn force_inference(_: &Cursor<Vec<u8>>) {}
|
|
|
|
/// # force_inference(&buff);
|
|
|
|
///
|
|
|
|
/// let vec = buff.into_inner();
|
|
|
|
/// ```
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-12-22 22:42:04 +00:00
|
|
|
pub fn into_inner(self) -> T {
|
|
|
|
self.inner
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
|
2015-04-13 14:21:32 +00:00
|
|
|
/// Gets a reference to the underlying value in this cursor.
|
2015-07-20 18:33:36 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io::Cursor;
|
|
|
|
///
|
|
|
|
/// let buff = Cursor::new(Vec::new());
|
|
|
|
/// # fn force_inference(_: &Cursor<Vec<u8>>) {}
|
|
|
|
/// # force_inference(&buff);
|
|
|
|
///
|
|
|
|
/// let reference = buff.get_ref();
|
|
|
|
/// ```
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-12-22 22:42:04 +00:00
|
|
|
pub fn get_ref(&self) -> &T {
|
|
|
|
&self.inner
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
|
2015-04-13 14:21:32 +00:00
|
|
|
/// Gets a mutable reference to the underlying value in this cursor.
|
2015-02-01 04:24:36 +00:00
|
|
|
///
|
|
|
|
/// Care should be taken to avoid modifying the internal I/O state of the
|
|
|
|
/// underlying value as it may corrupt this cursor's position.
|
2015-07-20 18:33:36 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io::Cursor;
|
|
|
|
///
|
|
|
|
/// let mut buff = Cursor::new(Vec::new());
|
|
|
|
/// # fn force_inference(_: &Cursor<Vec<u8>>) {}
|
|
|
|
/// # force_inference(&buff);
|
|
|
|
///
|
|
|
|
/// let reference = buff.get_mut();
|
|
|
|
/// ```
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-12-22 22:42:04 +00:00
|
|
|
pub fn get_mut(&mut self) -> &mut T {
|
|
|
|
&mut self.inner
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
|
2015-07-20 18:33:36 +00:00
|
|
|
/// Returns the current position of this cursor.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io::Cursor;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
/// use std::io::SeekFrom;
|
|
|
|
///
|
|
|
|
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
|
|
|
|
///
|
|
|
|
/// assert_eq!(buff.position(), 0);
|
|
|
|
///
|
|
|
|
/// buff.seek(SeekFrom::Current(2)).unwrap();
|
|
|
|
/// assert_eq!(buff.position(), 2);
|
|
|
|
///
|
|
|
|
/// buff.seek(SeekFrom::Current(-1)).unwrap();
|
|
|
|
/// assert_eq!(buff.position(), 1);
|
|
|
|
/// ```
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-12-22 22:42:04 +00:00
|
|
|
pub fn position(&self) -> u64 {
|
|
|
|
self.pos
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
|
2015-07-20 18:33:36 +00:00
|
|
|
/// Sets the position of this cursor.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::io::Cursor;
|
|
|
|
///
|
|
|
|
/// let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
|
|
|
|
///
|
|
|
|
/// assert_eq!(buff.position(), 0);
|
|
|
|
///
|
|
|
|
/// buff.set_position(2);
|
|
|
|
/// assert_eq!(buff.position(), 2);
|
|
|
|
///
|
|
|
|
/// buff.set_position(4);
|
|
|
|
/// assert_eq!(buff.position(), 4);
|
|
|
|
/// ```
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-12-22 22:42:04 +00:00
|
|
|
pub fn set_position(&mut self, pos: u64) {
|
|
|
|
self.pos = pos;
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
|
|
|
|
2015-03-14 12:39:39 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-12-22 22:42:04 +00:00
|
|
|
impl<T> io::Seek for Cursor<T>
|
|
|
|
where
|
|
|
|
T: AsRef<[u8]>,
|
|
|
|
{
|
2015-03-14 12:39:39 +00:00
|
|
|
fn seek(&mut self, style: SeekFrom) -> io::Result<u64> {
|
2017-02-16 01:21:08 +00:00
|
|
|
let (base_pos, offset) = match style {
|
2019-12-22 22:42:04 +00:00
|
|
|
SeekFrom::Start(n) => {
|
|
|
|
self.pos = n;
|
|
|
|
return Ok(n);
|
|
|
|
}
|
2017-02-16 01:21:08 +00:00
|
|
|
SeekFrom::End(n) => (self.inner.as_ref().len() as u64, n),
|
|
|
|
SeekFrom::Current(n) => (self.pos, n),
|
2015-03-14 12:39:39 +00:00
|
|
|
};
|
2017-02-16 01:21:08 +00:00
|
|
|
let new_pos = if offset >= 0 {
|
|
|
|
base_pos.checked_add(offset as u64)
|
2015-03-14 12:39:39 +00:00
|
|
|
} else {
|
2017-02-16 01:21:08 +00:00
|
|
|
base_pos.checked_sub((offset.wrapping_neg()) as u64)
|
|
|
|
};
|
|
|
|
match new_pos {
|
2019-12-22 22:42:04 +00:00
|
|
|
Some(n) => {
|
|
|
|
self.pos = n;
|
|
|
|
Ok(self.pos)
|
|
|
|
}
|
|
|
|
None => Err(Error::new(
|
|
|
|
ErrorKind::InvalidInput,
|
|
|
|
"invalid seek to a negative or overflowing position",
|
|
|
|
)),
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-14 12:27:49 +00:00
|
|
|
|
|
|
|
fn stream_len(&mut self) -> io::Result<u64> {
|
|
|
|
Ok(self.inner.as_ref().len() as u64)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stream_position(&mut self) -> io::Result<u64> {
|
|
|
|
Ok(self.pos)
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
|
|
|
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-12-22 22:42:04 +00:00
|
|
|
impl<T> Read for Cursor<T>
|
|
|
|
where
|
|
|
|
T: AsRef<[u8]>,
|
|
|
|
{
|
2015-03-14 12:39:39 +00:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
2016-03-23 03:01:37 +00:00
|
|
|
let n = Read::read(&mut self.fill_buf()?, buf)?;
|
2015-03-14 12:39:39 +00:00
|
|
|
self.pos += n as u64;
|
|
|
|
Ok(n)
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
2017-05-15 01:29:18 +00:00
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
2019-02-08 19:42:34 +00:00
|
|
|
let mut nread = 0;
|
|
|
|
for buf in bufs {
|
|
|
|
let n = self.read(buf)?;
|
|
|
|
nread += n;
|
|
|
|
if n < buf.len() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(nread)
|
|
|
|
}
|
|
|
|
|
2020-03-12 01:02:52 +00:00
|
|
|
fn is_read_vectored(&self) -> bool {
|
2020-01-03 19:26:05 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2017-12-04 04:45:12 +00:00
|
|
|
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(())
|
|
|
|
}
|
|
|
|
|
2017-05-15 01:29:18 +00:00
|
|
|
#[inline]
|
|
|
|
unsafe fn initializer(&self) -> Initializer {
|
|
|
|
Initializer::nop()
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
|
|
|
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-12-22 22:42:04 +00:00
|
|
|
impl<T> BufRead for Cursor<T>
|
|
|
|
where
|
|
|
|
T: AsRef<[u8]>,
|
|
|
|
{
|
2015-03-14 12:39:39 +00:00
|
|
|
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)..])
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
fn consume(&mut self, amt: usize) {
|
|
|
|
self.pos += amt as u64;
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
|
|
|
|
2017-12-18 21:11:44 +00:00
|
|
|
// Non-resizing write implementation
|
2019-05-09 02:38:58 +00:00
|
|
|
#[inline]
|
2017-12-18 21:11:44 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-05-09 02:38:58 +00:00
|
|
|
#[inline]
|
2019-02-08 19:42:34 +00:00
|
|
|
fn slice_write_vectored(
|
|
|
|
pos_mut: &mut u64,
|
|
|
|
slice: &mut [u8],
|
2019-04-27 15:34:08 +00:00
|
|
|
bufs: &[IoSlice<'_>],
|
2019-12-22 22:42:04 +00:00
|
|
|
) -> io::Result<usize> {
|
2019-02-08 19:42:34 +00:00
|
|
|
let mut nwritten = 0;
|
|
|
|
for buf in bufs {
|
|
|
|
let n = slice_write(pos_mut, slice, buf)?;
|
|
|
|
nwritten += n;
|
|
|
|
if n < buf.len() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(nwritten)
|
|
|
|
}
|
|
|
|
|
2017-12-18 21:11:44 +00:00
|
|
|
// Resizing write implementation
|
|
|
|
fn vec_write(pos_mut: &mut u64, vec: &mut Vec<u8>, buf: &[u8]) -> io::Result<usize> {
|
2018-06-06 10:54:25 +00:00
|
|
|
let pos: usize = (*pos_mut).try_into().map_err(|_| {
|
2019-12-22 22:42:04 +00:00
|
|
|
Error::new(
|
|
|
|
ErrorKind::InvalidInput,
|
|
|
|
"cursor position exceeds maximum possible vector length",
|
|
|
|
)
|
2017-12-18 21:11:44 +00:00
|
|
|
})?;
|
|
|
|
// 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())
|
|
|
|
}
|
|
|
|
|
2019-02-08 19:42:34 +00:00
|
|
|
fn vec_write_vectored(
|
|
|
|
pos_mut: &mut u64,
|
|
|
|
vec: &mut Vec<u8>,
|
2019-04-27 15:34:08 +00:00
|
|
|
bufs: &[IoSlice<'_>],
|
2019-12-22 22:42:04 +00:00
|
|
|
) -> io::Result<usize> {
|
2019-02-08 19:42:34 +00:00
|
|
|
let mut nwritten = 0;
|
|
|
|
for buf in bufs {
|
2019-02-12 03:31:37 +00:00
|
|
|
nwritten += vec_write(pos_mut, vec, buf)?;
|
2019-02-08 19:42:34 +00:00
|
|
|
}
|
|
|
|
Ok(nwritten)
|
|
|
|
}
|
|
|
|
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-02-18 03:42:36 +00:00
|
|
|
impl Write for Cursor<&mut [u8]> {
|
2016-05-28 02:34:20 +00:00
|
|
|
#[inline]
|
2017-12-18 21:11:44 +00:00
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
slice_write(&mut self.pos, self.inner, buf)
|
|
|
|
}
|
2019-02-08 19:42:34 +00:00
|
|
|
|
|
|
|
#[inline]
|
2019-04-27 15:34:08 +00:00
|
|
|
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
2019-02-08 19:42:34 +00:00
|
|
|
slice_write_vectored(&mut self.pos, self.inner, bufs)
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
fn is_write_vectored(&self) -> bool {
|
2020-01-03 19:26:05 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2019-05-09 02:38:58 +00:00
|
|
|
#[inline]
|
2019-12-22 22:42:04 +00:00
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-12-18 21:11:44 +00:00
|
|
|
}
|
|
|
|
|
2018-02-10 21:20:42 +00:00
|
|
|
#[stable(feature = "cursor_mut_vec", since = "1.25.0")]
|
2019-02-18 03:42:36 +00:00
|
|
|
impl Write for Cursor<&mut Vec<u8>> {
|
2017-12-18 21:11:44 +00:00
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
vec_write(&mut self.pos, self.inner, buf)
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
2019-02-08 19:42:34 +00:00
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
2019-02-08 19:42:34 +00:00
|
|
|
vec_write_vectored(&mut self.pos, self.inner, bufs)
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
fn is_write_vectored(&self) -> bool {
|
2020-01-03 19:26:05 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2019-05-09 02:38:58 +00:00
|
|
|
#[inline]
|
2019-12-22 22:42:04 +00:00
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
|
|
|
|
std: Stabilize portions of the `io` module
The new `io` module has had some time to bake and this commit stabilizes some of
the utilities associated with it. This commit also deprecates a number of
`std::old_io::util` functions and structures.
These items are now `#[stable]`
* `Cursor`
* `Cursor::{new, into_inner, get_ref, get_mut, position, set_position}`
* Implementations of I/O traits for `Cursor<T>`
* Delegating implementations of I/O traits for references and `Box` pointers
* Implementations of I/O traits for primitives like slices and `Vec<T>`
* `ReadExt::bytes`
* `Bytes` (and impls)
* `ReadExt::chain`
* `Chain` (and impls)
* `ReadExt::take` (and impls)
* `BufReadExt::lines`
* `Lines` (and impls)
* `io::copy`
* `io::{empty, Empty}` (and impls)
* `io::{sink, Sink}` (and impls)
* `io::{repeat, Repeat}` (and impls)
These items remain `#[unstable]`
* Core I/O traits. These may want a little bit more time to bake along with the
commonly used methods like `read_to_end`.
* `BufReadExt::split` - this function may be renamed to not conflict with
`SliceExt::split`.
* `Error` - there are a number of questions about its representation,
`ErrorKind`, and usability.
These items are now `#[deprecated]` in `old_io`
* `LimitReader` - use `take` instead
* `NullWriter` - use `io::sink` instead
* `ZeroReader` - use `io::repeat` instead
* `NullReader` - use `io::empty` instead
* `MultiWriter` - use `broadcast` instead
* `ChainedReader` - use `chain` instead
* `TeeReader` - use `tee` instead
* `copy` - use `io::copy` instead
[breaking-change]
2015-03-03 22:49:03 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-01 04:24:36 +00:00
|
|
|
impl Write for Cursor<Vec<u8>> {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
2017-12-18 21:11:44 +00:00
|
|
|
vec_write(&mut self.pos, &mut self.inner, buf)
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
2019-02-08 19:42:34 +00:00
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
2019-02-08 19:42:34 +00:00
|
|
|
vec_write_vectored(&mut self.pos, &mut self.inner, bufs)
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
fn is_write_vectored(&self) -> bool {
|
2020-01-03 19:26:05 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2019-05-09 02:38:58 +00:00
|
|
|
#[inline]
|
2019-12-22 22:42:04 +00:00
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
|
|
|
|
2015-08-19 06:02:49 +00:00
|
|
|
#[stable(feature = "cursor_box_slice", since = "1.5.0")]
|
|
|
|
impl Write for Cursor<Box<[u8]>> {
|
2016-05-28 02:34:20 +00:00
|
|
|
#[inline]
|
2015-08-19 06:02:49 +00:00
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
2017-12-18 21:11:44 +00:00
|
|
|
slice_write(&mut self.pos, &mut self.inner, buf)
|
2015-08-19 06:02:49 +00:00
|
|
|
}
|
2019-02-08 19:42:34 +00:00
|
|
|
|
|
|
|
#[inline]
|
2019-04-27 15:34:08 +00:00
|
|
|
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
|
2019-02-08 19:42:34 +00:00
|
|
|
slice_write_vectored(&mut self.pos, &mut self.inner, bufs)
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:26:05 +00:00
|
|
|
#[inline]
|
2020-03-12 01:02:52 +00:00
|
|
|
fn is_write_vectored(&self) -> bool {
|
2020-01-03 19:26:05 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2019-05-09 02:38:58 +00:00
|
|
|
#[inline]
|
2019-12-22 22:42:04 +00:00
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2015-08-19 06:02:49 +00:00
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::io::prelude::*;
|
2019-12-22 22:42:04 +00:00
|
|
|
use crate::io::{Cursor, IoSlice, IoSliceMut, SeekFrom};
|
2015-02-01 04:24:36 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_vec_writer() {
|
|
|
|
let mut writer = Vec::new();
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(writer.write(&[0]).unwrap(), 1);
|
|
|
|
assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3);
|
|
|
|
assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4);
|
2019-12-22 22:42:04 +00:00
|
|
|
assert_eq!(
|
|
|
|
writer
|
|
|
|
.write_vectored(&[IoSlice::new(&[]), IoSlice::new(&[8, 9]), IoSlice::new(&[10])],)
|
|
|
|
.unwrap(),
|
|
|
|
3
|
|
|
|
);
|
2019-02-08 19:42:34 +00:00
|
|
|
let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(writer, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mem_writer() {
|
|
|
|
let mut writer = Cursor::new(Vec::new());
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(writer.write(&[0]).unwrap(), 1);
|
|
|
|
assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3);
|
|
|
|
assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4);
|
2019-12-22 22:42:04 +00:00
|
|
|
assert_eq!(
|
|
|
|
writer
|
|
|
|
.write_vectored(&[IoSlice::new(&[]), IoSlice::new(&[8, 9]), IoSlice::new(&[10])],)
|
|
|
|
.unwrap(),
|
|
|
|
3
|
|
|
|
);
|
2019-02-08 19:42:34 +00:00
|
|
|
let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
2015-02-25 06:20:34 +00:00
|
|
|
assert_eq!(&writer.get_ref()[..], b);
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
|
|
|
|
2017-12-18 21:11:44 +00:00
|
|
|
#[test]
|
|
|
|
fn test_mem_mut_writer() {
|
|
|
|
let mut vec = Vec::new();
|
|
|
|
let mut writer = Cursor::new(&mut vec);
|
|
|
|
assert_eq!(writer.write(&[0]).unwrap(), 1);
|
|
|
|
assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3);
|
|
|
|
assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4);
|
2019-12-22 22:42:04 +00:00
|
|
|
assert_eq!(
|
|
|
|
writer
|
|
|
|
.write_vectored(&[IoSlice::new(&[]), IoSlice::new(&[8, 9]), IoSlice::new(&[10])],)
|
|
|
|
.unwrap(),
|
|
|
|
3
|
|
|
|
);
|
2019-02-08 19:42:34 +00:00
|
|
|
let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
2017-12-18 21:11:44 +00:00
|
|
|
assert_eq!(&writer.get_ref()[..], b);
|
|
|
|
}
|
|
|
|
|
2015-08-19 06:02:49 +00:00
|
|
|
#[test]
|
|
|
|
fn test_box_slice_writer() {
|
|
|
|
let mut writer = Cursor::new(vec![0u8; 9].into_boxed_slice());
|
|
|
|
assert_eq!(writer.position(), 0);
|
|
|
|
assert_eq!(writer.write(&[0]).unwrap(), 1);
|
|
|
|
assert_eq!(writer.position(), 1);
|
|
|
|
assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3);
|
|
|
|
assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4);
|
|
|
|
assert_eq!(writer.position(), 8);
|
|
|
|
assert_eq!(writer.write(&[]).unwrap(), 0);
|
|
|
|
assert_eq!(writer.position(), 8);
|
|
|
|
|
|
|
|
assert_eq!(writer.write(&[8, 9]).unwrap(), 1);
|
|
|
|
assert_eq!(writer.write(&[10]).unwrap(), 0);
|
|
|
|
let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8];
|
|
|
|
assert_eq!(&**writer.get_ref(), b);
|
|
|
|
}
|
|
|
|
|
2019-02-08 19:42:34 +00:00
|
|
|
#[test]
|
|
|
|
fn test_box_slice_writer_vectored() {
|
|
|
|
let mut writer = Cursor::new(vec![0u8; 9].into_boxed_slice());
|
|
|
|
assert_eq!(writer.position(), 0);
|
2019-04-27 15:34:08 +00:00
|
|
|
assert_eq!(writer.write_vectored(&[IoSlice::new(&[0])]).unwrap(), 1);
|
2019-02-08 19:42:34 +00:00
|
|
|
assert_eq!(writer.position(), 1);
|
|
|
|
assert_eq!(
|
2019-12-22 22:42:04 +00:00
|
|
|
writer
|
|
|
|
.write_vectored(&[IoSlice::new(&[1, 2, 3]), IoSlice::new(&[4, 5, 6, 7]),])
|
|
|
|
.unwrap(),
|
2019-02-08 19:42:34 +00:00
|
|
|
7,
|
|
|
|
);
|
|
|
|
assert_eq!(writer.position(), 8);
|
|
|
|
assert_eq!(writer.write_vectored(&[]).unwrap(), 0);
|
|
|
|
assert_eq!(writer.position(), 8);
|
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
assert_eq!(writer.write_vectored(&[IoSlice::new(&[8, 9])]).unwrap(), 1);
|
|
|
|
assert_eq!(writer.write_vectored(&[IoSlice::new(&[10])]).unwrap(), 0);
|
2019-02-08 19:42:34 +00:00
|
|
|
let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8];
|
|
|
|
assert_eq!(&**writer.get_ref(), b);
|
|
|
|
}
|
|
|
|
|
2015-02-01 04:24:36 +00:00
|
|
|
#[test]
|
|
|
|
fn test_buf_writer() {
|
|
|
|
let mut buf = [0 as u8; 9];
|
|
|
|
{
|
2015-02-18 19:48:57 +00:00
|
|
|
let mut writer = Cursor::new(&mut buf[..]);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(writer.position(), 0);
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(writer.write(&[0]).unwrap(), 1);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(writer.position(), 1);
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3);
|
|
|
|
assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(writer.position(), 8);
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(writer.write(&[]).unwrap(), 0);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(writer.position(), 8);
|
|
|
|
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(writer.write(&[8, 9]).unwrap(), 1);
|
|
|
|
assert_eq!(writer.write(&[10]).unwrap(), 0);
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
|
|
|
let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8];
|
|
|
|
assert_eq!(buf, b);
|
|
|
|
}
|
|
|
|
|
2019-02-08 19:42:34 +00:00
|
|
|
#[test]
|
|
|
|
fn test_buf_writer_vectored() {
|
|
|
|
let mut buf = [0 as u8; 9];
|
|
|
|
{
|
|
|
|
let mut writer = Cursor::new(&mut buf[..]);
|
|
|
|
assert_eq!(writer.position(), 0);
|
2019-04-27 15:34:08 +00:00
|
|
|
assert_eq!(writer.write_vectored(&[IoSlice::new(&[0])]).unwrap(), 1);
|
2019-02-08 19:42:34 +00:00
|
|
|
assert_eq!(writer.position(), 1);
|
|
|
|
assert_eq!(
|
2019-12-22 22:42:04 +00:00
|
|
|
writer
|
|
|
|
.write_vectored(&[IoSlice::new(&[1, 2, 3]), IoSlice::new(&[4, 5, 6, 7])],)
|
|
|
|
.unwrap(),
|
2019-02-08 19:42:34 +00:00
|
|
|
7,
|
|
|
|
);
|
|
|
|
assert_eq!(writer.position(), 8);
|
|
|
|
assert_eq!(writer.write_vectored(&[]).unwrap(), 0);
|
|
|
|
assert_eq!(writer.position(), 8);
|
|
|
|
|
2019-04-27 15:34:08 +00:00
|
|
|
assert_eq!(writer.write_vectored(&[IoSlice::new(&[8, 9])]).unwrap(), 1);
|
|
|
|
assert_eq!(writer.write_vectored(&[IoSlice::new(&[10])]).unwrap(), 0);
|
2019-02-08 19:42:34 +00:00
|
|
|
}
|
|
|
|
let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8];
|
|
|
|
assert_eq!(buf, b);
|
|
|
|
}
|
|
|
|
|
2015-02-01 04:24:36 +00:00
|
|
|
#[test]
|
|
|
|
fn test_buf_writer_seek() {
|
|
|
|
let mut buf = [0 as u8; 8];
|
|
|
|
{
|
2015-02-18 19:48:57 +00:00
|
|
|
let mut writer = Cursor::new(&mut buf[..]);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(writer.position(), 0);
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(writer.write(&[1]).unwrap(), 1);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(writer.position(), 1);
|
|
|
|
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(writer.seek(SeekFrom::Start(2)).unwrap(), 2);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(writer.position(), 2);
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(writer.write(&[2]).unwrap(), 1);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(writer.position(), 3);
|
|
|
|
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(writer.seek(SeekFrom::Current(-2)).unwrap(), 1);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(writer.position(), 1);
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(writer.write(&[3]).unwrap(), 1);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(writer.position(), 2);
|
|
|
|
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(writer.seek(SeekFrom::End(-1)).unwrap(), 7);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(writer.position(), 7);
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(writer.write(&[4]).unwrap(), 1);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(writer.position(), 8);
|
|
|
|
}
|
|
|
|
let b: &[_] = &[1, 3, 2, 0, 0, 0, 0, 4];
|
|
|
|
assert_eq!(buf, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_buf_writer_error() {
|
|
|
|
let mut buf = [0 as u8; 2];
|
2015-02-18 19:48:57 +00:00
|
|
|
let mut writer = Cursor::new(&mut buf[..]);
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(writer.write(&[0]).unwrap(), 1);
|
|
|
|
assert_eq!(writer.write(&[0, 0]).unwrap(), 1);
|
|
|
|
assert_eq!(writer.write(&[0, 0]).unwrap(), 0);
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mem_reader() {
|
2016-10-29 21:54:04 +00:00
|
|
|
let mut reader = Cursor::new(vec![0, 1, 2, 3, 4, 5, 6, 7]);
|
2015-02-01 04:24:36 +00:00
|
|
|
let mut buf = [];
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 0);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(reader.position(), 0);
|
|
|
|
let mut buf = [0];
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 1);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(reader.position(), 1);
|
|
|
|
let b: &[_] = &[0];
|
|
|
|
assert_eq!(buf, b);
|
|
|
|
let mut buf = [0; 4];
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 4);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(reader.position(), 5);
|
|
|
|
let b: &[_] = &[1, 2, 3, 4];
|
|
|
|
assert_eq!(buf, b);
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 3);
|
2015-02-01 04:24:36 +00:00
|
|
|
let b: &[_] = &[5, 6, 7];
|
|
|
|
assert_eq!(&buf[..3], b);
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 0);
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 19:42:34 +00:00
|
|
|
#[test]
|
|
|
|
fn test_mem_reader_vectored() {
|
|
|
|
let mut reader = Cursor::new(vec![0, 1, 2, 3, 4, 5, 6, 7]);
|
|
|
|
let mut buf = [];
|
2019-04-27 15:34:08 +00:00
|
|
|
assert_eq!(reader.read_vectored(&mut [IoSliceMut::new(&mut buf)]).unwrap(), 0);
|
2019-02-08 19:42:34 +00:00
|
|
|
assert_eq!(reader.position(), 0);
|
|
|
|
let mut buf = [0];
|
|
|
|
assert_eq!(
|
2019-12-22 22:42:04 +00:00
|
|
|
reader
|
|
|
|
.read_vectored(&mut [IoSliceMut::new(&mut []), IoSliceMut::new(&mut buf),])
|
|
|
|
.unwrap(),
|
2019-02-08 19:42:34 +00:00
|
|
|
1,
|
|
|
|
);
|
|
|
|
assert_eq!(reader.position(), 1);
|
|
|
|
let b: &[_] = &[0];
|
|
|
|
assert_eq!(buf, b);
|
|
|
|
let mut buf1 = [0; 4];
|
|
|
|
let mut buf2 = [0; 4];
|
|
|
|
assert_eq!(
|
2019-12-22 22:42:04 +00:00
|
|
|
reader
|
|
|
|
.read_vectored(&mut [IoSliceMut::new(&mut buf1), IoSliceMut::new(&mut buf2),])
|
|
|
|
.unwrap(),
|
2019-02-08 19:42:34 +00:00
|
|
|
7,
|
|
|
|
);
|
|
|
|
let b1: &[_] = &[1, 2, 3, 4];
|
|
|
|
let b2: &[_] = &[5, 6, 7];
|
|
|
|
assert_eq!(buf1, b1);
|
|
|
|
assert_eq!(&buf2[..3], b2);
|
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 0);
|
|
|
|
}
|
|
|
|
|
2015-08-19 06:02:49 +00:00
|
|
|
#[test]
|
|
|
|
fn test_boxed_slice_reader() {
|
2016-10-29 21:54:04 +00:00
|
|
|
let mut reader = Cursor::new(vec![0, 1, 2, 3, 4, 5, 6, 7].into_boxed_slice());
|
2015-08-19 06:02:49 +00:00
|
|
|
let mut buf = [];
|
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 0);
|
|
|
|
assert_eq!(reader.position(), 0);
|
|
|
|
let mut buf = [0];
|
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 1);
|
|
|
|
assert_eq!(reader.position(), 1);
|
|
|
|
let b: &[_] = &[0];
|
|
|
|
assert_eq!(buf, b);
|
|
|
|
let mut buf = [0; 4];
|
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 4);
|
|
|
|
assert_eq!(reader.position(), 5);
|
|
|
|
let b: &[_] = &[1, 2, 3, 4];
|
|
|
|
assert_eq!(buf, b);
|
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 3);
|
|
|
|
let b: &[_] = &[5, 6, 7];
|
|
|
|
assert_eq!(&buf[..3], b);
|
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 0);
|
|
|
|
}
|
|
|
|
|
2019-02-08 19:42:34 +00:00
|
|
|
#[test]
|
|
|
|
fn test_boxed_slice_reader_vectored() {
|
|
|
|
let mut reader = Cursor::new(vec![0, 1, 2, 3, 4, 5, 6, 7].into_boxed_slice());
|
|
|
|
let mut buf = [];
|
2019-04-27 15:34:08 +00:00
|
|
|
assert_eq!(reader.read_vectored(&mut [IoSliceMut::new(&mut buf)]).unwrap(), 0);
|
2019-02-08 19:42:34 +00:00
|
|
|
assert_eq!(reader.position(), 0);
|
|
|
|
let mut buf = [0];
|
|
|
|
assert_eq!(
|
2019-12-22 22:42:04 +00:00
|
|
|
reader
|
|
|
|
.read_vectored(&mut [IoSliceMut::new(&mut []), IoSliceMut::new(&mut buf),])
|
|
|
|
.unwrap(),
|
2019-02-08 19:42:34 +00:00
|
|
|
1,
|
|
|
|
);
|
|
|
|
assert_eq!(reader.position(), 1);
|
|
|
|
let b: &[_] = &[0];
|
|
|
|
assert_eq!(buf, b);
|
|
|
|
let mut buf1 = [0; 4];
|
|
|
|
let mut buf2 = [0; 4];
|
|
|
|
assert_eq!(
|
2019-12-22 22:42:04 +00:00
|
|
|
reader
|
|
|
|
.read_vectored(&mut [IoSliceMut::new(&mut buf1), IoSliceMut::new(&mut buf2)],)
|
|
|
|
.unwrap(),
|
2019-02-08 19:42:34 +00:00
|
|
|
7,
|
|
|
|
);
|
|
|
|
let b1: &[_] = &[1, 2, 3, 4];
|
|
|
|
let b2: &[_] = &[5, 6, 7];
|
|
|
|
assert_eq!(buf1, b1);
|
|
|
|
assert_eq!(&buf2[..3], b2);
|
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 0);
|
|
|
|
}
|
|
|
|
|
2015-02-01 04:24:36 +00:00
|
|
|
#[test]
|
|
|
|
fn read_to_end() {
|
2016-10-29 21:54:04 +00:00
|
|
|
let mut reader = Cursor::new(vec![0, 1, 2, 3, 4, 5, 6, 7]);
|
2015-02-01 04:24:36 +00:00
|
|
|
let mut v = Vec::new();
|
2015-03-20 07:19:13 +00:00
|
|
|
reader.read_to_end(&mut v).unwrap();
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(v, [0, 1, 2, 3, 4, 5, 6, 7]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_slice_reader() {
|
2015-03-03 08:42:26 +00:00
|
|
|
let in_buf = vec![0, 1, 2, 3, 4, 5, 6, 7];
|
2017-08-02 14:16:20 +00:00
|
|
|
let reader = &mut &in_buf[..];
|
2015-02-01 04:24:36 +00:00
|
|
|
let mut buf = [];
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 0);
|
2015-02-01 04:24:36 +00:00
|
|
|
let mut buf = [0];
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 1);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(reader.len(), 7);
|
|
|
|
let b: &[_] = &[0];
|
2015-03-30 18:00:05 +00:00
|
|
|
assert_eq!(&buf[..], b);
|
2015-02-01 04:24:36 +00:00
|
|
|
let mut buf = [0; 4];
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 4);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(reader.len(), 3);
|
|
|
|
let b: &[_] = &[1, 2, 3, 4];
|
2015-03-30 18:00:05 +00:00
|
|
|
assert_eq!(&buf[..], b);
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 3);
|
2015-02-01 04:24:36 +00:00
|
|
|
let b: &[_] = &[5, 6, 7];
|
|
|
|
assert_eq!(&buf[..3], b);
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 0);
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 19:42:34 +00:00
|
|
|
#[test]
|
|
|
|
fn test_slice_reader_vectored() {
|
|
|
|
let in_buf = vec![0, 1, 2, 3, 4, 5, 6, 7];
|
|
|
|
let reader = &mut &in_buf[..];
|
|
|
|
let mut buf = [];
|
2019-04-27 15:34:08 +00:00
|
|
|
assert_eq!(reader.read_vectored(&mut [IoSliceMut::new(&mut buf)]).unwrap(), 0);
|
2019-02-08 19:42:34 +00:00
|
|
|
let mut buf = [0];
|
|
|
|
assert_eq!(
|
2019-12-22 22:42:04 +00:00
|
|
|
reader
|
|
|
|
.read_vectored(&mut [IoSliceMut::new(&mut []), IoSliceMut::new(&mut buf),])
|
|
|
|
.unwrap(),
|
2019-02-08 19:42:34 +00:00
|
|
|
1,
|
|
|
|
);
|
|
|
|
assert_eq!(reader.len(), 7);
|
|
|
|
let b: &[_] = &[0];
|
|
|
|
assert_eq!(buf, b);
|
|
|
|
let mut buf1 = [0; 4];
|
|
|
|
let mut buf2 = [0; 4];
|
|
|
|
assert_eq!(
|
2019-12-22 22:42:04 +00:00
|
|
|
reader
|
|
|
|
.read_vectored(&mut [IoSliceMut::new(&mut buf1), IoSliceMut::new(&mut buf2)],)
|
|
|
|
.unwrap(),
|
2019-02-08 19:42:34 +00:00
|
|
|
7,
|
|
|
|
);
|
|
|
|
let b1: &[_] = &[1, 2, 3, 4];
|
|
|
|
let b2: &[_] = &[5, 6, 7];
|
|
|
|
assert_eq!(buf1, b1);
|
|
|
|
assert_eq!(&buf2[..3], b2);
|
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 0);
|
|
|
|
}
|
|
|
|
|
2017-12-04 04:45:12 +00:00
|
|
|
#[test]
|
|
|
|
fn test_read_exact() {
|
|
|
|
let in_buf = vec![0, 1, 2, 3, 4, 5, 6, 7];
|
|
|
|
let reader = &mut &in_buf[..];
|
|
|
|
let mut buf = [];
|
|
|
|
assert!(reader.read_exact(&mut buf).is_ok());
|
|
|
|
let mut buf = [8];
|
|
|
|
assert!(reader.read_exact(&mut buf).is_ok());
|
|
|
|
assert_eq!(buf[0], 0);
|
|
|
|
assert_eq!(reader.len(), 7);
|
|
|
|
let mut buf = [0, 0, 0, 0, 0, 0, 0];
|
|
|
|
assert!(reader.read_exact(&mut buf).is_ok());
|
|
|
|
assert_eq!(buf, [1, 2, 3, 4, 5, 6, 7]);
|
|
|
|
assert_eq!(reader.len(), 0);
|
|
|
|
let mut buf = [0];
|
|
|
|
assert!(reader.read_exact(&mut buf).is_err());
|
|
|
|
}
|
|
|
|
|
2015-02-01 04:24:36 +00:00
|
|
|
#[test]
|
|
|
|
fn test_buf_reader() {
|
2015-03-03 08:42:26 +00:00
|
|
|
let in_buf = vec![0, 1, 2, 3, 4, 5, 6, 7];
|
2015-03-30 18:00:05 +00:00
|
|
|
let mut reader = Cursor::new(&in_buf[..]);
|
2015-02-01 04:24:36 +00:00
|
|
|
let mut buf = [];
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 0);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(reader.position(), 0);
|
|
|
|
let mut buf = [0];
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 1);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(reader.position(), 1);
|
|
|
|
let b: &[_] = &[0];
|
|
|
|
assert_eq!(buf, b);
|
|
|
|
let mut buf = [0; 4];
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 4);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(reader.position(), 5);
|
|
|
|
let b: &[_] = &[1, 2, 3, 4];
|
|
|
|
assert_eq!(buf, b);
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 3);
|
2015-02-01 04:24:36 +00:00
|
|
|
let b: &[_] = &[5, 6, 7];
|
|
|
|
assert_eq!(&buf[..3], b);
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(reader.read(&mut buf).unwrap(), 0);
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn seek_past_end() {
|
|
|
|
let buf = [0xff];
|
2015-02-18 19:48:57 +00:00
|
|
|
let mut r = Cursor::new(&buf[..]);
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(r.seek(SeekFrom::Start(10)).unwrap(), 10);
|
|
|
|
assert_eq!(r.read(&mut [0]).unwrap(), 0);
|
2015-02-01 04:24:36 +00:00
|
|
|
|
2016-10-29 21:54:04 +00:00
|
|
|
let mut r = Cursor::new(vec![10]);
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(r.seek(SeekFrom::Start(10)).unwrap(), 10);
|
|
|
|
assert_eq!(r.read(&mut [0]).unwrap(), 0);
|
2015-02-01 04:24:36 +00:00
|
|
|
|
|
|
|
let mut buf = [0];
|
2015-02-18 19:48:57 +00:00
|
|
|
let mut r = Cursor::new(&mut buf[..]);
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(r.seek(SeekFrom::Start(10)).unwrap(), 10);
|
|
|
|
assert_eq!(r.write(&[3]).unwrap(), 0);
|
2015-08-19 06:02:49 +00:00
|
|
|
|
|
|
|
let mut r = Cursor::new(vec![10].into_boxed_slice());
|
|
|
|
assert_eq!(r.seek(SeekFrom::Start(10)).unwrap(), 10);
|
|
|
|
assert_eq!(r.write(&[3]).unwrap(), 0);
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
|
|
|
|
2017-02-16 01:16:41 +00:00
|
|
|
#[test]
|
|
|
|
fn seek_past_i64() {
|
|
|
|
let buf = [0xff];
|
|
|
|
let mut r = Cursor::new(&buf[..]);
|
|
|
|
assert_eq!(r.seek(SeekFrom::Start(6)).unwrap(), 6);
|
|
|
|
assert_eq!(r.seek(SeekFrom::Current(0x7ffffffffffffff0)).unwrap(), 0x7ffffffffffffff6);
|
|
|
|
assert_eq!(r.seek(SeekFrom::Current(0x10)).unwrap(), 0x8000000000000006);
|
|
|
|
assert_eq!(r.seek(SeekFrom::Current(0)).unwrap(), 0x8000000000000006);
|
|
|
|
assert!(r.seek(SeekFrom::Current(0x7ffffffffffffffd)).is_err());
|
|
|
|
assert_eq!(r.seek(SeekFrom::Current(-0x8000000000000000)).unwrap(), 6);
|
|
|
|
|
|
|
|
let mut r = Cursor::new(vec![10]);
|
|
|
|
assert_eq!(r.seek(SeekFrom::Start(6)).unwrap(), 6);
|
|
|
|
assert_eq!(r.seek(SeekFrom::Current(0x7ffffffffffffff0)).unwrap(), 0x7ffffffffffffff6);
|
|
|
|
assert_eq!(r.seek(SeekFrom::Current(0x10)).unwrap(), 0x8000000000000006);
|
|
|
|
assert_eq!(r.seek(SeekFrom::Current(0)).unwrap(), 0x8000000000000006);
|
|
|
|
assert!(r.seek(SeekFrom::Current(0x7ffffffffffffffd)).is_err());
|
|
|
|
assert_eq!(r.seek(SeekFrom::Current(-0x8000000000000000)).unwrap(), 6);
|
|
|
|
|
|
|
|
let mut buf = [0];
|
|
|
|
let mut r = Cursor::new(&mut buf[..]);
|
|
|
|
assert_eq!(r.seek(SeekFrom::Start(6)).unwrap(), 6);
|
|
|
|
assert_eq!(r.seek(SeekFrom::Current(0x7ffffffffffffff0)).unwrap(), 0x7ffffffffffffff6);
|
|
|
|
assert_eq!(r.seek(SeekFrom::Current(0x10)).unwrap(), 0x8000000000000006);
|
|
|
|
assert_eq!(r.seek(SeekFrom::Current(0)).unwrap(), 0x8000000000000006);
|
|
|
|
assert!(r.seek(SeekFrom::Current(0x7ffffffffffffffd)).is_err());
|
|
|
|
assert_eq!(r.seek(SeekFrom::Current(-0x8000000000000000)).unwrap(), 6);
|
|
|
|
|
|
|
|
let mut r = Cursor::new(vec![10].into_boxed_slice());
|
|
|
|
assert_eq!(r.seek(SeekFrom::Start(6)).unwrap(), 6);
|
|
|
|
assert_eq!(r.seek(SeekFrom::Current(0x7ffffffffffffff0)).unwrap(), 0x7ffffffffffffff6);
|
|
|
|
assert_eq!(r.seek(SeekFrom::Current(0x10)).unwrap(), 0x8000000000000006);
|
|
|
|
assert_eq!(r.seek(SeekFrom::Current(0)).unwrap(), 0x8000000000000006);
|
|
|
|
assert!(r.seek(SeekFrom::Current(0x7ffffffffffffffd)).is_err());
|
|
|
|
assert_eq!(r.seek(SeekFrom::Current(-0x8000000000000000)).unwrap(), 6);
|
|
|
|
}
|
|
|
|
|
2015-02-01 04:24:36 +00:00
|
|
|
#[test]
|
|
|
|
fn seek_before_0() {
|
2015-03-03 08:42:26 +00:00
|
|
|
let buf = [0xff];
|
2015-02-18 19:48:57 +00:00
|
|
|
let mut r = Cursor::new(&buf[..]);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert!(r.seek(SeekFrom::End(-2)).is_err());
|
|
|
|
|
2016-10-29 21:54:04 +00:00
|
|
|
let mut r = Cursor::new(vec![10]);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert!(r.seek(SeekFrom::End(-2)).is_err());
|
|
|
|
|
|
|
|
let mut buf = [0];
|
2015-02-18 19:48:57 +00:00
|
|
|
let mut r = Cursor::new(&mut buf[..]);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert!(r.seek(SeekFrom::End(-2)).is_err());
|
2015-08-19 06:02:49 +00:00
|
|
|
|
2016-10-29 21:54:04 +00:00
|
|
|
let mut r = Cursor::new(vec![10].into_boxed_slice());
|
2015-08-19 06:02:49 +00:00
|
|
|
assert!(r.seek(SeekFrom::End(-2)).is_err());
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_seekable_mem_writer() {
|
|
|
|
let mut writer = Cursor::new(Vec::<u8>::new());
|
|
|
|
assert_eq!(writer.position(), 0);
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(writer.write(&[0]).unwrap(), 1);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(writer.position(), 1);
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(writer.write(&[1, 2, 3]).unwrap(), 3);
|
|
|
|
assert_eq!(writer.write(&[4, 5, 6, 7]).unwrap(), 4);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(writer.position(), 8);
|
|
|
|
let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7];
|
2015-02-25 06:20:34 +00:00
|
|
|
assert_eq!(&writer.get_ref()[..], b);
|
2015-02-01 04:24:36 +00:00
|
|
|
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(writer.seek(SeekFrom::Start(0)).unwrap(), 0);
|
2015-02-01 04:24:36 +00:00
|
|
|
assert_eq!(writer.position(), 0);
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(writer.write(&[3, 4]).unwrap(), 2);
|
2015-02-01 04:24:36 +00:00
|
|
|
let b: &[_] = &[3, 4, 2, 3, 4, 5, 6, 7];
|
2015-02-25 06:20:34 +00:00
|
|
|
assert_eq!(&writer.get_ref()[..], b);
|
2015-02-01 04:24:36 +00:00
|
|
|
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(writer.seek(SeekFrom::Current(1)).unwrap(), 3);
|
|
|
|
assert_eq!(writer.write(&[0, 1]).unwrap(), 2);
|
2015-02-01 04:24:36 +00:00
|
|
|
let b: &[_] = &[3, 4, 2, 0, 1, 5, 6, 7];
|
2015-02-25 06:20:34 +00:00
|
|
|
assert_eq!(&writer.get_ref()[..], b);
|
2015-02-01 04:24:36 +00:00
|
|
|
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(writer.seek(SeekFrom::End(-1)).unwrap(), 7);
|
|
|
|
assert_eq!(writer.write(&[1, 2]).unwrap(), 2);
|
2015-02-01 04:24:36 +00:00
|
|
|
let b: &[_] = &[3, 4, 2, 0, 1, 5, 6, 1, 2];
|
2015-02-25 06:20:34 +00:00
|
|
|
assert_eq!(&writer.get_ref()[..], b);
|
2015-02-01 04:24:36 +00:00
|
|
|
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(writer.seek(SeekFrom::End(1)).unwrap(), 10);
|
|
|
|
assert_eq!(writer.write(&[1]).unwrap(), 1);
|
2015-02-01 04:24:36 +00:00
|
|
|
let b: &[_] = &[3, 4, 2, 0, 1, 5, 6, 1, 2, 0, 1];
|
2015-02-25 06:20:34 +00:00
|
|
|
assert_eq!(&writer.get_ref()[..], b);
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn vec_seek_past_end() {
|
|
|
|
let mut r = Cursor::new(Vec::new());
|
2015-03-31 23:20:09 +00:00
|
|
|
assert_eq!(r.seek(SeekFrom::Start(10)).unwrap(), 10);
|
|
|
|
assert_eq!(r.write(&[3]).unwrap(), 1);
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn vec_seek_before_0() {
|
|
|
|
let mut r = Cursor::new(Vec::new());
|
|
|
|
assert!(r.seek(SeekFrom::End(-2)).is_err());
|
|
|
|
}
|
2016-09-30 22:00:00 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
fn vec_seek_and_write_past_usize_max() {
|
|
|
|
let mut c = Cursor::new(Vec::new());
|
2020-06-02 07:59:11 +00:00
|
|
|
c.set_position(usize::MAX as u64 + 1);
|
2016-09-30 22:00:00 +00:00
|
|
|
assert!(c.write_all(&[1, 2, 3]).is_err());
|
|
|
|
}
|
2019-12-11 16:59:32 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_partial_eq() {
|
|
|
|
assert_eq!(Cursor::new(Vec::<u8>::new()), Cursor::new(Vec::<u8>::new()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_eq() {
|
|
|
|
struct AssertEq<T: Eq>(pub T);
|
|
|
|
|
|
|
|
let _: AssertEq<Cursor<Vec<u8>>> = AssertEq(Cursor::new(Vec::new()));
|
|
|
|
}
|
2015-02-01 04:24:36 +00:00
|
|
|
}
|