Skip to content

Commit

Permalink
test(server): Use a !Send and !Sync HttpBody in an example
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
espindola authored and seanmonstar committed Oct 12, 2021
1 parent 6169db2 commit 7feab2f
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion examples/single_threaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 7feab2f

Please sign in to comment.