Vendor asic-rs 0.8.1 (280 crates, offline builds) + wxWidgets 3.3.3 (pristine + patch queue); UI spec + ADR-0003; M1 on asic-rs
This commit is contained in:
+122
@@ -0,0 +1,122 @@
|
||||
use crate::BodyExt as _;
|
||||
|
||||
use bytes::Buf;
|
||||
use http_body::{Body, Frame, SizeHint};
|
||||
use std::{
|
||||
fmt,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
/// A boxed [`Body`] trait object.
|
||||
pub struct BoxBody<D, E> {
|
||||
inner: Pin<Box<dyn Body<Data = D, Error = E> + Send + Sync + 'static>>,
|
||||
}
|
||||
|
||||
/// A boxed [`Body`] trait object that is !Sync.
|
||||
pub struct UnsyncBoxBody<D, E> {
|
||||
inner: Pin<Box<dyn Body<Data = D, Error = E> + Send + 'static>>,
|
||||
}
|
||||
|
||||
impl<D, E> BoxBody<D, E> {
|
||||
/// Create a new `BoxBody`.
|
||||
pub fn new<B>(body: B) -> Self
|
||||
where
|
||||
B: Body<Data = D, Error = E> + Send + Sync + 'static,
|
||||
D: Buf,
|
||||
{
|
||||
Self {
|
||||
inner: Box::pin(body),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<D, E> fmt::Debug for BoxBody<D, E> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("BoxBody").finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<D, E> Body for BoxBody<D, E>
|
||||
where
|
||||
D: Buf,
|
||||
{
|
||||
type Data = D;
|
||||
type Error = E;
|
||||
|
||||
fn poll_frame(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
|
||||
self.inner.as_mut().poll_frame(cx)
|
||||
}
|
||||
|
||||
fn is_end_stream(&self) -> bool {
|
||||
self.inner.is_end_stream()
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> SizeHint {
|
||||
self.inner.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<D, E> Default for BoxBody<D, E>
|
||||
where
|
||||
D: Buf + 'static,
|
||||
{
|
||||
fn default() -> Self {
|
||||
BoxBody::new(crate::Empty::new().map_err(|err| match err {}))
|
||||
}
|
||||
}
|
||||
|
||||
// === UnsyncBoxBody ===
|
||||
impl<D, E> UnsyncBoxBody<D, E> {
|
||||
/// Create a new `UnsyncBoxBody`.
|
||||
pub fn new<B>(body: B) -> Self
|
||||
where
|
||||
B: Body<Data = D, Error = E> + Send + 'static,
|
||||
D: Buf,
|
||||
{
|
||||
Self {
|
||||
inner: Box::pin(body),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<D, E> fmt::Debug for UnsyncBoxBody<D, E> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("UnsyncBoxBody").finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<D, E> Body for UnsyncBoxBody<D, E>
|
||||
where
|
||||
D: Buf,
|
||||
{
|
||||
type Data = D;
|
||||
type Error = E;
|
||||
|
||||
fn poll_frame(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
|
||||
self.inner.as_mut().poll_frame(cx)
|
||||
}
|
||||
|
||||
fn is_end_stream(&self) -> bool {
|
||||
self.inner.is_end_stream()
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> SizeHint {
|
||||
self.inner.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<D, E> Default for UnsyncBoxBody<D, E>
|
||||
where
|
||||
D: Buf + 'static,
|
||||
{
|
||||
fn default() -> Self {
|
||||
UnsyncBoxBody::new(crate::Empty::new().map_err(|err| match err {}))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
use std::{
|
||||
future::Future,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
use futures_core::ready;
|
||||
use http_body::Body;
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
/// Future that resolves into a [`Collected`].
|
||||
///
|
||||
/// [`Collected`]: crate::Collected
|
||||
pub struct Collect<T>
|
||||
where
|
||||
T: Body,
|
||||
T: ?Sized,
|
||||
{
|
||||
pub(crate) collected: Option<crate::Collected<T::Data>>,
|
||||
#[pin]
|
||||
pub(crate) body: T,
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Body + ?Sized> Future for Collect<T> {
|
||||
type Output = Result<crate::Collected<T::Data>, T::Error>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> std::task::Poll<Self::Output> {
|
||||
let mut me = self.project();
|
||||
|
||||
loop {
|
||||
let frame = ready!(me.body.as_mut().poll_frame(cx));
|
||||
|
||||
let frame = if let Some(frame) = frame {
|
||||
frame?
|
||||
} else {
|
||||
return Poll::Ready(Ok(me.collected.take().expect("polled after complete")));
|
||||
};
|
||||
|
||||
me.collected.as_mut().unwrap().push_frame(frame);
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
use http_body::Body;
|
||||
|
||||
use core::future::Future;
|
||||
use core::pin::Pin;
|
||||
use core::task;
|
||||
|
||||
#[must_use = "futures don't do anything unless polled"]
|
||||
#[derive(Debug)]
|
||||
/// Future that resolves to the next frame from a [`Body`].
|
||||
pub struct Frame<'a, T: ?Sized>(pub(crate) &'a mut T);
|
||||
|
||||
impl<T: Body + Unpin + ?Sized> Future for Frame<'_, T> {
|
||||
type Output = Option<Result<http_body::Frame<T::Data>, T::Error>>;
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, ctx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
|
||||
Pin::new(&mut self.0).poll_frame(ctx)
|
||||
}
|
||||
}
|
||||
+232
@@ -0,0 +1,232 @@
|
||||
use std::{
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
use http_body::{Body, Frame, SizeHint};
|
||||
|
||||
/// A "fused" [`Body`].
|
||||
///
|
||||
/// This [`Body`] yields `Poll::Ready(None)` forever after the underlying body yields
|
||||
/// `Poll::Ready(None)`, or an error `Poll::Ready(Some(Err(_)))`, once.
|
||||
///
|
||||
/// Bodies should ideally continue to return `Poll::Ready(None)` indefinitely after the end of
|
||||
/// the stream is reached. [`Fuse<B>`] avoids polling its underlying body `B` further after the
|
||||
/// underlying stream as ended, which can be useful for implementation that cannot uphold this
|
||||
/// guarantee.
|
||||
///
|
||||
/// This is akin to the functionality that [`std::iter::Iterator::fuse()`] provides for
|
||||
/// [`Iterator`][std::iter::Iterator]s.
|
||||
#[derive(Debug)]
|
||||
pub struct Fuse<B> {
|
||||
inner: Option<B>,
|
||||
}
|
||||
|
||||
impl<B> Fuse<B>
|
||||
where
|
||||
B: Body,
|
||||
{
|
||||
/// Returns a fused body.
|
||||
pub fn new(body: B) -> Self {
|
||||
Self {
|
||||
inner: if body.is_end_stream() {
|
||||
None
|
||||
} else {
|
||||
Some(body)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<B> Body for Fuse<B>
|
||||
where
|
||||
B: Body + Unpin,
|
||||
{
|
||||
type Data = B::Data;
|
||||
type Error = B::Error;
|
||||
|
||||
fn poll_frame(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<Option<Result<Frame<B::Data>, B::Error>>> {
|
||||
let Self { inner } = self.get_mut();
|
||||
|
||||
let poll = inner
|
||||
.as_mut()
|
||||
.map(|mut inner| match Pin::new(&mut inner).poll_frame(cx) {
|
||||
frame @ Poll::Ready(Some(Ok(_))) => (frame, inner.is_end_stream()),
|
||||
end @ Poll::Ready(Some(Err(_)) | None) => (end, true),
|
||||
poll @ Poll::Pending => (poll, false),
|
||||
});
|
||||
|
||||
if let Some((frame, eos)) = poll {
|
||||
eos.then(|| inner.take());
|
||||
frame
|
||||
} else {
|
||||
Poll::Ready(None)
|
||||
}
|
||||
}
|
||||
|
||||
fn is_end_stream(&self) -> bool {
|
||||
self.inner.is_none()
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> SizeHint {
|
||||
self.inner
|
||||
.as_ref()
|
||||
.map(B::size_hint)
|
||||
.unwrap_or_else(|| SizeHint::with_exact(0))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use bytes::Bytes;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
/// A value returned by a call to [`Body::poll_frame()`].
|
||||
type PollFrame = Poll<Option<Result<Frame<Bytes>, Error>>>;
|
||||
|
||||
type Error = &'static str;
|
||||
|
||||
struct Mock<'count> {
|
||||
poll_count: &'count mut u8,
|
||||
polls: VecDeque<PollFrame>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_never_polls() {
|
||||
let mut count = 0_u8;
|
||||
let empty = Mock::new(&mut count, []);
|
||||
debug_assert!(empty.is_end_stream());
|
||||
let fused = Fuse::new(empty);
|
||||
assert!(fused.inner.is_none());
|
||||
drop(fused);
|
||||
assert_eq!(count, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stops_polling_after_none() {
|
||||
let mut count = 0_u8;
|
||||
let empty = Mock::new(&mut count, [Poll::Ready(None)]);
|
||||
debug_assert!(!empty.is_end_stream());
|
||||
let mut fused = Fuse::new(empty);
|
||||
assert!(fused.inner.is_some());
|
||||
|
||||
let waker = futures_util::task::noop_waker();
|
||||
let mut cx = Context::from_waker(&waker);
|
||||
match Pin::new(&mut fused).poll_frame(&mut cx) {
|
||||
Poll::Ready(None) => {}
|
||||
other => panic!("unexpected poll outcome: {:?}", other),
|
||||
}
|
||||
|
||||
assert!(fused.inner.is_none());
|
||||
match Pin::new(&mut fused).poll_frame(&mut cx) {
|
||||
Poll::Ready(None) => {}
|
||||
other => panic!("unexpected poll outcome: {:?}", other),
|
||||
}
|
||||
|
||||
drop(fused);
|
||||
assert_eq!(count, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stops_polling_after_some_eos() {
|
||||
let mut count = 0_u8;
|
||||
let body = Mock::new(
|
||||
&mut count,
|
||||
[Poll::Ready(Some(Ok(Frame::data(Bytes::from_static(
|
||||
b"hello",
|
||||
)))))],
|
||||
);
|
||||
debug_assert!(!body.is_end_stream());
|
||||
let mut fused = Fuse::new(body);
|
||||
assert!(fused.inner.is_some());
|
||||
|
||||
let waker = futures_util::task::noop_waker();
|
||||
let mut cx = Context::from_waker(&waker);
|
||||
|
||||
match Pin::new(&mut fused).poll_frame(&mut cx) {
|
||||
Poll::Ready(Some(Ok(bytes))) => assert_eq!(bytes.into_data().expect("data"), "hello"),
|
||||
other => panic!("unexpected poll outcome: {:?}", other),
|
||||
}
|
||||
|
||||
assert!(fused.inner.is_none());
|
||||
match Pin::new(&mut fused).poll_frame(&mut cx) {
|
||||
Poll::Ready(None) => {}
|
||||
other => panic!("unexpected poll outcome: {:?}", other),
|
||||
}
|
||||
|
||||
drop(fused);
|
||||
assert_eq!(count, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stops_polling_after_some_error() {
|
||||
let mut count = 0_u8;
|
||||
let body = Mock::new(
|
||||
&mut count,
|
||||
[
|
||||
Poll::Ready(Some(Ok(Frame::data(Bytes::from_static(b"hello"))))),
|
||||
Poll::Ready(Some(Err("oh no"))),
|
||||
Poll::Ready(Some(Ok(Frame::data(Bytes::from_static(b"world"))))),
|
||||
],
|
||||
);
|
||||
debug_assert!(!body.is_end_stream());
|
||||
let mut fused = Fuse::new(body);
|
||||
assert!(fused.inner.is_some());
|
||||
|
||||
let waker = futures_util::task::noop_waker();
|
||||
let mut cx = Context::from_waker(&waker);
|
||||
|
||||
match Pin::new(&mut fused).poll_frame(&mut cx) {
|
||||
Poll::Ready(Some(Ok(bytes))) => assert_eq!(bytes.into_data().expect("data"), "hello"),
|
||||
other => panic!("unexpected poll outcome: {:?}", other),
|
||||
}
|
||||
|
||||
assert!(fused.inner.is_some());
|
||||
match Pin::new(&mut fused).poll_frame(&mut cx) {
|
||||
Poll::Ready(Some(Err("oh no"))) => {}
|
||||
other => panic!("unexpected poll outcome: {:?}", other),
|
||||
}
|
||||
|
||||
assert!(fused.inner.is_none());
|
||||
match Pin::new(&mut fused).poll_frame(&mut cx) {
|
||||
Poll::Ready(None) => {}
|
||||
other => panic!("unexpected poll outcome: {:?}", other),
|
||||
}
|
||||
|
||||
drop(fused);
|
||||
assert_eq!(count, 2);
|
||||
}
|
||||
|
||||
// === impl Mock ===
|
||||
|
||||
impl<'count> Mock<'count> {
|
||||
fn new(poll_count: &'count mut u8, polls: impl IntoIterator<Item = PollFrame>) -> Self {
|
||||
Self {
|
||||
poll_count,
|
||||
polls: polls.into_iter().collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Body for Mock<'_> {
|
||||
type Data = Bytes;
|
||||
type Error = &'static str;
|
||||
|
||||
fn poll_frame(
|
||||
self: Pin<&mut Self>,
|
||||
_cx: &mut Context<'_>,
|
||||
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
|
||||
let Self { poll_count, polls } = self.get_mut();
|
||||
**poll_count = poll_count.saturating_add(1);
|
||||
polls.pop_front().unwrap_or(Poll::Ready(None))
|
||||
}
|
||||
|
||||
fn is_end_stream(&self) -> bool {
|
||||
self.polls.is_empty()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
use http_body::{Body, Frame};
|
||||
use pin_project_lite::pin_project;
|
||||
use std::{
|
||||
any::type_name,
|
||||
fmt,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
pin_project! {
|
||||
/// Body returned by the [`inspect_err()`] combinator.
|
||||
///
|
||||
/// [`inspect_err()`]: crate::BodyExt::inspect_err
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct InspectErr<B, F> {
|
||||
#[pin]
|
||||
inner: B,
|
||||
f: F
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, F> InspectErr<B, F> {
|
||||
#[inline]
|
||||
pub(crate) fn new(body: B, f: F) -> Self {
|
||||
Self { inner: body, f }
|
||||
}
|
||||
|
||||
/// Get a reference to the inner body
|
||||
pub fn get_ref(&self) -> &B {
|
||||
&self.inner
|
||||
}
|
||||
|
||||
/// Get a mutable reference to the inner body
|
||||
pub fn get_mut(&mut self) -> &mut B {
|
||||
&mut self.inner
|
||||
}
|
||||
|
||||
/// Get a pinned mutable reference to the inner body
|
||||
pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut B> {
|
||||
self.project().inner
|
||||
}
|
||||
|
||||
/// Consume `self`, returning the inner body
|
||||
pub fn into_inner(self) -> B {
|
||||
self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, F> Body for InspectErr<B, F>
|
||||
where
|
||||
B: Body,
|
||||
F: FnMut(&B::Error),
|
||||
{
|
||||
type Data = B::Data;
|
||||
type Error = B::Error;
|
||||
|
||||
fn poll_frame(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
|
||||
let this = self.project();
|
||||
match this.inner.poll_frame(cx) {
|
||||
Poll::Pending => Poll::Pending,
|
||||
Poll::Ready(None) => Poll::Ready(None),
|
||||
Poll::Ready(Some(Ok(frame))) => Poll::Ready(Some(Ok(frame))),
|
||||
Poll::Ready(Some(Err(err))) => {
|
||||
(this.f)(&err);
|
||||
Poll::Ready(Some(Err(err)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> http_body::SizeHint {
|
||||
self.inner.size_hint()
|
||||
}
|
||||
|
||||
fn is_end_stream(&self) -> bool {
|
||||
self.inner.is_end_stream()
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, F> fmt::Debug for InspectErr<B, F>
|
||||
where
|
||||
B: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.debug_struct("InspectErr")
|
||||
.field("inner", &self.inner)
|
||||
.field("f", &type_name::<F>())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
use http_body::{Body, Frame};
|
||||
use pin_project_lite::pin_project;
|
||||
use std::{
|
||||
any::type_name,
|
||||
fmt,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
pin_project! {
|
||||
/// Body returned by the [`inspect_frame()`] combinator.
|
||||
///
|
||||
/// [`inspect_frame()`]: crate::BodyExt::inspect_frame
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct InspectFrame<B, F> {
|
||||
#[pin]
|
||||
inner: B,
|
||||
f: F
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, F> InspectFrame<B, F> {
|
||||
#[inline]
|
||||
pub(crate) fn new(body: B, f: F) -> Self {
|
||||
Self { inner: body, f }
|
||||
}
|
||||
|
||||
/// Get a reference to the inner body
|
||||
pub fn get_ref(&self) -> &B {
|
||||
&self.inner
|
||||
}
|
||||
|
||||
/// Get a mutable reference to the inner body
|
||||
pub fn get_mut(&mut self) -> &mut B {
|
||||
&mut self.inner
|
||||
}
|
||||
|
||||
/// Get a pinned mutable reference to the inner body
|
||||
pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut B> {
|
||||
self.project().inner
|
||||
}
|
||||
|
||||
/// Consume `self`, returning the inner body
|
||||
pub fn into_inner(self) -> B {
|
||||
self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, F> Body for InspectFrame<B, F>
|
||||
where
|
||||
B: Body,
|
||||
F: FnMut(&Frame<B::Data>),
|
||||
{
|
||||
type Data = B::Data;
|
||||
type Error = B::Error;
|
||||
|
||||
fn poll_frame(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
|
||||
let this = self.project();
|
||||
match this.inner.poll_frame(cx) {
|
||||
Poll::Pending => Poll::Pending,
|
||||
Poll::Ready(None) => Poll::Ready(None),
|
||||
Poll::Ready(Some(Err(err))) => Poll::Ready(Some(Err(err))),
|
||||
Poll::Ready(Some(Ok(frame))) => {
|
||||
(this.f)(&frame);
|
||||
Poll::Ready(Some(Ok(frame)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> http_body::SizeHint {
|
||||
self.inner.size_hint()
|
||||
}
|
||||
|
||||
fn is_end_stream(&self) -> bool {
|
||||
self.inner.is_end_stream()
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, F> fmt::Debug for InspectFrame<B, F>
|
||||
where
|
||||
B: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.debug_struct("InspectFrame")
|
||||
.field("inner", &self.inner)
|
||||
.field("f", &type_name::<F>())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
use http_body::{Body, Frame, SizeHint};
|
||||
use pin_project_lite::pin_project;
|
||||
use std::{
|
||||
any::type_name,
|
||||
fmt,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
pin_project! {
|
||||
/// Body returned by the [`map_err`] combinator.
|
||||
///
|
||||
/// [`map_err`]: crate::BodyExt::map_err
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct MapErr<B, F> {
|
||||
#[pin]
|
||||
inner: B,
|
||||
f: F
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, F> MapErr<B, F> {
|
||||
#[inline]
|
||||
pub(crate) fn new(body: B, f: F) -> Self {
|
||||
Self { inner: body, f }
|
||||
}
|
||||
|
||||
/// Get a reference to the inner body
|
||||
pub fn get_ref(&self) -> &B {
|
||||
&self.inner
|
||||
}
|
||||
|
||||
/// Get a mutable reference to the inner body
|
||||
pub fn get_mut(&mut self) -> &mut B {
|
||||
&mut self.inner
|
||||
}
|
||||
|
||||
/// Get a pinned mutable reference to the inner body
|
||||
pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut B> {
|
||||
self.project().inner
|
||||
}
|
||||
|
||||
/// Consume `self`, returning the inner body
|
||||
pub fn into_inner(self) -> B {
|
||||
self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, F, E> Body for MapErr<B, F>
|
||||
where
|
||||
B: Body,
|
||||
F: FnMut(B::Error) -> E,
|
||||
{
|
||||
type Data = B::Data;
|
||||
type Error = E;
|
||||
|
||||
fn poll_frame(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
|
||||
let this = self.project();
|
||||
match this.inner.poll_frame(cx) {
|
||||
Poll::Pending => Poll::Pending,
|
||||
Poll::Ready(None) => Poll::Ready(None),
|
||||
Poll::Ready(Some(Ok(frame))) => Poll::Ready(Some(Ok(frame))),
|
||||
Poll::Ready(Some(Err(err))) => Poll::Ready(Some(Err((this.f)(err)))),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_end_stream(&self) -> bool {
|
||||
self.inner.is_end_stream()
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> SizeHint {
|
||||
self.inner.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, F> fmt::Debug for MapErr<B, F>
|
||||
where
|
||||
B: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.debug_struct("MapErr")
|
||||
.field("inner", &self.inner)
|
||||
.field("f", &type_name::<F>())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
use bytes::Buf;
|
||||
use http_body::{Body, Frame};
|
||||
use pin_project_lite::pin_project;
|
||||
use std::{
|
||||
any::type_name,
|
||||
fmt,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
pin_project! {
|
||||
/// Body returned by the [`map_frame`] combinator.
|
||||
///
|
||||
/// [`map_frame`]: crate::BodyExt::map_frame
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct MapFrame<B, F> {
|
||||
#[pin]
|
||||
inner: B,
|
||||
f: F
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, F> MapFrame<B, F> {
|
||||
#[inline]
|
||||
pub(crate) fn new(body: B, f: F) -> Self {
|
||||
Self { inner: body, f }
|
||||
}
|
||||
|
||||
/// Get a reference to the inner body
|
||||
pub fn get_ref(&self) -> &B {
|
||||
&self.inner
|
||||
}
|
||||
|
||||
/// Get a mutable reference to the inner body
|
||||
pub fn get_mut(&mut self) -> &mut B {
|
||||
&mut self.inner
|
||||
}
|
||||
|
||||
/// Get a pinned mutable reference to the inner body
|
||||
pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut B> {
|
||||
self.project().inner
|
||||
}
|
||||
|
||||
/// Consume `self`, returning the inner body
|
||||
pub fn into_inner(self) -> B {
|
||||
self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, F, B2> Body for MapFrame<B, F>
|
||||
where
|
||||
B: Body,
|
||||
F: FnMut(Frame<B::Data>) -> Frame<B2>,
|
||||
B2: Buf,
|
||||
{
|
||||
type Data = B2;
|
||||
type Error = B::Error;
|
||||
|
||||
fn poll_frame(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
|
||||
let this = self.project();
|
||||
match this.inner.poll_frame(cx) {
|
||||
Poll::Pending => Poll::Pending,
|
||||
Poll::Ready(None) => Poll::Ready(None),
|
||||
Poll::Ready(Some(Ok(frame))) => Poll::Ready(Some(Ok((this.f)(frame)))),
|
||||
Poll::Ready(Some(Err(err))) => Poll::Ready(Some(Err(err))),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_end_stream(&self) -> bool {
|
||||
self.inner.is_end_stream()
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, F> fmt::Debug for MapFrame<B, F>
|
||||
where
|
||||
B: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.debug_struct("MapFrame")
|
||||
.field("inner", &self.inner)
|
||||
.field("f", &type_name::<F>())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
//! Combinators for the `Body` trait.
|
||||
|
||||
mod box_body;
|
||||
mod collect;
|
||||
mod frame;
|
||||
mod fuse;
|
||||
mod inspect_err;
|
||||
mod inspect_frame;
|
||||
mod map_err;
|
||||
mod map_frame;
|
||||
mod with_trailers;
|
||||
|
||||
pub use self::{
|
||||
box_body::{BoxBody, UnsyncBoxBody},
|
||||
collect::Collect,
|
||||
frame::Frame,
|
||||
fuse::Fuse,
|
||||
inspect_err::InspectErr,
|
||||
inspect_frame::InspectFrame,
|
||||
map_err::MapErr,
|
||||
map_frame::MapFrame,
|
||||
with_trailers::WithTrailers,
|
||||
};
|
||||
@@ -0,0 +1,213 @@
|
||||
use std::{
|
||||
future::Future,
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
use futures_core::ready;
|
||||
use http::HeaderMap;
|
||||
use http_body::{Body, Frame};
|
||||
use pin_project_lite::pin_project;
|
||||
|
||||
pin_project! {
|
||||
/// Adds trailers to a body.
|
||||
///
|
||||
/// See [`BodyExt::with_trailers`] for more details.
|
||||
pub struct WithTrailers<T, F> {
|
||||
#[pin]
|
||||
state: State<T, F>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, F> WithTrailers<T, F> {
|
||||
pub(crate) fn new(body: T, trailers: F) -> Self {
|
||||
Self {
|
||||
state: State::PollBody {
|
||||
body,
|
||||
trailers: Some(trailers),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pin_project! {
|
||||
#[project = StateProj]
|
||||
enum State<T, F> {
|
||||
PollBody {
|
||||
#[pin]
|
||||
body: T,
|
||||
trailers: Option<F>,
|
||||
},
|
||||
PollTrailers {
|
||||
#[pin]
|
||||
trailers: F,
|
||||
prev_trailers: Option<HeaderMap>,
|
||||
},
|
||||
Done,
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, F> Body for WithTrailers<T, F>
|
||||
where
|
||||
T: Body,
|
||||
F: Future<Output = Option<Result<HeaderMap, T::Error>>>,
|
||||
{
|
||||
type Data = T::Data;
|
||||
type Error = T::Error;
|
||||
|
||||
fn poll_frame(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
|
||||
loop {
|
||||
let mut this = self.as_mut().project();
|
||||
|
||||
match this.state.as_mut().project() {
|
||||
StateProj::PollBody { body, trailers } => match ready!(body.poll_frame(cx)?) {
|
||||
Some(frame) => match frame.into_trailers() {
|
||||
Ok(prev_trailers) => {
|
||||
let trailers = trailers.take().unwrap();
|
||||
this.state.set(State::PollTrailers {
|
||||
trailers,
|
||||
prev_trailers: Some(prev_trailers),
|
||||
});
|
||||
}
|
||||
Err(frame) => {
|
||||
return Poll::Ready(Some(Ok(frame)));
|
||||
}
|
||||
},
|
||||
None => {
|
||||
let trailers = trailers.take().unwrap();
|
||||
this.state.set(State::PollTrailers {
|
||||
trailers,
|
||||
prev_trailers: None,
|
||||
});
|
||||
}
|
||||
},
|
||||
StateProj::PollTrailers {
|
||||
trailers,
|
||||
prev_trailers,
|
||||
} => {
|
||||
let trailers = ready!(trailers.poll(cx)?);
|
||||
match (trailers, prev_trailers.take()) {
|
||||
(None, None) => return Poll::Ready(None),
|
||||
(None, Some(trailers)) | (Some(trailers), None) => {
|
||||
this.state.set(State::Done);
|
||||
return Poll::Ready(Some(Ok(Frame::trailers(trailers))));
|
||||
}
|
||||
(Some(new_trailers), Some(mut prev_trailers)) => {
|
||||
prev_trailers.extend(new_trailers);
|
||||
this.state.set(State::Done);
|
||||
return Poll::Ready(Some(Ok(Frame::trailers(prev_trailers))));
|
||||
}
|
||||
}
|
||||
}
|
||||
StateProj::Done => {
|
||||
return Poll::Ready(None);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> http_body::SizeHint {
|
||||
match &self.state {
|
||||
State::PollBody { body, .. } => body.size_hint(),
|
||||
State::PollTrailers { .. } | State::Done => Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::convert::Infallible;
|
||||
|
||||
use bytes::Bytes;
|
||||
use http::{HeaderName, HeaderValue};
|
||||
|
||||
use crate::{BodyExt, Empty, Full};
|
||||
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn works() {
|
||||
let mut trailers = HeaderMap::new();
|
||||
trailers.insert(
|
||||
HeaderName::from_static("foo"),
|
||||
HeaderValue::from_static("bar"),
|
||||
);
|
||||
|
||||
let body =
|
||||
Full::<Bytes>::from("hello").with_trailers(std::future::ready(Some(
|
||||
Ok::<_, Infallible>(trailers.clone()),
|
||||
)));
|
||||
|
||||
futures_util::pin_mut!(body);
|
||||
let waker = futures_util::task::noop_waker();
|
||||
let mut cx = Context::from_waker(&waker);
|
||||
|
||||
let data = unwrap_ready(body.as_mut().poll_frame(&mut cx))
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.into_data()
|
||||
.unwrap();
|
||||
assert_eq!(data, "hello");
|
||||
|
||||
let body_trailers = unwrap_ready(body.as_mut().poll_frame(&mut cx))
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.into_trailers()
|
||||
.unwrap();
|
||||
assert_eq!(body_trailers, trailers);
|
||||
|
||||
assert!(unwrap_ready(body.as_mut().poll_frame(&mut cx)).is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn merges_trailers() {
|
||||
let mut trailers_1 = HeaderMap::new();
|
||||
trailers_1.insert(
|
||||
HeaderName::from_static("foo"),
|
||||
HeaderValue::from_static("bar"),
|
||||
);
|
||||
|
||||
let mut trailers_2 = HeaderMap::new();
|
||||
trailers_2.insert(
|
||||
HeaderName::from_static("baz"),
|
||||
HeaderValue::from_static("qux"),
|
||||
);
|
||||
|
||||
let body = Empty::<Bytes>::new()
|
||||
.with_trailers(std::future::ready(Some(Ok::<_, Infallible>(
|
||||
trailers_1.clone(),
|
||||
))))
|
||||
.with_trailers(std::future::ready(Some(Ok::<_, Infallible>(
|
||||
trailers_2.clone(),
|
||||
))));
|
||||
|
||||
futures_util::pin_mut!(body);
|
||||
let waker = futures_util::task::noop_waker();
|
||||
let mut cx = Context::from_waker(&waker);
|
||||
|
||||
let body_trailers = unwrap_ready(body.as_mut().poll_frame(&mut cx))
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.into_trailers()
|
||||
.unwrap();
|
||||
|
||||
let mut all_trailers = HeaderMap::new();
|
||||
all_trailers.extend(trailers_1);
|
||||
all_trailers.extend(trailers_2);
|
||||
assert_eq!(body_trailers, all_trailers);
|
||||
|
||||
assert!(unwrap_ready(body.as_mut().poll_frame(&mut cx)).is_none());
|
||||
}
|
||||
|
||||
fn unwrap_ready<T>(poll: Poll<T>) -> T {
|
||||
match poll {
|
||||
Poll::Ready(t) => t,
|
||||
Poll::Pending => panic!("pending"),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user