Skip to content

Commit 62a96c0

Browse files
committed
feat(body): change Sender::send_data to an async fn.
The previous version is renamed to `try_send_data`. BREAKING CHANGE: Usage of `send_data` should either be changed to async/await or use `try_send_data`.
1 parent 0331219 commit 62a96c0

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

src/body/body.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ use http_body::{SizeHint, Body as HttpBody};
1010
use http::HeaderMap;
1111

1212
use crate::common::{Future, Never, Pin, Poll, task};
13-
use super::internal::{FullDataArg, FullDataRet};
14-
use super::{Chunk, Payload};
13+
use super::Chunk;
1514
use crate::upgrade::OnUpgrade;
1615

1716
type BodySender = mpsc::Sender<Result<Chunk, crate::Error>>;
@@ -467,14 +466,25 @@ impl Sender {
467466
self.tx.poll_ready(cx).map_err(|_| crate::Error::new_closed())
468467
}
469468

470-
/// Sends data on this channel.
469+
/// Send data on this channel when it is ready.
470+
pub async fn send_data(&mut self, chunk: Chunk) -> crate::Result<()> {
471+
futures_util::future::poll_fn(|cx| self.poll_ready(cx)).await?;
472+
self.tx.try_send(Ok(chunk)).map_err(|_| crate::Error::new_closed())
473+
}
474+
475+
/// Try to send data on this channel.
471476
///
472-
/// This should be called after `poll_ready` indicated the channel
473-
/// could accept another `Chunk`.
477+
/// # Errors
474478
///
475479
/// Returns `Err(Chunk)` if the channel could not (currently) accept
476480
/// another `Chunk`.
477-
pub fn send_data(&mut self, chunk: Chunk) -> Result<(), Chunk> {
481+
///
482+
/// # Note
483+
///
484+
/// This is mostly useful for when trying to send from some other thread
485+
/// that doesn't have an async context. If in an async context, prefer
486+
/// [`send_data`][] instead.
487+
pub fn try_send_data(&mut self, chunk: Chunk) -> Result<(), Chunk> {
478488
self.tx
479489
.try_send(Ok(chunk))
480490
.map_err(|err| err.into_inner().expect("just sent Ok"))

src/proto/h1/dispatch.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use http::{Request, Response, StatusCode};
55
use tokio_io::{AsyncRead, AsyncWrite};
66

77
use crate::body::{Body, Payload};
8-
use crate::body::internal::FullDataArg;
98
use crate::common::{Future, Never, Poll, Pin, Unpin, task};
109
use crate::proto::{BodyLength, DecodedLength, Conn, Dispatched, MessageHead, RequestHead, RequestLine, ResponseHead};
1110
use super::Http1Transaction;
@@ -169,7 +168,7 @@ where
169168
}
170169
match self.conn.poll_read_body(cx) {
171170
Poll::Ready(Some(Ok(chunk))) => {
172-
match body.send_data(chunk) {
171+
match body.try_send_data(chunk) {
173172
Ok(()) => {
174173
self.body_tx = Some(body);
175174
},
@@ -249,7 +248,7 @@ where
249248
return Poll::Ready(Ok(()));
250249
} else if self.body_rx.is_none() && self.conn.can_write_head() && self.dispatch.should_poll() {
251250
if let Some(msg) = ready!(self.dispatch.poll_msg(cx)) {
252-
let (head, mut body) = msg.map_err(crate::Error::new_user_service)?;
251+
let (head, body) = msg.map_err(crate::Error::new_user_service)?;
253252

254253
let body_type = if body.is_end_stream() {
255254
self.body_rx.set(None);

tests/client.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1748,7 +1748,7 @@ mod conn {
17481748

17491749
let (mut sender, body) = Body::channel();
17501750
let sender = thread::spawn(move || {
1751-
sender.send_data("hello".into()).ok().unwrap();
1751+
sender.try_send_data("hello".into()).expect("try_send_data");
17521752
Runtime::new().unwrap().block_on(rx).unwrap();
17531753
sender.abort();
17541754
});

tests/server.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ mod response_body_lengths {
101101
},
102102
Bd::Unknown(b) => {
103103
let (mut tx, body) = hyper::Body::channel();
104-
tx.send_data(b.into()).expect("send_data");
104+
tx.try_send_data(b.into()).expect("try_send_data");
105105
reply.body_stream(body);
106106
b
107107
},

0 commit comments

Comments
 (0)