Skip to content

Struct std::io::IntoInnerError

rust
pub struct IntoInnerError<W>(_, _);

BufWriter::into_inner 返回的错误,将写出缓冲区时发生的错误与缓冲的 writer 对象结合在一起,可用于从条件中恢复。

示例

rust
use std::io::BufWriter;
use std::net::TcpStream;

let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());

// 用流做东西

// 我们想取回 `TcpStream`,所以让我们尝试:

let stream = match stream.into_inner() {
    Ok(s) => s,
    Err(e) => {
        // 在这里,e 是一个 IntoInnerError
        panic!("An error occurred");
    }
};

Implementations

impl<W> IntoInnerError<W>

error

返回导致 BufWriter::into_inner() 调用失败的错误。

尝试写入内部缓冲区时返回此错误。

rust
pub fn error(&self) -> &Error

返回值:返回一个错误对象

rust
use std::io::BufWriter;
use std::net::TcpStream;

let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());

// 用流做东西

// 我们想取回 `TcpStream`,所以让我们尝试:

let stream = match stream.into_inner() {
    Ok(s) => s,
    Err(e) => {
        // 在这里,e 是一个 IntoInnerError,让我们记录内部错误。
        // 在此示例中,我们仅将 'log' 更改为 stdout。
        println!("{}", e.error());

        panic!("An unexpected error occurred.");
    }
};

into_inner

返回产生错误的缓冲 writer 实例。

返回的对象可用于错误恢复,例如重新检查缓冲区。

rust
pub fn into_inner(self) -> W

返回值:返回内部的writer

rust
use std::io::BufWriter;
use std::net::TcpStream;

let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap());

// 用流做东西

// 我们想取回 `TcpStream`,所以让我们尝试:

let stream = match stream.into_inner() {
    Ok(s) => s,
    Err(e) => {
        // 在这里,e 是一个 IntoInnerError,让我们重新检查缓冲区:
        let buffer = e.into_inner();

        // 做一些事情来尝试恢复

        // 之后,让我们返回流
        buffer.into_inner().unwrap()
    }
};

into_error

消耗 IntoInnerError 并返回导致 BufWriter::into_inner() 调用失败的错误。 与 error 不同,它可用于获取基本错误的所有权。

rust
pub fn into_error(self) -> Error

返回值:返回一个Error对象

rust
use std::io::{BufWriter, ErrorKind, Write};

let mut not_enough_space = [0u8; 10];
let mut stream = BufWriter::new(not_enough_space.as_mut());
write!(stream, "this cannot be actually written").unwrap();
let into_inner_err = stream.into_inner().expect_err("now we discover it's too small");
let err = into_inner_err.into_error();
assert_eq!(err.kind(), ErrorKind::WriteZero);

into_parts

消耗 IntoInnerError 并返回导致 BufWriter::into_inner() 调用失败的错误,以及返回的 writer。

这可以用来简单地获取潜在错误的所有权。它也可以用于高级错误恢复。

rust
pub fn into_parts(self) -> (Error, W)

返回值:返回一个元组,第一个为Error对象,第二个为内部writer

rust
use std::io::{BufWriter, ErrorKind, Write};

let mut not_enough_space = [0u8; 10];
let mut stream = BufWriter::new(not_enough_space.as_mut());
write!(stream, "this cannot be actually written").unwrap();
let into_inner_err = stream.into_inner().expect_err("now we discover it's too small");
let (err, recovered_writer) = into_inner_err.into_parts();
assert_eq!(err.kind(), ErrorKind::WriteZero);
assert_eq!(recovered_writer.buffer(), b"t be actually written");

Trait Implementations

impl<W: Debug> Debug for IntoInnerError<W>

fmt

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

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

impl<W> Display for IntoInnerError<W>

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

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

impl<W: Send + Debug> Error for IntoInnerError<W>

source

此错误的下级来源 (如果有)。

rust
fn source(&self) -> Option<&(dyn Error + 'static)>

provide

提供对用于错误报告的上下文的基于类型的访问。

rust
fn provide<'a>(&'a self, demand: &mut Demand<'a>)

impl<W> From<IntoInnerError<W>> for Error

from

从输入类型转换为此类型。

rust
fn from(iie: IntoInnerError<W>) -> Error

Auto Trait Implementations

impl<W> !RefUnwindSafe for IntoInnerError<W>

impl<W> Send for IntoInnerError<W>

rust
impl<W> Send for IntoInnerError<W>
where
    W: Send,

impl<W> Sync for IntoInnerError<W>

rust
impl<W> Sync for IntoInnerError<W>
where
    W: Sync,

impl<W> Unpin for IntoInnerError<W>

rust
impl<W> Unpin for IntoInnerError<W>
where
    W: Unpin,

impl<W> !UnwindSafe for IntoInnerError<W>

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<E> Provider for E

rust
impl<E> Provider for E
where
    E: Error + ?Sized,

impl<T> ToString for T

rust
impl<T> ToString for T
where
    T: Display + ?Sized,

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