3 #![feature(io_error_uncategorized)]
6 use std::io::{self, Error, Write, sink};
10 impl fmt::Display for ErrorDisplay {
11 fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
18 const FORMAT_ERROR: io::ErrorKind = io::ErrorKind::Uncategorized;
19 const WRITER_ERROR: io::ErrorKind = io::ErrorKind::NotConnected;
21 impl Write for ErrorWriter {
22 fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
23 Err(Error::new(WRITER_ERROR, "not connected"))
26 fn flush(&mut self) -> io::Result<()> { Ok(()) }
30 // Test that the error from the formatter is propagated.
31 let res = write!(sink(), "{} {} {}", 1, ErrorDisplay, "bar");
32 assert!(res.is_err(), "formatter error did not propagate");
33 assert_eq!(res.unwrap_err().kind(), FORMAT_ERROR);
35 // Test that an underlying error is propagated
36 let res = write!(ErrorWriter, "abc");
37 assert!(res.is_err(), "writer error did not propagate");
40 let res = write!(ErrorWriter, "abc {}", ErrorDisplay);
41 assert!(res.is_err(), "writer error did not propagate");
42 assert_eq!(res.unwrap_err().kind(), WRITER_ERROR);
45 let res = write!(ErrorWriter, "{} abc", ErrorDisplay);
46 assert!(res.is_err(), "formatter error did not propagate");
47 assert_eq!(res.unwrap_err().kind(), FORMAT_ERROR);