pub struct Cursor<T> { /* private fields */ }
Expand description
Cursor
包装内存中的缓冲区,并为其提供 Seek
实现。
Cursor
s 与内存缓冲区一起使用,任何实现 AsRef<[u8]>
,以允许它们实现 Read
或者 Write
,从而允许这些缓冲区在您可能使用进行实际 I/O 的读取器或写入器的任何地方使用。
标准库在通常用作缓冲区的各种类型上实现了一些 I/O traits,例如 Cursor<Vec<u8>>
and Cursor<&[u8]>
。
Examples
我们可能想在生产代码中将字节写入 File
,但在测试中使用内存缓冲区。我们可以做到这一点
Cursor
:
use std::io::prelude::*;
use std::io::{self, SeekFrom};
use std::fs::File;
// 我们编写的库函数
fn write_ten_bytes_at_end<W: Write + Seek>(mut writer: W) -> io::Result<()> {
writer.seek(SeekFrom::End(-10))?;
for i in 0..10 {
writer.write(&[i])?;
}
// 一切顺利
Ok(())
}
// 这是一些使用此库函数的代码。
// 我们可能想在此处使用 BufReader 来提高效率,但让我们继续关注此示例。
let mut file = File::create("foo.txt")?;
write_ten_bytes_at_end(&mut file)?;
// 现在让我们编写一个测试
#[test]
fn test_writes_bytes() {
// 设置一个真实的文件要比内存中的缓冲区慢得多,让我们用游标代替
use std::io::Cursor;
let mut buff = Cursor::new(vec![0; 15]);
write_ten_bytes_at_end(&mut buff).unwrap();
assert_eq!(&buff.get_ref()[5..15], &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
}
RunImplementations§
source§impl<T> Cursor<T>
impl<T> Cursor<T>
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
const: unstable · sourcepub fn position(&self) -> u64
pub fn position(&self) -> u64
返回此游标的当前位置。
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);
Runsourcepub fn set_position(&mut self, pos: u64)
pub fn set_position(&mut self, pos: u64)
source§impl<T> Cursor<T>where
T: AsRef<[u8]>,
impl<T> Cursor<T>where T: AsRef<[u8]>,
sourcepub fn remaining_slice(&self) -> &[u8] ⓘ
🔬This is a nightly-only experimental API. (cursor_remaining
#86369)
pub fn remaining_slice(&self) -> &[u8] ⓘ
cursor_remaining
#86369)返回剩余的三个。
Examples
#![feature(cursor_remaining)]
use std::io::Cursor;
let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
assert_eq!(buff.remaining_slice(), &[1, 2, 3, 4, 5]);
buff.set_position(2);
assert_eq!(buff.remaining_slice(), &[3, 4, 5]);
buff.set_position(4);
assert_eq!(buff.remaining_slice(), &[5]);
buff.set_position(6);
assert_eq!(buff.remaining_slice(), &[]);
RunTrait Implementations§
source§impl<T> BufRead for Cursor<T>where
T: AsRef<[u8]>,
impl<T> BufRead for Cursor<T>where T: AsRef<[u8]>,
source§fn fill_buf(&mut self) -> Result<&[u8]>
fn fill_buf(&mut self) -> Result<&[u8]>
返回内部缓冲区的内容,如果内部缓冲区为空,则使用内部 reader 中的更多数据填充内部缓冲区。 Read more
source§fn has_data_left(&mut self) -> Result<bool>
fn has_data_left(&mut self) -> Result<bool>
🔬This is a nightly-only experimental API. (
buf_read_has_data_left
#86423)检查底层
Read
是否有任何数据可供读取。 Read moresource§impl<T> Read for Cursor<T>where
T: AsRef<[u8]>,
impl<T> Read for Cursor<T>where T: AsRef<[u8]>,
source§fn read_buf(&mut self, cursor: BorrowedCursor<'_>) -> Result<()>
fn read_buf(&mut self, cursor: BorrowedCursor<'_>) -> Result<()>
🔬This is a nightly-only experimental API. (
read_buf
#78485)从此源中提取一些字节到指定的缓冲区中。 Read more
source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
与
read
相似,不同之处在于它读入缓冲区的一部分。 Read moresource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector
#69941)source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
读取所有字节,直到此源中的 EOF 为止,然后将它们放入
buf
。 Read moresource§fn read_to_string(&mut self, buf: &mut String) -> Result<usize>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize>
读取这个源中的所有字节,直到 EOF 为止,然后将它们追加到
buf
。 Read moresource§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<()>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<()>
🔬This is a nightly-only experimental API. (
read_buf
#78485)读取填充
cursor
所需的确切字节数。 Read moresource§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere Self: Sized,
为这个
Read
实例创建一个 “by reference” 适配器。 Read moresource§impl<T> Seek for Cursor<T>where
T: AsRef<[u8]>,
impl<T> Seek for Cursor<T>where T: AsRef<[u8]>,
source§impl Write for Cursor<&mut [u8]>
impl Write for Cursor<&mut [u8]>
source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector
#69941)source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<()>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<()>
🔬This is a nightly-only experimental API. (
write_all_vectored
#70436)尝试将多个缓冲区写入此 writer。 Read more
1.25.0 · source§impl<A> Write for Cursor<&mut Vec<u8, A>>where
A: Allocator,
impl<A> Write for Cursor<&mut Vec<u8, A>>where A: Allocator,
source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector
#69941)source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<()>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<()>
🔬This is a nightly-only experimental API. (
write_all_vectored
#70436)尝试将多个缓冲区写入此 writer。 Read more
1.61.0 · source§impl<const N: usize> Write for Cursor<[u8; N]>
impl<const N: usize> Write for Cursor<[u8; N]>
source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector
#69941)source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<()>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<()>
🔬This is a nightly-only experimental API. (
write_all_vectored
#70436)尝试将多个缓冲区写入此 writer。 Read more
1.5.0 · source§impl<A> Write for Cursor<Box<[u8], A>>where
A: Allocator,
impl<A> Write for Cursor<Box<[u8], A>>where A: Allocator,
source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector
#69941)source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<()>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<()>
🔬This is a nightly-only experimental API. (
write_all_vectored
#70436)尝试将多个缓冲区写入此 writer。 Read more
source§impl<A> Write for Cursor<Vec<u8, A>>where
A: Allocator,
impl<A> Write for Cursor<Vec<u8, A>>where A: Allocator,
source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector
#69941)source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<()>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<()>
🔬This is a nightly-only experimental API. (
write_all_vectored
#70436)尝试将多个缓冲区写入此 writer。 Read more
impl<T: Eq> Eq for Cursor<T>
impl<T> StructuralEq for Cursor<T>
impl<T> StructuralPartialEq for Cursor<T>
Auto Trait Implementations§
impl<T> RefUnwindSafe for Cursor<T>where T: RefUnwindSafe,
impl<T> Send for Cursor<T>where T: Send,
impl<T> Sync for Cursor<T>where T: Sync,
impl<T> Unpin for Cursor<T>where T: Unpin,
impl<T> UnwindSafe for Cursor<T>where T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
从拥有的值中借用。 Read more