mirror of
https://gitee.com/openharmony/third_party_rust_either
synced 2024-11-23 06:59:40 +00:00
Merge pull request #60 from Kerollmops/impl-seek
Implement the io::Seek trait
This commit is contained in:
commit
cc9187fccf
47
src/lib.rs
47
src/lib.rs
@ -36,7 +36,7 @@ use std::ops::DerefMut;
|
||||
#[cfg(any(test, feature = "use_std"))]
|
||||
use std::error::Error;
|
||||
#[cfg(any(test, feature = "use_std"))]
|
||||
use std::io::{self, BufRead, Read, Write};
|
||||
use std::io::{self, BufRead, Read, Seek, SeekFrom, Write};
|
||||
|
||||
pub use Either::{Left, Right};
|
||||
|
||||
@ -903,6 +903,20 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "use_std"))]
|
||||
/// `Either<L, R>` implements `Seek` if both `L` and `R` do.
|
||||
///
|
||||
/// Requires crate feature `"use_std"`
|
||||
impl<L, R> Seek for Either<L, R>
|
||||
where
|
||||
L: Seek,
|
||||
R: Seek,
|
||||
{
|
||||
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
|
||||
for_both!(*self, ref mut inner => inner.seek(pos))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(test, feature = "use_std"))]
|
||||
/// Requires crate feature `"use_std"`
|
||||
impl<L, R> BufRead for Either<L, R>
|
||||
@ -1113,6 +1127,37 @@ fn iter() {
|
||||
assert_eq!(iter.count(), 9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn seek() {
|
||||
use std::io;
|
||||
|
||||
let use_empty = false;
|
||||
let mut mockdata = [0x00; 256];
|
||||
for i in 0..256 {
|
||||
mockdata[i] = i as u8;
|
||||
}
|
||||
|
||||
let mut reader = if use_empty {
|
||||
// Empty didn't impl Seek until Rust 1.51
|
||||
Left(io::Cursor::new([]))
|
||||
} else {
|
||||
Right(io::Cursor::new(&mockdata[..]))
|
||||
};
|
||||
|
||||
let mut buf = [0u8; 16];
|
||||
assert_eq!(reader.read(&mut buf).unwrap(), buf.len());
|
||||
assert_eq!(&buf, &mockdata[..buf.len()]);
|
||||
|
||||
// the first read should advance the cursor and return the next 16 bytes thus the `ne`
|
||||
assert_eq!(reader.read(&mut buf).unwrap(), buf.len());
|
||||
assert!(&buf != &mockdata[..buf.len()]); // (assert_ne needs Rust 1.13)
|
||||
|
||||
// if the seek operation fails it should read 16..31 instead of 0..15
|
||||
reader.seek(io::SeekFrom::Start(0)).unwrap();
|
||||
assert_eq!(reader.read(&mut buf).unwrap(), buf.len());
|
||||
assert_eq!(&buf, &mockdata[..buf.len()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_write() {
|
||||
use std::io;
|
||||
|
Loading…
Reference in New Issue
Block a user