From 75450277750435078cbe6b5d59ccd52a74762c2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Michel?= Date: Mon, 10 Jul 2023 14:41:04 +0000 Subject: [PATCH] implement async methods for connection and streams --- transport/src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/transport/src/lib.rs b/transport/src/lib.rs index 5409bbc..cc35615 100644 --- a/transport/src/lib.rs +++ b/transport/src/lib.rs @@ -2,7 +2,7 @@ // do a PR with the changes afterwards use std::task::{self, Poll}; -use bytes::Buf; +use bytes::{Buf, BufMut}; use anyhow::Error; type ErrorCode = u64; @@ -125,10 +125,50 @@ pub trait RecvStream { /// Get QUIC send stream id fn recv_id(&self) -> StreamId; + } +pub async fn accept_recv, R: RecvStream>(conn: &mut C) -> anyhow::Result, Error> { + Ok(std::future::poll_fn(|cx| conn.poll_accept_recv(cx)).await?) + +} + +pub async fn accept_bidi, B: BidiStream>(conn: &mut C) -> anyhow::Result, Error> { + Ok(std::future::poll_fn(|cx| conn.poll_accept_bidi(cx)).await?) + +} + +pub async fn open_send, S: SendStream>(conn: &mut C) -> anyhow::Result { + Ok(std::future::poll_fn(|cx| conn.poll_open_send(cx)).await?) + +} + +pub async fn open_bidi, B: BidiStream>(conn: &mut C) -> anyhow::Result { + Ok(std::future::poll_fn(|cx| conn.poll_open_bidi(cx)).await?) + +} + + + +pub async fn recv>(recv: &mut R , outbuf: &mut BM) -> anyhow::Result { + let buf = std::future::poll_fn(|cx| recv.poll_data(cx)).await?; + match buf { + Some(buf) => { + outbuf.put(buf); + Ok(true) + } + None => Ok(false) // stream finished + } +} + +pub async fn send>(send: &mut S, buf: &mut B) -> anyhow::Result { + Ok(std::future::poll_fn(|cx| send.poll_send(cx, buf)).await?) +} + + + /// Optional trait to allow "splitting" a bidirectional stream into two sides. -pub trait BidiStream: SendStream + RecvStream { +pub trait BidiStream: SendStream + RecvStream { /// The type for the send half. type SendStream: SendStream; /// The type for the receive half.