test(server): Use a !Send and !Sync HttpBody in an example

The default Body type is used in the client in a Pool, so it has to be
Send. On the server, we can use a !Send type if the executor is single
threaded.

Expand the existing example to show that.
This commit is contained in:
Rafael Ávila de Espíndola
2021-10-12 14:51:32 -07:00
committed by Sean McArthur
parent 6169db250c
commit 7feab2f3db

View File

@@ -3,8 +3,47 @@
use std::cell::Cell;
use std::rc::Rc;
use hyper::body::{Bytes, HttpBody};
use hyper::header::{HeaderMap, HeaderValue};
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Error, Response, Server};
use hyper::{Error, Response, Server};
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};
struct Body {
// Our Body type is !Send and !Sync:
_marker: PhantomData<*const ()>,
data: Option<Bytes>,
}
impl From<String> for Body {
fn from(a: String) -> Self {
Body {
_marker: PhantomData,
data: Some(a.into()),
}
}
}
impl HttpBody for Body {
type Data = Bytes;
type Error = Error;
fn poll_data(
self: Pin<&mut Self>,
_: &mut Context<'_>,
) -> Poll<Option<Result<Self::Data, Self::Error>>> {
Poll::Ready(self.get_mut().data.take().map(Ok))
}
fn poll_trailers(
self: Pin<&mut Self>,
_: &mut Context<'_>,
) -> Poll<Result<Option<HeaderMap<HeaderValue>>, Self::Error>> {
Poll::Ready(Ok(None))
}
}
fn main() {
pretty_env_logger::init();