4 use std::io::{self, Error, Write, sink};
8 impl fmt::Display for ErrorDisplay {
9 fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
16 const FORMAT_ERROR: io::ErrorKind = io::ErrorKind::Other;
17 const WRITER_ERROR: io::ErrorKind = io::ErrorKind::NotConnected;
19 impl Write for ErrorWriter {
20 fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
21 Err(Error::new(WRITER_ERROR, "not connected"))
24 fn flush(&mut self) -> io::Result<()> { Ok(()) }
28 // Test that the error from the formatter is propagated.
29 let res = write!(sink(), "{} {} {}", 1, ErrorDisplay, "bar");
30 assert!(res.is_err(), "formatter error did not propagate");
31 assert_eq!(res.unwrap_err().kind(), FORMAT_ERROR);
33 // Test that an underlying error is propagated
34 let res = write!(ErrorWriter, "abc");
35 assert!(res.is_err(), "writer error did not propagate");
38 let res = write!(ErrorWriter, "abc {}", ErrorDisplay);
39 assert!(res.is_err(), "writer error did not propagate");
40 assert_eq!(res.unwrap_err().kind(), WRITER_ERROR);
43 let res = write!(ErrorWriter, "{} abc", ErrorDisplay);
44 assert!(res.is_err(), "formatter error did not propagate");
45 assert_eq!(res.unwrap_err().kind(), FORMAT_ERROR);