2 use std::io::{self, Error, Write, sink};
6 impl fmt::Display for ErrorDisplay {
7 fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
14 const FORMAT_ERROR: io::ErrorKind = io::ErrorKind::Other;
15 const WRITER_ERROR: io::ErrorKind = io::ErrorKind::NotConnected;
17 impl Write for ErrorWriter {
18 fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
19 Err(Error::new(WRITER_ERROR, "not connected"))
22 fn flush(&mut self) -> io::Result<()> { Ok(()) }
26 // Test that the error from the formatter is propagated.
27 let res = write!(sink(), "{} {} {}", 1, ErrorDisplay, "bar");
28 assert!(res.is_err(), "formatter error did not propagate");
29 assert_eq!(res.unwrap_err().kind(), FORMAT_ERROR);
31 // Test that an underlying error is propagated
32 let res = write!(ErrorWriter, "abc");
33 assert!(res.is_err(), "writer error did not propagate");
36 let res = write!(ErrorWriter, "abc {}", ErrorDisplay);
37 assert!(res.is_err(), "writer error did not propagate");
38 assert_eq!(res.unwrap_err().kind(), WRITER_ERROR);
41 let res = write!(ErrorWriter, "{} abc", ErrorDisplay);
42 assert!(res.is_err(), "formatter error did not propagate");
43 assert_eq!(res.unwrap_err().kind(), FORMAT_ERROR);