summaryrefslogtreecommitdiff
path: root/vendor/hyper-util/src/common
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-07-02 18:36:06 -0600
committermo khan <mo@mokhan.ca>2025-07-02 18:36:06 -0600
commit8cdfa445d6629ffef4cb84967ff7017654045bc2 (patch)
tree22f0b0907c024c78d26a731e2e1f5219407d8102 /vendor/hyper-util/src/common
parent4351c74c7c5f97156bc94d3a8549b9940ac80e3f (diff)
chore: add vendor directory
Diffstat (limited to 'vendor/hyper-util/src/common')
-rw-r--r--vendor/hyper-util/src/common/exec.rs53
-rw-r--r--vendor/hyper-util/src/common/future.rs30
-rw-r--r--vendor/hyper-util/src/common/lazy.rs78
-rw-r--r--vendor/hyper-util/src/common/mod.rs19
-rw-r--r--vendor/hyper-util/src/common/rewind.rs137
-rw-r--r--vendor/hyper-util/src/common/sync.rs67
-rw-r--r--vendor/hyper-util/src/common/timer.rs38
7 files changed, 422 insertions, 0 deletions
diff --git a/vendor/hyper-util/src/common/exec.rs b/vendor/hyper-util/src/common/exec.rs
new file mode 100644
index 00000000..40860ee1
--- /dev/null
+++ b/vendor/hyper-util/src/common/exec.rs
@@ -0,0 +1,53 @@
+#![allow(dead_code)]
+
+use hyper::rt::Executor;
+use std::fmt;
+use std::future::Future;
+use std::pin::Pin;
+use std::sync::Arc;
+
+pub(crate) type BoxSendFuture = Pin<Box<dyn Future<Output = ()> + Send>>;
+
+// Either the user provides an executor for background tasks, or we use
+// `tokio::spawn`.
+#[derive(Clone)]
+pub(crate) enum Exec {
+ Executor(Arc<dyn Executor<BoxSendFuture> + Send + Sync>),
+}
+
+// ===== impl Exec =====
+
+impl Exec {
+ pub(crate) fn new<E>(inner: E) -> Self
+ where
+ E: Executor<BoxSendFuture> + Send + Sync + 'static,
+ {
+ Exec::Executor(Arc::new(inner))
+ }
+
+ pub(crate) fn execute<F>(&self, fut: F)
+ where
+ F: Future<Output = ()> + Send + 'static,
+ {
+ match *self {
+ Exec::Executor(ref e) => {
+ e.execute(Box::pin(fut));
+ }
+ }
+ }
+}
+
+impl fmt::Debug for Exec {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("Exec").finish()
+ }
+}
+
+impl<F> hyper::rt::Executor<F> for Exec
+where
+ F: Future<Output = ()> + Send + 'static,
+{
+ fn execute(&self, fut: F) {
+ Exec::execute(self, fut);
+ }
+}
diff --git a/vendor/hyper-util/src/common/future.rs b/vendor/hyper-util/src/common/future.rs
new file mode 100644
index 00000000..47897f24
--- /dev/null
+++ b/vendor/hyper-util/src/common/future.rs
@@ -0,0 +1,30 @@
+use std::{
+ future::Future,
+ pin::Pin,
+ task::{Context, Poll},
+};
+
+// TODO: replace with `std::future::poll_fn` once MSRV >= 1.64
+pub(crate) fn poll_fn<T, F>(f: F) -> PollFn<F>
+where
+ F: FnMut(&mut Context<'_>) -> Poll<T>,
+{
+ PollFn { f }
+}
+
+pub(crate) struct PollFn<F> {
+ f: F,
+}
+
+impl<F> Unpin for PollFn<F> {}
+
+impl<T, F> Future for PollFn<F>
+where
+ F: FnMut(&mut Context<'_>) -> Poll<T>,
+{
+ type Output = T;
+
+ fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
+ (self.f)(cx)
+ }
+}
diff --git a/vendor/hyper-util/src/common/lazy.rs b/vendor/hyper-util/src/common/lazy.rs
new file mode 100644
index 00000000..7ec09bbe
--- /dev/null
+++ b/vendor/hyper-util/src/common/lazy.rs
@@ -0,0 +1,78 @@
+use pin_project_lite::pin_project;
+
+use std::future::Future;
+use std::pin::Pin;
+use std::task::{self, Poll};
+
+pub(crate) trait Started: Future {
+ fn started(&self) -> bool;
+}
+
+pub(crate) fn lazy<F, R>(func: F) -> Lazy<F, R>
+where
+ F: FnOnce() -> R,
+ R: Future + Unpin,
+{
+ Lazy {
+ inner: Inner::Init { func },
+ }
+}
+
+// FIXME: allow() required due to `impl Trait` leaking types to this lint
+pin_project! {
+ #[allow(missing_debug_implementations)]
+ pub(crate) struct Lazy<F, R> {
+ #[pin]
+ inner: Inner<F, R>,
+ }
+}
+
+pin_project! {
+ #[project = InnerProj]
+ #[project_replace = InnerProjReplace]
+ enum Inner<F, R> {
+ Init { func: F },
+ Fut { #[pin] fut: R },
+ Empty,
+ }
+}
+
+impl<F, R> Started for Lazy<F, R>
+where
+ F: FnOnce() -> R,
+ R: Future,
+{
+ fn started(&self) -> bool {
+ match self.inner {
+ Inner::Init { .. } => false,
+ Inner::Fut { .. } | Inner::Empty => true,
+ }
+ }
+}
+
+impl<F, R> Future for Lazy<F, R>
+where
+ F: FnOnce() -> R,
+ R: Future,
+{
+ type Output = R::Output;
+
+ fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
+ let mut this = self.project();
+
+ if let InnerProj::Fut { fut } = this.inner.as_mut().project() {
+ return fut.poll(cx);
+ }
+
+ match this.inner.as_mut().project_replace(Inner::Empty) {
+ InnerProjReplace::Init { func } => {
+ this.inner.set(Inner::Fut { fut: func() });
+ if let InnerProj::Fut { fut } = this.inner.project() {
+ return fut.poll(cx);
+ }
+ unreachable!()
+ }
+ _ => unreachable!("lazy state wrong"),
+ }
+ }
+}
diff --git a/vendor/hyper-util/src/common/mod.rs b/vendor/hyper-util/src/common/mod.rs
new file mode 100644
index 00000000..b45cd0b2
--- /dev/null
+++ b/vendor/hyper-util/src/common/mod.rs
@@ -0,0 +1,19 @@
+#![allow(missing_docs)]
+
+pub(crate) mod exec;
+#[cfg(feature = "client")]
+mod lazy;
+pub(crate) mod rewind;
+#[cfg(feature = "client")]
+mod sync;
+pub(crate) mod timer;
+
+#[cfg(feature = "client")]
+pub(crate) use exec::Exec;
+
+#[cfg(feature = "client")]
+pub(crate) use lazy::{lazy, Started as Lazy};
+#[cfg(feature = "client")]
+pub(crate) use sync::SyncWrapper;
+
+pub(crate) mod future;
diff --git a/vendor/hyper-util/src/common/rewind.rs b/vendor/hyper-util/src/common/rewind.rs
new file mode 100644
index 00000000..760d7966
--- /dev/null
+++ b/vendor/hyper-util/src/common/rewind.rs
@@ -0,0 +1,137 @@
+use std::{cmp, io};
+
+use bytes::{Buf, Bytes};
+use hyper::rt::{Read, ReadBufCursor, Write};
+
+use std::{
+ pin::Pin,
+ task::{self, Poll},
+};
+
+/// Combine a buffer with an IO, rewinding reads to use the buffer.
+#[derive(Debug)]
+pub(crate) struct Rewind<T> {
+ pub(crate) pre: Option<Bytes>,
+ pub(crate) inner: T,
+}
+
+impl<T> Rewind<T> {
+ #[cfg(all(feature = "server", any(feature = "http1", feature = "http2")))]
+ pub(crate) fn new_buffered(io: T, buf: Bytes) -> Self {
+ Rewind {
+ pre: Some(buf),
+ inner: io,
+ }
+ }
+}
+
+impl<T> Read for Rewind<T>
+where
+ T: Read + Unpin,
+{
+ fn poll_read(
+ mut self: Pin<&mut Self>,
+ cx: &mut task::Context<'_>,
+ mut buf: ReadBufCursor<'_>,
+ ) -> Poll<io::Result<()>> {
+ if let Some(mut prefix) = self.pre.take() {
+ // If there are no remaining bytes, let the bytes get dropped.
+ if !prefix.is_empty() {
+ let copy_len = cmp::min(prefix.len(), buf.remaining());
+ buf.put_slice(&prefix[..copy_len]);
+ prefix.advance(copy_len);
+ // Put back what's left
+ if !prefix.is_empty() {
+ self.pre = Some(prefix);
+ }
+
+ return Poll::Ready(Ok(()));
+ }
+ }
+ Pin::new(&mut self.inner).poll_read(cx, buf)
+ }
+}
+
+impl<T> Write for Rewind<T>
+where
+ T: Write + Unpin,
+{
+ fn poll_write(
+ mut self: Pin<&mut Self>,
+ cx: &mut task::Context<'_>,
+ buf: &[u8],
+ ) -> Poll<io::Result<usize>> {
+ Pin::new(&mut self.inner).poll_write(cx, buf)
+ }
+
+ fn poll_write_vectored(
+ mut self: Pin<&mut Self>,
+ cx: &mut task::Context<'_>,
+ bufs: &[io::IoSlice<'_>],
+ ) -> Poll<io::Result<usize>> {
+ Pin::new(&mut self.inner).poll_write_vectored(cx, bufs)
+ }
+
+ fn poll_flush(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<io::Result<()>> {
+ Pin::new(&mut self.inner).poll_flush(cx)
+ }
+
+ fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<io::Result<()>> {
+ Pin::new(&mut self.inner).poll_shutdown(cx)
+ }
+
+ fn is_write_vectored(&self) -> bool {
+ self.inner.is_write_vectored()
+ }
+}
+
+/*
+#[cfg(test)]
+mod tests {
+ use super::Rewind;
+ use bytes::Bytes;
+ use tokio::io::AsyncReadExt;
+
+ #[cfg(not(miri))]
+ #[tokio::test]
+ async fn partial_rewind() {
+ let underlying = [104, 101, 108, 108, 111];
+
+ let mock = tokio_test::io::Builder::new().read(&underlying).build();
+
+ let mut stream = Rewind::new(mock);
+
+ // Read off some bytes, ensure we filled o1
+ let mut buf = [0; 2];
+ stream.read_exact(&mut buf).await.expect("read1");
+
+ // Rewind the stream so that it is as if we never read in the first place.
+ stream.rewind(Bytes::copy_from_slice(&buf[..]));
+
+ let mut buf = [0; 5];
+ stream.read_exact(&mut buf).await.expect("read1");
+
+ // At this point we should have read everything that was in the MockStream
+ assert_eq!(&buf, &underlying);
+ }
+
+ #[cfg(not(miri))]
+ #[tokio::test]
+ async fn full_rewind() {
+ let underlying = [104, 101, 108, 108, 111];
+
+ let mock = tokio_test::io::Builder::new().read(&underlying).build();
+
+ let mut stream = Rewind::new(mock);
+
+ let mut buf = [0; 5];
+ stream.read_exact(&mut buf).await.expect("read1");
+
+ // Rewind the stream so that it is as if we never read in the first place.
+ stream.rewind(Bytes::copy_from_slice(&buf[..]));
+
+ let mut buf = [0; 5];
+ stream.read_exact(&mut buf).await.expect("read1");
+ }
+}
+*/
diff --git a/vendor/hyper-util/src/common/sync.rs b/vendor/hyper-util/src/common/sync.rs
new file mode 100644
index 00000000..2755fd05
--- /dev/null
+++ b/vendor/hyper-util/src/common/sync.rs
@@ -0,0 +1,67 @@
+pub(crate) struct SyncWrapper<T>(T);
+
+impl<T> SyncWrapper<T> {
+ /// Creates a new SyncWrapper containing the given value.
+ ///
+ /// # Examples
+ ///
+ /// ```ignore
+ /// use hyper::common::sync_wrapper::SyncWrapper;
+ ///
+ /// let wrapped = SyncWrapper::new(42);
+ /// ```
+ pub(crate) fn new(value: T) -> Self {
+ Self(value)
+ }
+
+ /// Acquires a reference to the protected value.
+ ///
+ /// This is safe because it requires an exclusive reference to the wrapper. Therefore this method
+ /// neither panics nor does it return an error. This is in contrast to [`Mutex::get_mut`] which
+ /// returns an error if another thread panicked while holding the lock. It is not recommended
+ /// to send an exclusive reference to a potentially damaged value to another thread for further
+ /// processing.
+ ///
+ /// [`Mutex::get_mut`]: https://doc.rust-lang.org/std/sync/struct.Mutex.html#method.get_mut
+ ///
+ /// # Examples
+ ///
+ /// ```ignore
+ /// use hyper::common::sync_wrapper::SyncWrapper;
+ ///
+ /// let mut wrapped = SyncWrapper::new(42);
+ /// let value = wrapped.get_mut();
+ /// *value = 0;
+ /// assert_eq!(*wrapped.get_mut(), 0);
+ /// ```
+ pub(crate) fn get_mut(&mut self) -> &mut T {
+ &mut self.0
+ }
+
+ /// Consumes this wrapper, returning the underlying data.
+ ///
+ /// This is safe because it requires ownership of the wrapper, aherefore this method will neither
+ /// panic nor does it return an error. This is in contrast to [`Mutex::into_inner`] which
+ /// returns an error if another thread panicked while holding the lock. It is not recommended
+ /// to send an exclusive reference to a potentially damaged value to another thread for further
+ /// processing.
+ ///
+ /// [`Mutex::into_inner`]: https://doc.rust-lang.org/std/sync/struct.Mutex.html#method.into_inner
+ ///
+ /// # Examples
+ ///
+ /// ```ignore
+ /// use hyper::common::sync_wrapper::SyncWrapper;
+ ///
+ /// let mut wrapped = SyncWrapper::new(42);
+ /// assert_eq!(wrapped.into_inner(), 42);
+ /// ```
+ #[allow(dead_code)]
+ pub(crate) fn into_inner(self) -> T {
+ self.0
+ }
+}
+
+// this is safe because the only operations permitted on this data structure require exclusive
+// access or ownership
+unsafe impl<T: Send> Sync for SyncWrapper<T> {}
diff --git a/vendor/hyper-util/src/common/timer.rs b/vendor/hyper-util/src/common/timer.rs
new file mode 100644
index 00000000..390be3b0
--- /dev/null
+++ b/vendor/hyper-util/src/common/timer.rs
@@ -0,0 +1,38 @@
+#![allow(dead_code)]
+
+use std::fmt;
+use std::pin::Pin;
+use std::sync::Arc;
+use std::time::Duration;
+use std::time::Instant;
+
+use hyper::rt::Sleep;
+
+#[derive(Clone)]
+pub(crate) struct Timer(Arc<dyn hyper::rt::Timer + Send + Sync>);
+
+// =====impl Timer=====
+impl Timer {
+ pub(crate) fn new<T>(inner: T) -> Self
+ where
+ T: hyper::rt::Timer + Send + Sync + 'static,
+ {
+ Self(Arc::new(inner))
+ }
+}
+
+impl fmt::Debug for Timer {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("Timer").finish()
+ }
+}
+
+impl hyper::rt::Timer for Timer {
+ fn sleep(&self, duration: Duration) -> Pin<Box<dyn Sleep>> {
+ self.0.sleep(duration)
+ }
+
+ fn sleep_until(&self, deadline: Instant) -> Pin<Box<dyn Sleep>> {
+ self.0.sleep_until(deadline)
+ }
+}