print_bytes/
writer.rs

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#![cfg_attr(windows, allow(private_interfaces))]

use std::io::BufWriter;
use std::io::LineWriter;
#[cfg(any(doc, not(feature = "specialization")))]
use std::io::Stderr;
#[cfg(any(doc, not(feature = "specialization")))]
use std::io::StderrLock;
#[cfg(any(doc, not(feature = "specialization")))]
use std::io::Stdout;
#[cfg(any(doc, not(feature = "specialization")))]
use std::io::StdoutLock;
use std::io::Write;
#[cfg(all(feature = "specialization", windows))]
use std::os::windows::io::AsHandle;

#[cfg(windows)]
use super::console::Console;

#[cfg(windows)]
pub(super) trait ToConsole {
    fn to_console(&self) -> Option<Console<'_>>;
}

#[cfg(all(feature = "specialization", windows))]
impl<T> ToConsole for T
where
    T: ?Sized,
{
    default fn to_console(&self) -> Option<Console<'_>> {
        None
    }
}

#[cfg(all(feature = "specialization", windows))]
impl<T> ToConsole for T
where
    T: AsHandle + ?Sized + Write,
{
    fn to_console(&self) -> Option<Console<'_>> {
        Console::from_handle(self)
    }
}

/// A bound for [`write_lossy`] that allows it to be used for some types
/// without specialization.
///
/// When the "specialization" feature is enabled, this trait is implemented for
/// all types.
///
/// [`write_lossy`]: super::write_lossy
pub trait WriteLossy {
    #[cfg(windows)]
    #[doc(hidden)]
    fn __to_console(&self) -> Option<Console<'_>>;
}

#[cfg(feature = "specialization")]
#[cfg_attr(print_bytes_docs_rs, doc(cfg(feature = "specialization")))]
impl<T> WriteLossy for T
where
    T: ?Sized,
{
    #[cfg(windows)]
    default fn __to_console(&self) -> Option<Console<'_>> {
        self.to_console()
    }
}

macro_rules! r#impl {
    ( $generic:ident , $type:ty ) => {
        impl<$generic> WriteLossy for $type
        where
            $generic: ?Sized + WriteLossy,
        {
            #[cfg(windows)]
            fn __to_console(&self) -> Option<Console<'_>> {
                (**self).__to_console()
            }
        }
    };
}
r#impl!(T, &mut T);
r#impl!(T, Box<T>);

macro_rules! r#impl {
    ( $generic:ident , $type:ty ) => {
        impl<$generic> WriteLossy for $type
        where
            $generic: Write + WriteLossy,
        {
            #[cfg(windows)]
            fn __to_console(&self) -> Option<Console<'_>> {
                self.get_ref().__to_console()
            }
        }
    };
}
r#impl!(T, BufWriter<T>);
r#impl!(T, LineWriter<T>);

macro_rules! impl_to_console {
    ( $(#[ $attr:meta ])* $type:ty , $to_console_fn:expr , ) => {
        #[cfg(any(doc, not(feature = "specialization")))]
        impl $crate::WriteLossy for $type {
            #[cfg(windows)]
            fn __to_console(&self) -> Option<Console<'_>> {
                $crate::writer::ToConsole::to_console(self)
            }
        }

        #[cfg(windows)]
        $(#[$attr])*
        impl $crate::writer::ToConsole for $type {
            fn to_console<'a>(&'a self) -> Option<Console<'a>> {
                let to_console_fn: fn(&'a Self) -> _ = $to_console_fn;
                to_console_fn(self)
            }
        }
    };
}

macro_rules! r#impl {
    ( $($type:ty),+ ) => {
    $(
        impl_to_console! {
            #[cfg(not(feature = "specialization"))]
            $type, Console::from_handle,
        }
    )+
    };
}
r#impl!(Stderr, StderrLock<'_>, Stdout, StdoutLock<'_>);

impl_to_console! {
    #[cfg(not(feature = "specialization"))]
    Vec<u8>, |_| None,
}