diff --git a/crates/anstream/src/stream.rs b/crates/anstream/src/stream.rs index 844ccec..b45be04 100644 --- a/crates/anstream/src/stream.rs +++ b/crates/anstream/src/stream.rs @@ -142,6 +142,30 @@ pub trait AsLockedWrite: private::Sealed { fn as_locked_write(&mut self) -> Self::Write<'_>; } +impl AsLockedWrite for &mut T { + type Write<'w> + = T::Write<'w> + where + Self: 'w; + + #[inline] + fn as_locked_write(&mut self) -> Self::Write<'_> { + (**self).as_locked_write() + } +} + +impl AsLockedWrite for Box { + type Write<'w> + = T::Write<'w> + where + Self: 'w; + + #[inline] + fn as_locked_write(&mut self) -> Self::Write<'_> { + (**self).as_locked_write() + } +} + impl AsLockedWrite for std::io::Stdout { type Write<'w> = std::io::StdoutLock<'w>; @@ -187,6 +211,24 @@ impl AsLockedWrite for dyn std::io::Write { } } +impl AsLockedWrite for dyn std::io::Write + Send { + type Write<'w> = &'w mut Self; + + #[inline] + fn as_locked_write(&mut self) -> Self::Write<'_> { + self + } +} + +impl AsLockedWrite for dyn std::io::Write + Send + Sync { + type Write<'w> = &'w mut Self; + + #[inline] + fn as_locked_write(&mut self) -> Self::Write<'_> { + self + } +} + impl AsLockedWrite for Vec { type Write<'w> = &'w mut Self; @@ -241,3 +283,33 @@ mod private { #[allow(deprecated)] impl Sealed for crate::Buffer {} } + +#[cfg(test)] +mod tests { + use super::*; + + fn assert_raw_stream() + where + crate::AutoStream: std::io::Write, + { + } + + #[test] + fn test() { + assert_raw_stream::>(); + assert_raw_stream::>(); + assert_raw_stream::>(); + assert_raw_stream::>(); + + assert_raw_stream::<&mut (dyn std::io::Write)>(); + assert_raw_stream::<&mut (dyn std::io::Write + 'static)>(); + assert_raw_stream::<&mut (dyn std::io::Write + Send)>(); + assert_raw_stream::<&mut (dyn std::io::Write + Send + Sync)>(); + + assert_raw_stream::>(); + assert_raw_stream::<&mut Vec>(); + + assert_raw_stream::(); + assert_raw_stream::<&mut std::fs::File>(); + } +}