|
| 1 | +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use std::fmt; |
| 12 | +use std::io::{self, Error, Write, sink}; |
| 13 | + |
| 14 | +struct ErrorDisplay; |
| 15 | + |
| 16 | +impl fmt::Display for ErrorDisplay { |
| 17 | + fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result { |
| 18 | + Err(fmt::Error) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +struct ErrorWriter; |
| 23 | + |
| 24 | +const FORMAT_ERROR: io::ErrorKind = io::ErrorKind::Other; |
| 25 | +const WRITER_ERROR: io::ErrorKind = io::ErrorKind::NotConnected; |
| 26 | + |
| 27 | +impl Write for ErrorWriter { |
| 28 | + fn write(&mut self, _buf: &[u8]) -> io::Result<usize> { |
| 29 | + Err(Error::new(WRITER_ERROR, "not connected")) |
| 30 | + } |
| 31 | + |
| 32 | + fn flush(&mut self) -> io::Result<()> { Ok(()) } |
| 33 | +} |
| 34 | + |
| 35 | +fn main() { |
| 36 | + // Test that the error from the formatter is propagated. |
| 37 | + let res = write!(sink(), "{} {} {}", 1, ErrorDisplay, "bar"); |
| 38 | + assert!(res.is_err(), "formatter error did not propagate"); |
| 39 | + assert_eq!(res.unwrap_err().kind(), FORMAT_ERROR); |
| 40 | + |
| 41 | + // Test that an underlying error is propagated |
| 42 | + let res = write!(ErrorWriter, "abc"); |
| 43 | + assert!(res.is_err(), "writer error did not propagate"); |
| 44 | + |
| 45 | + // Writer error |
| 46 | + let res = write!(ErrorWriter, "abc {}", ErrorDisplay); |
| 47 | + assert!(res.is_err(), "writer error did not propagate"); |
| 48 | + assert_eq!(res.unwrap_err().kind(), WRITER_ERROR); |
| 49 | + |
| 50 | + // Formatter error |
| 51 | + let res = write!(ErrorWriter, "{} abc", ErrorDisplay); |
| 52 | + assert!(res.is_err(), "formatter error did not propagate"); |
| 53 | + assert_eq!(res.unwrap_err().kind(), FORMAT_ERROR); |
| 54 | +} |
0 commit comments