bitcoin_io/
macros.rs

1//! Public macros for porvide.d for users to be able implement our `io::Write` trait.
2
3#[macro_export]
4/// Because we cannot provide a blanket implementation of [`std::io::Write`] for all implementers
5/// of this crate's `io::Write` trait, we provide this macro instead.
6///
7/// This macro will implement `Write` given a `write` and `flush` fn, either by implementing the
8/// crate's native `io::Write` trait directly, or a more generic trait from `std` for users using
9/// that feature. In any case, this crate's `io::Write` feature will be implemented for the given
10/// type, even if indirectly.
11#[cfg(not(feature = "std"))]
12macro_rules! impl_write {
13    ($ty: ty, $write_fn: expr, $flush_fn: expr $(, $bounded_ty: ident : $bounds: path),*) => {
14        impl<$($bounded_ty: $bounds),*> $crate::Write for $ty {
15            #[inline]
16            fn write(&mut self, buf: &[u8]) -> $crate::Result<usize> {
17                $write_fn(self, buf)
18            }
19            #[inline]
20            fn flush(&mut self) -> $crate::Result<()> {
21                $flush_fn(self)
22            }
23        }
24    }
25}
26
27#[macro_export]
28/// Because we cannot provide a blanket implementation of [`std::io::Write`] for all implementers
29/// of this crate's `io::Write` trait, we provide this macro instead.
30///
31/// This macro will implement `Write` given a `write` and `flush` fn, either by implementing the
32/// crate's native `io::Write` trait directly, or a more generic trait from `std` for users using
33/// that feature. In any case, this crate's `io::Write` feature will be implemented for the given
34/// type, even if indirectly.
35#[cfg(feature = "std")]
36macro_rules! impl_write {
37    ($ty: ty, $write_fn: expr, $flush_fn: expr $(, $bounded_ty: ident : $bounds: path),*) => {
38        impl<$($bounded_ty: $bounds),*> std::io::Write for $ty {
39            #[inline]
40            fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
41                $write_fn(self, buf)
42            }
43            #[inline]
44            fn flush(&mut self) -> std::io::Result<()> {
45                $flush_fn(self)
46            }
47        }
48
49        impl<$($bounded_ty: $bounds),*> $crate::Write for $ty {
50            #[inline]
51            fn write(&mut self, buf: &[u8]) -> $crate::Result<usize> {
52                $write_fn(self, buf)
53            }
54            #[inline]
55            fn flush(&mut self) -> $crate::Result<()> {
56                $flush_fn(self)
57            }
58        }
59    }
60}