From 7feab2f3db33205968a9dba078a9c406a233ce30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20=C3=81vila=20de=20Esp=C3=ADndola?= Date: Tue, 12 Oct 2021 14:51:32 -0700 Subject: [PATCH] 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. --- examples/single_threaded.rs | 41 ++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/examples/single_threaded.rs b/examples/single_threaded.rs index 89f4953c..e8885cdb 100644 --- a/examples/single_threaded.rs +++ b/examples/single_threaded.rs @@ -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, +} + +impl From 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>> { + Poll::Ready(self.get_mut().data.take().map(Ok)) + } + + fn poll_trailers( + self: Pin<&mut Self>, + _: &mut Context<'_>, + ) -> Poll>, Self::Error>> { + Poll::Ready(Ok(None)) + } +} fn main() { pretty_env_logger::init();