diff --git a/io/src/lib.rs b/io/src/lib.rs index 4c663061..c79e6f34 100644 --- a/io/src/lib.rs +++ b/io/src/lib.rs @@ -20,6 +20,8 @@ #[cfg(any(feature = "alloc", feature = "std"))] extern crate alloc; +mod macros; + /// Standard I/O stream definitions which are API-equivalent to `std`'s `io` module. See /// [`std::io`] for more info. pub mod io { @@ -350,56 +352,3 @@ pub mod io { /// Returns a sink to which all writes succeed. See [`std::io::sink`] for more info. pub fn sink() -> Sink { Sink } } - -#[doc(hidden)] -#[cfg(feature = "std")] -/// Re-export std for the below macro -pub use std as _std; - -#[macro_export] -/// Because we cannot provide a blanket implementation of [`std::io::Write`] for all implementers -/// of this crate's `io::Write` trait, we provide this macro instead. -/// -/// This macro will implement `Write` given a `write` and `flush` fn, either by implementing the -/// crate's native `io::Write` trait directly, or a more generic trait from `std` for users using -/// that feature. In any case, this crate's `io::Write` feature will be implemented for the given -/// type, even if indirectly. -#[cfg(not(feature = "std"))] -macro_rules! impl_write { - ($ty: ty, $write_fn: expr, $flush_fn: expr $(, $bounded_ty: ident : $bounds: path),*) => { - impl<$($bounded_ty: $bounds),*> $crate::io::Write for $ty { - #[inline] - fn write(&mut self, buf: &[u8]) -> $crate::io::Result { - $write_fn(self, buf) - } - #[inline] - fn flush(&mut self) -> $crate::io::Result<()> { - $flush_fn(self) - } - } - } -} - -#[macro_export] -/// Because we cannot provide a blanket implementation of [`std::io::Write`] for all implementers -/// of this crate's `io::Write` trait, we provide this macro instead. -/// -/// This macro will implement `Write` given a `write` and `flush` fn, either by implementing the -/// crate's native `io::Write` trait directly, or a more generic trait from `std` for users using -/// that feature. In any case, this crate's `io::Write` feature will be implemented for the given -/// type, even if indirectly. -#[cfg(feature = "std")] -macro_rules! impl_write { - ($ty: ty, $write_fn: expr, $flush_fn: expr $(, $bounded_ty: ident : $bounds: path),*) => { - impl<$($bounded_ty: $bounds),*> $crate::_std::io::Write for $ty { - #[inline] - fn write(&mut self, buf: &[u8]) -> $crate::_std::io::Result { - $write_fn(self, buf) - } - #[inline] - fn flush(&mut self) -> $crate::_std::io::Result<()> { - $flush_fn(self) - } - } - } -} diff --git a/io/src/macros.rs b/io/src/macros.rs new file mode 100644 index 00000000..a3714188 --- /dev/null +++ b/io/src/macros.rs @@ -0,0 +1,49 @@ +//! Public macros for porvide.d for users to be able implement our `io::Write` trait. + +#[macro_export] +/// Because we cannot provide a blanket implementation of [`std::io::Write`] for all implementers +/// of this crate's `io::Write` trait, we provide this macro instead. +/// +/// This macro will implement `Write` given a `write` and `flush` fn, either by implementing the +/// crate's native `io::Write` trait directly, or a more generic trait from `std` for users using +/// that feature. In any case, this crate's `io::Write` feature will be implemented for the given +/// type, even if indirectly. +#[cfg(not(feature = "std"))] +macro_rules! impl_write { + ($ty: ty, $write_fn: expr, $flush_fn: expr $(, $bounded_ty: ident : $bounds: path),*) => { + impl<$($bounded_ty: $bounds),*> $crate::io::Write for $ty { + #[inline] + fn write(&mut self, buf: &[u8]) -> $crate::io::Result { + $write_fn(self, buf) + } + #[inline] + fn flush(&mut self) -> $crate::io::Result<()> { + $flush_fn(self) + } + } + } +} + +#[macro_export] +/// Because we cannot provide a blanket implementation of [`std::io::Write`] for all implementers +/// of this crate's `io::Write` trait, we provide this macro instead. +/// +/// This macro will implement `Write` given a `write` and `flush` fn, either by implementing the +/// crate's native `io::Write` trait directly, or a more generic trait from `std` for users using +/// that feature. In any case, this crate's `io::Write` feature will be implemented for the given +/// type, even if indirectly. +#[cfg(feature = "std")] +macro_rules! impl_write { + ($ty: ty, $write_fn: expr, $flush_fn: expr $(, $bounded_ty: ident : $bounds: path),*) => { + impl<$($bounded_ty: $bounds),*> std::io::Write for $ty { + #[inline] + fn write(&mut self, buf: &[u8]) -> std::io::Result { + $write_fn(self, buf) + } + #[inline] + fn flush(&mut self) -> std::io::Result<()> { + $flush_fn(self) + } + } + } +}