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
macro_rules! rv {
    ($x:expr) => {{
        let val = $x;
        if val < 0 {
            Err(crate::util::error::Errno(-val as c_int))
        } else {
            Ok(())
        }
    }};
    ($x:expr, -> $t:ty) => {{
        let val = $x;
        if val < 0 {
            Err(crate::util::error::Errno(-val as c_int))
        } else {
            Ok(val as $t)
        }
    }};
}

#[cfg(feature = "std")]
macro_rules! impl_std_write {
    ($ty:ty) => {
        impl std::io::Write for $ty {
            fn write(&mut self, buf: &[u8]) -> std::result::Result<usize, std::io::Error> {
                Ok(Write::write(self, buf)?)
            }

            fn write_vectored(&mut self, bufs: &[std::io::IoSlice]) -> std::result::Result<usize, std::io::Error> {
                unsafe {
                    Ok(Write::gather_write(self, core::mem::transmute(bufs))?)
                }
            }

            fn flush(&mut self) -> std::result::Result<(), std::io::Error> {
                Ok(Write::flush(self)?)
            }
        }
    }
}

#[cfg(feature = "std")]
macro_rules! impl_std_read {
    ($ty:ty) => {
        impl std::io::Read for $ty {
            fn read(&mut self, buf: &mut [u8]) -> std::result::Result<usize, std::io::Error> {
                Ok(Read::read(self, crate::util::data::d8::from_byte_slice_mut(buf))?)
            }

            fn read_vectored(&mut self, bufs: &mut [std::io::IoSliceMut]) -> std::result::Result<usize, std::io::Error> {
                unsafe {
                    Ok(Read::scatter_read(self, core::mem::transmute(bufs))?)
                }
            }

            unsafe fn initializer(&self) -> std::io::Initializer {
                std::io::Initializer::nop()
            }
        }
    }
}

macro_rules! impl_fmt_write {
    ($ty:ty) => {
        impl core::fmt::Write for $ty {
            fn write_str(&mut self, s: &str) -> core::fmt::Result {
                self.write_all(s.as_bytes()).map(|_| ()).map_err(|_| core::fmt::Error)
            }
        }
    }
}

#[macro_export]
macro_rules! write {
    ($dst:expr, $($arg:tt)*) => ($dst.write_fmt_linux(format_args!($($arg)*)))
}

#[macro_export]
macro_rules! writeln {
    ($dst:expr) => (
        write!($dst, "\n")
    );
    ($dst:expr,) => (
        writeln!($dst)
    );
    ($dst:expr, $arg:expr) => (
        $dst.write_fmt_linux(format_args!(concat!($arg, "\n")))
    );
    ($dst:expr, $arg:expr, $($args:tt)*) => (
        $dst.write_fmt_linux(format_args!(concat!($arg, "\n"), $($args)*))
    );
}