diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:03:36 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:03:36 +0000 |
commit | 17d40c6057c88f4c432b0d7bac88e1b84cb7e67f (patch) | |
tree | 3f66c4a5918660bb8a758ab6cda5ff8ee4f6cdcd /vendor/either/src | |
parent | Adding upstream version 1.64.0+dfsg1. (diff) | |
download | rustc-17d40c6057c88f4c432b0d7bac88e1b84cb7e67f.tar.xz rustc-17d40c6057c88f4c432b0d7bac88e1b84cb7e67f.zip |
Adding upstream version 1.65.0+dfsg1.upstream/1.65.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/either/src')
-rw-r--r-- | vendor/either/src/lib.rs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/vendor/either/src/lib.rs b/vendor/either/src/lib.rs index a4f2a5454..9a271c351 100644 --- a/vendor/either/src/lib.rs +++ b/vendor/either/src/lib.rs @@ -26,9 +26,11 @@ pub mod serde_untagged_optional; use core::convert::{AsMut, AsRef}; use core::fmt; +use core::future::Future; use core::iter; use core::ops::Deref; use core::ops::DerefMut; +use core::pin::Pin; #[cfg(any(test, feature = "use_std"))] use std::error::Error; @@ -255,6 +257,35 @@ impl<L, R> Either<L, R> { } } + /// Convert `Pin<&Either<L, R>>` to `Either<Pin<&L>, Pin<&R>>`, + /// pinned projections of the inner variants. + pub fn as_pin_ref(self: Pin<&Self>) -> Either<Pin<&L>, Pin<&R>> { + // SAFETY: We can use `new_unchecked` because the `inner` parts are + // guaranteed to be pinned, as they come from `self` which is pinned. + unsafe { + match *Pin::get_ref(self) { + Left(ref inner) => Left(Pin::new_unchecked(inner)), + Right(ref inner) => Right(Pin::new_unchecked(inner)), + } + } + } + + /// Convert `Pin<&mut Either<L, R>>` to `Either<Pin<&mut L>, Pin<&mut R>>`, + /// pinned projections of the inner variants. + pub fn as_pin_mut(self: Pin<&mut Self>) -> Either<Pin<&mut L>, Pin<&mut R>> { + // SAFETY: `get_unchecked_mut` is fine because we don't move anything. + // We can use `new_unchecked` because the `inner` parts are guaranteed + // to be pinned, as they come from `self` which is pinned, and we never + // offer an unpinned `&mut L` or `&mut R` through `Pin<&mut Self>`. We + // also don't have an implementation of `Drop`, nor manual `Unpin`. + unsafe { + match *Pin::get_unchecked_mut(self) { + Left(ref mut inner) => Left(Pin::new_unchecked(inner)), + Right(ref mut inner) => Right(Pin::new_unchecked(inner)), + } + } + } + /// Convert `Either<L, R>` to `Either<R, L>`. /// /// ``` @@ -1038,6 +1069,22 @@ where { } +/// `Either<L, R>` is a future if both `L` and `R` are futures. +impl<L, R> Future for Either<L, R> +where + L: Future, + R: Future<Output = L::Output>, +{ + type Output = L::Output; + + fn poll( + self: Pin<&mut Self>, + cx: &mut core::task::Context<'_>, + ) -> core::task::Poll<Self::Output> { + for_both!(self.as_pin_mut(), inner => inner.poll(cx)) + } +} + #[cfg(any(test, feature = "use_std"))] /// `Either<L, R>` implements `Read` if both `L` and `R` do. /// @@ -1051,9 +1098,17 @@ where for_both!(*self, ref mut inner => inner.read(buf)) } + fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { + for_both!(*self, ref mut inner => inner.read_exact(buf)) + } + fn read_to_end(&mut self, buf: &mut std::vec::Vec<u8>) -> io::Result<usize> { for_both!(*self, ref mut inner => inner.read_to_end(buf)) } + + fn read_to_string(&mut self, buf: &mut std::string::String) -> io::Result<usize> { + for_both!(*self, ref mut inner => inner.read_to_string(buf)) + } } #[cfg(any(test, feature = "use_std"))] @@ -1084,6 +1139,14 @@ where fn consume(&mut self, amt: usize) { for_both!(*self, ref mut inner => inner.consume(amt)) } + + fn read_until(&mut self, byte: u8, buf: &mut std::vec::Vec<u8>) -> io::Result<usize> { + for_both!(*self, ref mut inner => inner.read_until(byte, buf)) + } + + fn read_line(&mut self, buf: &mut std::string::String) -> io::Result<usize> { + for_both!(*self, ref mut inner => inner.read_line(buf)) + } } #[cfg(any(test, feature = "use_std"))] @@ -1099,6 +1162,14 @@ where for_both!(*self, ref mut inner => inner.write(buf)) } + fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { + for_both!(*self, ref mut inner => inner.write_all(buf)) + } + + fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> { + for_both!(*self, ref mut inner => inner.write_fmt(fmt)) + } + fn flush(&mut self) -> io::Result<()> { for_both!(*self, ref mut inner => inner.flush()) } |