From 80fe9b99b222ed8ca4d42b9130dd0c43e13c1f9d Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 28 Nov 2023 11:43:35 +1100 Subject: [PATCH] Move public macros to a separate module In preparation for inlining the `io` molule, move the public macros to a private `macros` module. Includes removal of the public re-export of `std` as `_std` - flaggin this because I do not understand why it is here in the first place, we can use `std::io::Write` in code that is feature gated on "std". --- io/src/lib.rs | 55 ++---------------------------------------------- io/src/macros.rs | 49 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 53 deletions(-) create mode 100644 io/src/macros.rs 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) + } + } + } +}