Skip to content

Struct std::io::WriterPanicked

当底层 writer 之前有 panicked 时,为来自 BufWriter::into_parts 的缓冲数据返回错误。 包含 (可能是部分写入的) 缓冲数据

rust
pub struct WriterPanicked { /* private fields */ }

示例

rust
use std::io::{self, BufWriter, Write};
use std::panic::{catch_unwind, AssertUnwindSafe};

struct PanickingWriter;
impl Write for PanickingWriter {
  fn write(&mut self, buf: &[u8]) -> io::Result<usize> { panic!() }
  fn flush(&mut self) -> io::Result<()> { panic!() }
}

let mut stream = BufWriter::new(PanickingWriter);
write!(stream, "some data").unwrap();
let result = catch_unwind(AssertUnwindSafe(|| {
    stream.flush().unwrap()
}));
assert!(result.is_err());
let (recovered_writer, buffered_data) = stream.into_parts();
assert!(matches!(recovered_writer, PanickingWriter));
assert_eq!(buffered_data.unwrap_err().into_inner(), b"some data");

Implementations

impl WriterPanicked

into_inner

返回可能未写入的数据。 其中一些数据可能是由 panic 调用写入底层编写器的,因此简单地再次编写它并不是一个好主意。

rust
pub fn into_inner(self) -> Vec<u8>

Trait Implementations

impl Debug for WriterPanicked

fmt

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

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

impl Display for WriterPanicked

fmt

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

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

impl Error for WriterPanicked

source

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

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

provide

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

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

Auto Trait Implementations

impl RefUnwindSafe for WriterPanicked

impl Send for WriterPanicked

impl Sync for WriterPanicked

impl Unpin for WriterPanicked

impl UnwindSafe for WriterPanicked

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