Skip to content

Commit

Permalink
wasmtime-wasi-http: provide Debug impls in all pub types (#8979)
Browse files Browse the repository at this point in the history
* wasmtime-wasi: derive(Debug) on AbortOnDropJoinHandle

* wasmtime_wasi_http: derive(Debug) on all pub types and their contents
  • Loading branch information
Pat Hickey authored Jul 22, 2024
1 parent 542af68 commit 0d1efe7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
11 changes: 10 additions & 1 deletion crates/wasi-http/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub type HyperIncomingBody = BoxBody<Bytes, types::ErrorCode>;
pub type HyperOutgoingBody = BoxBody<Bytes, types::ErrorCode>;

/// The concrete type behind a `was:http/types/incoming-body` resource.
#[derive(Debug)]
pub struct HostIncomingBody {
body: IncomingBodyState,
/// An optional worker task to keep alive while this body is being read.
Expand Down Expand Up @@ -72,6 +73,7 @@ impl HostIncomingBody {
}

/// Internal state of a [`HostIncomingBody`].
#[derive(Debug)]
enum IncomingBodyState {
/// The body is stored here meaning that within `HostIncomingBody` the
/// `take_stream` method can be called for example.
Expand All @@ -84,6 +86,7 @@ enum IncomingBodyState {
}

/// Small wrapper around [`HyperIncomingBody`] which adds a timeout to every frame.
#[derive(Debug)]
struct BodyWithTimeout {
/// Underlying stream that frames are coming from.
inner: HyperIncomingBody,
Expand Down Expand Up @@ -146,6 +149,7 @@ impl Body for BodyWithTimeout {

/// Message sent when a `HostIncomingBodyStream` is done to the
/// `HostFutureTrailers` state.
#[derive(Debug)]
enum StreamEnd {
/// The body wasn't completely read and was dropped early. May still have
/// trailers, but requires reading more frames.
Expand All @@ -158,6 +162,7 @@ enum StreamEnd {

/// The concrete type behind the `wasi:io/streams/input-stream` resource returned
/// by `wasi:http/types/incoming-body`'s `stream` method.
#[derive(Debug)]
pub struct HostIncomingBodyStream {
state: IncomingBodyStreamState,
buffer: Bytes,
Expand Down Expand Up @@ -209,6 +214,7 @@ impl HostIncomingBodyStream {
}
}

#[derive(Debug)]
enum IncomingBodyStreamState {
/// The body is currently open for reading and present here.
///
Expand Down Expand Up @@ -293,6 +299,7 @@ impl Drop for HostIncomingBodyStream {
}

/// The concrete type behind a `wasi:http/types/future-trailers` resource.
#[derive(Debug)]
pub enum HostFutureTrailers {
/// Trailers aren't here yet.
///
Expand Down Expand Up @@ -378,7 +385,7 @@ impl Subscribe for HostFutureTrailers {
}
}

#[derive(Clone)]
#[derive(Debug, Clone)]
struct WrittenState {
expected: u64,
written: Arc<std::sync::atomic::AtomicU64>,
Expand Down Expand Up @@ -540,6 +547,7 @@ impl HostOutgoingBody {
}

/// Message sent to end the `[HostOutgoingBody]` stream.
#[derive(Debug)]
enum FinishMessage {
Finished,
Trailers(hyper::HeaderMap),
Expand All @@ -566,6 +574,7 @@ impl StreamContext {
}

/// Provides a [`HostOutputStream`] impl from a [`tokio::sync::mpsc::Sender`].
#[derive(Debug)]
struct BodyWriteStream {
context: StreamContext,
writer: mpsc::Sender<Bytes>,
Expand Down
1 change: 1 addition & 0 deletions crates/wasi-http/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};

/// A type that wraps any type implementing [`tokio::io::AsyncRead`] and [`tokio::io::AsyncWrite`]
/// and itself implements [`hyper::rt::Read`] and [`hyper::rt::Write`].
#[derive(Debug)]
pub struct TokioIo<T> {
inner: T,
}
Expand Down
9 changes: 8 additions & 1 deletion crates/wasi-http/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use wasmtime::component::{Resource, ResourceTable};
use wasmtime_wasi::{runtime::AbortOnDropJoinHandle, Subscribe};

/// Capture the state necessary for use in the wasi-http API implementation.
#[derive(Debug)]
pub struct WasiHttpCtx {
_priv: (),
}
Expand Down Expand Up @@ -486,6 +487,7 @@ impl TryInto<http::Method> for types::Method {
}

/// The concrete type behind a `wasi:http/types/incoming-request` resource.
#[derive(Debug)]
pub struct HostIncomingRequest {
pub(crate) parts: http::request::Parts,
pub(crate) scheme: Scheme,
Expand Down Expand Up @@ -561,6 +563,7 @@ impl TryFrom<HostOutgoingResponse> for hyper::Response<HyperOutgoingBody> {
}

/// The concrete type behind a `wasi:http/types/outgoing-request` resource.
#[derive(Debug)]
pub struct HostOutgoingRequest {
/// The method of the request.
pub method: Method,
Expand All @@ -577,7 +580,7 @@ pub struct HostOutgoingRequest {
}

/// The concrete type behind a `wasi:http/types/request-options` resource.
#[derive(Default)]
#[derive(Debug, Default)]
pub struct HostRequestOptions {
/// How long to wait for a connection to be established.
pub connect_timeout: Option<std::time::Duration>,
Expand All @@ -588,6 +591,7 @@ pub struct HostRequestOptions {
}

/// The concrete type behind a `wasi:http/types/incoming-response` resource.
#[derive(Debug)]
pub struct HostIncomingResponse {
/// The response status
pub status: u16,
Expand All @@ -598,6 +602,7 @@ pub struct HostIncomingResponse {
}

/// The concrete type behind a `wasi:http/types/fields` resource.
#[derive(Debug)]
pub enum HostFields {
/// A reference to the fields of a parent entry.
Ref {
Expand Down Expand Up @@ -626,6 +631,7 @@ pub type FutureIncomingResponseHandle =
AbortOnDropJoinHandle<anyhow::Result<Result<IncomingResponse, types::ErrorCode>>>;

/// A response that is in the process of being received.
#[derive(Debug)]
pub struct IncomingResponse {
/// The response itself.
pub resp: hyper::Response<HyperIncomingBody>,
Expand All @@ -636,6 +642,7 @@ pub struct IncomingResponse {
}

/// The concrete type behind a `wasi:http/types/future-incoming-response` resource.
#[derive(Debug)]
pub enum HostFutureIncomingResponse {
/// A pending response
Pending(FutureIncomingResponseHandle),
Expand Down
1 change: 1 addition & 0 deletions crates/wasi/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub(crate) static RUNTIME: once_cell::sync::Lazy<tokio::runtime::Runtime> =
///
/// This behavior makes it easier to tie a worker task to the lifetime of a Resource
/// by keeping this handle owned by the Resource.
#[derive(Debug)]
pub struct AbortOnDropJoinHandle<T>(tokio::task::JoinHandle<T>);
impl<T> Drop for AbortOnDropJoinHandle<T> {
fn drop(&mut self) {
Expand Down

0 comments on commit 0d1efe7

Please sign in to comment.