Debian Codegraph @

src/writer.rs

raw symbols SCIP
1 use std::io::BufWriter;
2 use std::io::LineWriter;
3 #[cfg(any(doc, not(feature = "specialization")))]
4 use std::io::Stderr;
5 #[cfg(any(doc, not(feature = "specialization")))]
6 use std::io::StderrLock;
7 #[cfg(any(doc, not(feature = "specialization")))]
8 use std::io::Stdout;
9 #[cfg(any(doc, not(feature = "specialization")))]
10 use std::io::StdoutLock;
11 use std::io::Write;
12 #[cfg(all(feature = "specialization", windows))]
13 use std::os::windows::io::AsHandle;
14
15 #[cfg(windows)]
16 use super::console::Console;
17
18 pub(super) trait ToConsole {
19 #[cfg(windows)]
20 fn to_console(&self) -> Option<Console<'_>>;
21 }
22
23 #[cfg(feature = "specialization")]
24 impl<T> ToConsole for T
25 where
26 T: ?Sized,
27 {
28 #[cfg(windows)]
29 default fn to_console(&self) -> Option<Console<'_>> {
30 None
31 }
32 }
33
34 #[cfg(all(feature = "specialization", windows))]
35 impl<T> ToConsole for T
36 where
37 T: AsHandle + ?Sized + Write,
38 {
39 fn to_console(&self) -> Option<Console<'_>> {
40 Console::from_handle(self)
41 }
42 }
43
44 /// A bound for [`write_lossy`] that allows it to be used for some types
45 /// without specialization.
46 ///
47 /// When the "specialization" feature is enabled, this trait is implemented for
48 /// all types.
49 ///
50 /// [`write_lossy`]: super::write_lossy
51 pub trait WriteLossy {
52 #[cfg(windows)]
53 #[doc(hidden)]
54 fn __to_console(&self) -> Option<Console<'_>>;
55 }
56
57 #[cfg(feature = "specialization")]
58 #[cfg_attr(print_bytes_docs_rs, doc(cfg(feature = "specialization")))]
59 impl<T> WriteLossy for T
60 where
61 T: ?Sized,
62 {
63 #[cfg(windows)]
64 default fn __to_console(&self) -> Option<Console<'_>> {
65 self.to_console()
66 }
67 }
68
69 macro_rules! r#impl {
70 ( $generic:ident , $type:ty ) => {
71 impl<$generic> WriteLossy for $type
72 where
73 $generic: ?Sized + WriteLossy,
74 {
75 #[cfg(windows)]
76 fn __to_console(&self) -> Option<Console<'_>> {
77 (**self).__to_console()
78 }
79 }
80 };
81 }
82 r#impl!(T, &mut T);
83 r#impl!(T, Box<T>);
84
85 macro_rules! r#impl {
86 ( $generic:ident , $type:ty ) => {
87 impl<$generic> WriteLossy for $type
88 where
89 $generic: Write + WriteLossy,
90 {
91 #[cfg(windows)]
92 fn __to_console(&self) -> Option<Console<'_>> {
93 self.get_ref().__to_console()
94 }
95 }
96 };
97 }
98 r#impl!(T, BufWriter<T>);
99 r#impl!(T, LineWriter<T>);
100
101 macro_rules! impl_to_console {
102 ( $(#[ $attr:meta ])* $type:ty , $to_console_fn:expr , ) => {
103 #[cfg(any(doc, not(feature = "specialization")))]
104 impl $crate::WriteLossy for $type {
105 #[cfg(windows)]
106 fn __to_console(&self) -> Option<Console<'_>> {
107 $crate::writer::ToConsole::to_console(self)
108 }
109 }
110
111 $(#[$attr])*
112 impl $crate::writer::ToConsole for $type {
113 #[cfg(windows)]
114 fn to_console<'a>(&'a self) -> Option<Console<'a>> {
115 let to_console_fn: fn(&'a Self) -> _ = $to_console_fn;
116 to_console_fn(self)
117 }
118 }
119 };
120 }
121
122 macro_rules! r#impl {
123 ( $($type:ty),+ ) => {
124 $(impl_to_console! {
125 #[cfg(not(feature = "specialization"))]
126 $type, Console::from_handle,
127 })+
128 };
129 }
130 r#impl!(Stderr, StderrLock<'_>, Stdout, StdoutLock<'_>);
131
132 impl_to_console! {
133 #[cfg(not(feature = "specialization"))]
134 Vec<u8>, |_| None,
135 }