Skip to content

Struct std::io::Take

Reader 适配器,用于限制从底层 reader 读取的字节。

这个结构体通常是通过在 reader 上调用 take 来创建的。 有关更多详细信息,请参见 take 的文档。

Implementations

impl<T> Take<T>

limit

返回在此实例返回 EOF 之前可以读取的字节数。

rust
pub fn limit(&self) -> u64

返回值:返回可读字节数

TIP

如果底层 Read 实例达到 EOF,则在读取的字节数少于此方法指示的字节数之后,此实例可能到达 EOF

rust
use std::io;
use std::io::prelude::*;
use std::fs::File;

fn main() -> io::Result<()> {
    let f = File::open("foo.txt")?;

    // 最多读取五个字节
    let handle = f.take(5);

    println!("limit: {}", handle.limit());
    Ok(())
}

set_limit

设置在此实例返回 EOF 之前可以读取的字节数。 这与构造新的 Take 实例相同,因此在调用此方法时,读取的字节数和先前的限制值无关紧要。

rust
pub fn set_limit(&mut self, limit: u64)

参数

  • limit:需要设置的可读字节数
rust
use std::io;
use std::io::prelude::*;
use std::fs::File;

fn main() -> io::Result<()> {
    let f = File::open("foo.txt")?;

    // 最多读取五个字节
    let mut handle = f.take(5);
    handle.set_limit(10);

    assert_eq!(handle.limit(), 10);
    Ok(())
}

into_inner

消耗 Take,返回包装的 reader。

rust
pub fn into_inner(self) -> T

返回值:返回内部的reader

rust
use std::io;
use std::io::prelude::*;
use std::fs::File;

fn main() -> io::Result<()> {
    let mut file = File::open("foo.txt")?;

    let mut buffer = [0; 5];
    let mut handle = file.take(5);
    handle.read(&mut buffer)?;

    let file = handle.into_inner();
    Ok(())
}

get_ref

获取对底层 reader 的引用。

rust
pub fn get_ref(&self) -> &T

返回值:返回内部reader的不可变引用

rust
use std::io;
use std::io::prelude::*;
use std::fs::File;

fn main() -> io::Result<()> {
    let mut file = File::open("foo.txt")?;

    let mut buffer = [0; 5];
    let mut handle = file.take(5);
    handle.read(&mut buffer)?;

    let file = handle.get_ref();
    Ok(())
}

get_mut

获取底层 reader 的可变引用。

应注意避免修改底层 reader 的内部 I/O 状态,因为这样做可能会破坏该 Take 的内部限制。

rust
pub fn get_mut(&mut self) -> &mut T

返回值:返回内部reader的可变引用

rust
use std::io;
use std::io::prelude::*;
use std::fs::File;

fn main() -> io::Result<()> {
    let mut file = File::open("foo.txt")?;

    let mut buffer = [0; 5];
    let mut handle = file.take(5);
    handle.read(&mut buffer)?;

    let file = handle.get_mut();
    Ok(())
}

Trait Implementations

impl<T: BufRead> BufRead for Take<T>

fill_buf

返回内部缓冲区的内容,如果内部缓冲区为空,则使用内部 reader 中的更多数据填充内部缓冲区。

rust
fn fill_buf(&mut self) -> Result<&[u8]>

consume

告诉此缓冲区 amt 字节已从缓冲区中消耗掉,因此在调用 read 时不再应返回它们。

rust
fn consume(&mut self, amt: usize)

has_data_left

检查底层 Read 是否有任何数据可供读取。 nightly-only

rust
fn has_data_left(&mut self) -> Result<bool>

read_until

将所有字节读入 buf,直到到达定界符 byte 或 EOF。

rust
fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize>

read_line

读取所有字节直到到达换行符 (0xA 字节),并将它们,追加,到提供的 String 缓冲区。

rust
fn read_line(&mut self, buf: &mut String) -> Result<usize>

split

返回对该字节 byte 上的 reader 拆分内容的迭代器。

rust
fn split(self, byte: u8) -> Split<Self> 
where
  Self: Sized,

lines

返回此 reader 的各行上的迭代器。

rust
fn lines(self) -> Lines<Self> 
where
  Self: Sized,

impl<T: Debug> Debug for Take<T>

fmt

使用给定的格式化程序格式化该值。

rust
fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl<T: Read> Read for Take<T>

read

从该源中提取一些字节到指定的缓冲区中,返回读取的字节数。

rust
fn read(&mut self, buf: &mut [u8]) -> Result<usize>

read_buf

从此源中提取一些字节到指定的缓冲区中。 nightly-only

rust
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<()>

read_vectored

与 read 相似,不同之处在于它读入缓冲区的一部分。

rust
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>

is_read_vectored

确定此 Read 是否具有有效的 read_vectored 实现。 nightly-only

rust
fn is_read_vectored(&self) -> bool

read_to_end

读取所有字节,直到此源中的 EOF 为止,然后将它们放入 buf。

rust
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>

read_to_string

读取这个源中的所有字节,直到 EOF 为止,然后将它们追加到 buf。

rust
fn read_to_string(&mut self, buf: &mut String) -> Result<usize>

read_exact

读取填充 buf 所需的确切字节数。

rust
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()>

read_buf_exact

读取填充 cursor 所需的确切字节数。 nightly-only

rust
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<()>

by_ref

为这个 Read 实例创建一个 “by reference” 适配器。

rust
fn by_ref(&mut self) -> &mut Self
where
  Self: Sized,

bytes

将此 Read 实例的字节数转换为 Iterator。

rust
fn bytes(self) -> Bytes<Self> 
where
  Self: Sized,

chain

创建一个适配器,将这个流与另一个链接起来。

rust
fn chain<R: Read>(self, next: R) -> Chain<Self, R> 
where
  Self: Sized,

take

创建一个适配器,最多从中读取 limit 个字节。

rust
fn take(self, limit: u64) -> Take<Self> 
where
  Self: Sized,

Auto Trait Implementations

impl<T> RefUnwindSafe for Take<T>

rust
impl<T> RefUnwindSafe for Take<T>
where
  T: RefUnwindSafe,

impl<T> Send for Take<T>

rust
impl<T> Send for Take<T>
where
  T: Send,

impl<T> Sync for Take<T>

rust
impl<T> Sync for Take<T>
where
  T: Sync,

impl<T> Unpin for Take<T>

rust
impl<T> Unpin for Take<T>
where
  T: Unpin,

impl<T> UnwindSafe for Take<T>

rust
impl<T> UnwindSafe for Take<T>
where
  T: UnwindSafe,

Blanket Implementations

impl<T> Any for T

rust
impl<T> Any for T
where
  T: 'static + ?Sized,

impl<T> Borrow<T> for T

rust
impl<T> Borrow<T> for T

where

  T: ?Sized,

impl<T> BorrowMut<T> for T

rust
impl<T> BorrowMut<T> for T
where
  T: ?Sized,

impl<T> From<T> for T

impl<T, U> Into<U> for T

rust
impl<T, U> Into<U> for T
where
  U: From<T>,

impl<T, U> TryFrom<U> for T

rust
impl<T, U> TryFrom<U> for T
where
  U: Into<T>,

impl<T, U> TryInto<U> for T

rust
impl<T, U> TryInto<U> for T
where
  U: TryFrom<T>,

MIT Licensed