From 8cdfa445d6629ffef4cb84967ff7017654045bc2 Mon Sep 17 00:00:00 2001 From: mo khan Date: Wed, 2 Jul 2025 18:36:06 -0600 Subject: chore: add vendor directory --- vendor/hyper-util/src/common/exec.rs | 53 +++++++++++++ vendor/hyper-util/src/common/future.rs | 30 ++++++++ vendor/hyper-util/src/common/lazy.rs | 78 +++++++++++++++++++ vendor/hyper-util/src/common/mod.rs | 19 +++++ vendor/hyper-util/src/common/rewind.rs | 137 +++++++++++++++++++++++++++++++++ vendor/hyper-util/src/common/sync.rs | 67 ++++++++++++++++ vendor/hyper-util/src/common/timer.rs | 38 +++++++++ 7 files changed, 422 insertions(+) create mode 100644 vendor/hyper-util/src/common/exec.rs create mode 100644 vendor/hyper-util/src/common/future.rs create mode 100644 vendor/hyper-util/src/common/lazy.rs create mode 100644 vendor/hyper-util/src/common/mod.rs create mode 100644 vendor/hyper-util/src/common/rewind.rs create mode 100644 vendor/hyper-util/src/common/sync.rs create mode 100644 vendor/hyper-util/src/common/timer.rs (limited to 'vendor/hyper-util/src/common') 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 + Send>>; + +// Either the user provides an executor for background tasks, or we use +// `tokio::spawn`. +#[derive(Clone)] +pub(crate) enum Exec { + Executor(Arc + Send + Sync>), +} + +// ===== impl Exec ===== + +impl Exec { + pub(crate) fn new(inner: E) -> Self + where + E: Executor + Send + Sync + 'static, + { + Exec::Executor(Arc::new(inner)) + } + + pub(crate) fn execute(&self, fut: F) + where + F: Future + 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 hyper::rt::Executor for Exec +where + F: Future + 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(f: F) -> PollFn +where + F: FnMut(&mut Context<'_>) -> Poll, +{ + PollFn { f } +} + +pub(crate) struct PollFn { + f: F, +} + +impl Unpin for PollFn {} + +impl Future for PollFn +where + F: FnMut(&mut Context<'_>) -> Poll, +{ + type Output = T; + + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + (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(func: F) -> Lazy +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 { + #[pin] + inner: Inner, + } +} + +pin_project! { + #[project = InnerProj] + #[project_replace = InnerProjReplace] + enum Inner { + Init { func: F }, + Fut { #[pin] fut: R }, + Empty, + } +} + +impl Started for Lazy +where + F: FnOnce() -> R, + R: Future, +{ + fn started(&self) -> bool { + match self.inner { + Inner::Init { .. } => false, + Inner::Fut { .. } | Inner::Empty => true, + } + } +} + +impl Future for Lazy +where + F: FnOnce() -> R, + R: Future, +{ + type Output = R::Output; + + fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll { + 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 { + pub(crate) pre: Option, + pub(crate) inner: T, +} + +impl Rewind { + #[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 Read for Rewind +where + T: Read + Unpin, +{ + fn poll_read( + mut self: Pin<&mut Self>, + cx: &mut task::Context<'_>, + mut buf: ReadBufCursor<'_>, + ) -> Poll> { + 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 Write for Rewind +where + T: Write + Unpin, +{ + fn poll_write( + mut self: Pin<&mut Self>, + cx: &mut task::Context<'_>, + buf: &[u8], + ) -> Poll> { + 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> { + Pin::new(&mut self.inner).poll_write_vectored(cx, bufs) + } + + fn poll_flush(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll> { + Pin::new(&mut self.inner).poll_flush(cx) + } + + fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll> { + 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); + +impl SyncWrapper { + /// 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 Sync for SyncWrapper {} 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); + +// =====impl Timer===== +impl Timer { + pub(crate) fn new(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> { + self.0.sleep(duration) + } + + fn sleep_until(&self, deadline: Instant) -> Pin> { + self.0.sleep_until(deadline) + } +} -- cgit v1.2.3