custom_print/
concat_try_writer.rs1use core::fmt::{Arguments, Debug};
2
3use crate::{IntoTryWriteFn, NeverError, WriteBytes, WriteStr};
4
5#[derive(Clone, Copy, Debug, Eq, PartialEq)]
13pub struct ConcatTryWriter<F1>(F1);
14
15pub trait IntoConcatWriteResult {
20 type Output;
22
23 fn into_concat_write_result(self) -> Self::Output;
25}
26
27impl<F1> ConcatTryWriter<F1>
28where
29 F1: WriteStr,
30{
31 pub fn new(write: F1) -> Self {
33 Self(write)
34 }
35
36 pub fn from_closure<F, Ts>(closure: F) -> Self
39 where
40 F: IntoTryWriteFn<Ts, TryWriteFn = F1>,
41 {
42 Self(closure.into_try_write_fn())
43 }
44}
45
46impl<F1> ConcatTryWriter<F1>
47where
48 Self: WriteStr,
49{
50 pub fn write_fmt(&mut self, args: Arguments<'_>) -> <Self as WriteStr>::Output {
59 if let Some(buf) = args.as_str() {
60 self.write_str(buf)
61 } else {
62 let buf = alloc::fmt::format(args);
63 self.write_str(&buf)
64 }
65 }
66}
67
68impl<F1, Output> WriteStr for ConcatTryWriter<F1>
69where
70 F1: WriteStr,
71 F1::Output: IntoConcatWriteResult<Output = Output>,
72{
73 type Output = Output;
74
75 fn write_str(&mut self, buf: &str) -> Output {
76 self.0.write_str(buf).into_concat_write_result()
77 }
78}
79
80impl<F1, Output> WriteBytes for ConcatTryWriter<F1>
81where
82 F1: WriteBytes,
83 F1::Output: IntoConcatWriteResult<Output = Output>,
84{
85 type Output = Output;
86
87 fn write_bytes(&mut self, buf: &[u8]) -> Output {
88 self.0.write_bytes(buf).into_concat_write_result()
89 }
90}
91
92impl IntoConcatWriteResult for () {
93 type Output = Result<(), NeverError>;
94 fn into_concat_write_result(self) -> Self::Output {
95 Ok(())
96 }
97}
98
99impl<T, E: Debug> IntoConcatWriteResult for Result<T, E> {
100 type Output = Result<T, E>;
101 fn into_concat_write_result(self) -> Self::Output {
102 self
103 }
104}