1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use ;
use crateWriteExtension;
/// Adapter that enables writing through a [`fmt::Write`] to an underlying
/// [`io::Write`].
///
/// # Examples
///
/// ```rust
/// # use std::{fmt, str};
/// use io_adapters::WriteExtension;
///
/// let mut output1 = String::new();
/// let mut output2 = [0u8; 13]; // Or io::stdout() for example
///
/// my_common_writer(&mut output1).unwrap();
/// my_common_writer(&mut output2.as_mut_slice().write_adapter()).unwrap();
///
/// fn my_common_writer(mut output: impl fmt::Write) -> fmt::Result {
/// write!(output, "Hello, World!")
/// }
///
/// assert_eq!(&output1, "Hello, World!");
/// assert_eq!(str::from_utf8(&output2).unwrap(), "Hello, World!");
/// ```
///
/// ## Error handling
///
/// Error handling is unpleasant with this adapter due to the [`fmt::Write`]
/// interface returning a stateless [`fmt::Error`] rather than an [`io::Error`].
/// Here is the suggested usage when unwrap doesn't cut it:
///
/// ```
/// # use std::{fmt, io, str};
/// # use io_adapters::WriteExtension;
///
/// let mut adapter = io::stdout().write_adapter();
/// match (conv(&mut adapter), adapter.error) {
/// (Ok(()), None) => {}
/// (Ok(()), Some(_)) => unreachable!(),
/// (Err(fmt::Error), None) => { /* Handle format error. */ }
/// (Err(fmt::Error), Some(e)) => { /* Handle I/O error. */ }
/// }
///
/// fn conv(output: impl fmt::Write) -> fmt::Result
/// # { Ok(()) }
/// ```