Skip to content

Commit 2fbba1b

Browse files
committed
Improve the outparam API
Signed-off-by: Ryan Levick <[email protected]>
1 parent 9ae9308 commit 2fbba1b

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

examples/wasi-http-rust-streaming-outgoing-body/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use anyhow::{bail, Result};
22
use futures::{stream, SinkExt, StreamExt, TryStreamExt};
33
use spin_sdk::http::send;
44
use spin_sdk::http::{
5-
Fields, IncomingRequest, IncomingResponse, Method, OutgoingBody, OutgoingRequest,
5+
Headers, IncomingRequest, IncomingResponse, Method, OutgoingBody, OutgoingRequest,
66
OutgoingResponse, ResponseOutparam, Scheme,
77
};
88
use spin_sdk::http_component;
@@ -32,12 +32,12 @@ async fn handle_request(request: IncomingRequest, response_out: ResponseOutparam
3232

3333
let response = OutgoingResponse::new(
3434
200,
35-
&Fields::new(&[("content-type".to_string(), b"text/plain".to_vec())]),
35+
&Headers::new(&[("content-type".to_string(), b"text/plain".to_vec())]),
3636
);
3737

3838
let mut body = response.take_body();
3939

40-
ResponseOutparam::set(response_out, Ok(response));
40+
response_out.set(response);
4141

4242
while let Some((url, result)) = results.next().await {
4343
let payload = match result {
@@ -54,7 +54,7 @@ async fn handle_request(request: IncomingRequest, response_out: ResponseOutparam
5454
(Method::Post, Some("/echo")) => {
5555
let response = OutgoingResponse::new(
5656
200,
57-
&Fields::new(
57+
&Headers::new(
5858
&headers
5959
.into_iter()
6060
.filter_map(|(k, v)| (k == "content-type").then_some((k, v)))
@@ -64,7 +64,7 @@ async fn handle_request(request: IncomingRequest, response_out: ResponseOutparam
6464

6565
let mut body = response.take_body();
6666

67-
ResponseOutparam::set(response_out, Ok(response));
67+
response_out.set(response);
6868

6969
let mut stream = request.into_body_stream();
7070
while let Some(chunk) = stream.next().await {
@@ -84,11 +84,11 @@ async fn handle_request(request: IncomingRequest, response_out: ResponseOutparam
8484
}
8585

8686
_ => {
87-
let response = OutgoingResponse::new(405, &Fields::new(&[]));
87+
let response = OutgoingResponse::new(405, &Headers::new(&[]));
8888

8989
let body = response.write().expect("response should be writable");
9090

91-
ResponseOutparam::set(response_out, Ok(response));
91+
response_out.set(response);
9292

9393
OutgoingBody::finish(body, None);
9494
}
@@ -105,7 +105,7 @@ async fn hash(url: &Url) -> Result<String> {
105105
scheme => Scheme::Other(scheme.into()),
106106
}),
107107
Some(url.authority()),
108-
&Fields::new(&[]),
108+
&Headers::new(&[]),
109109
);
110110

111111
let response: IncomingResponse = send(request).await?;

sdk/rust/macro/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ pub fn http_component(_attr: TokenStream, item: TokenStream) -> TokenStream {
121121

122122
impl From<self::wasi::http::types::IncomingRequest> for ::spin_sdk::http::IncomingRequest {
123123
fn from(req: self::wasi::http::types::IncomingRequest) -> Self {
124-
let req = ::std::mem::ManuallyDrop::new(req);
125-
unsafe { Self::from_handle(req.handle()) }
124+
unsafe { Self::from_handle(req.into_handle()) }
126125
}
127126
}
128127

@@ -134,8 +133,7 @@ pub fn http_component(_attr: TokenStream, item: TokenStream) -> TokenStream {
134133

135134
impl From<self::wasi::http::types::ResponseOutparam> for ::spin_sdk::http::ResponseOutparam {
136135
fn from(resp: self::wasi::http::types::ResponseOutparam) -> Self {
137-
let resp = ::std::mem::ManuallyDrop::new(resp);
138-
unsafe { Self::from_handle(resp.handle()) }
136+
unsafe { Self::from_handle(resp.into_handle()) }
139137
}
140138
}
141139
}

sdk/rust/src/http.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ pub use conversions::IntoResponse;
66

77
use self::conversions::TryFromIncomingResponse;
88

9+
use super::wit::wasi::http::types;
910
#[doc(inline)]
10-
pub use super::wit::wasi::http::types::*;
11+
pub use types::{
12+
Error, Fields, Headers, IncomingRequest, IncomingResponse, Method, OutgoingBody,
13+
OutgoingRequest, OutgoingResponse, Scheme, StatusCode, Trailers,
14+
};
1115

1216
/// A unified request object that can represent both incoming and outgoing requests.
1317
///
@@ -153,7 +157,22 @@ impl OutgoingResponse {
153157
}
154158
}
155159

160+
/// The out param for setting an `OutgoingResponse`
161+
pub struct ResponseOutparam(types::ResponseOutparam);
162+
156163
impl ResponseOutparam {
164+
#[doc(hidden)]
165+
// This is needed for the macro so we can transfrom the macro's
166+
// `ResponseOutparam` to this `ResponseOutparam`
167+
pub unsafe fn from_handle(handle: u32) -> Self {
168+
Self(types::ResponseOutparam::from_handle(handle))
169+
}
170+
171+
/// Set the outgoing response
172+
pub fn set(self, response: OutgoingResponse) {
173+
types::ResponseOutparam::set(self.0, Ok(response));
174+
}
175+
157176
/// Set with the outgoing response and the supplied buffer
158177
///
159178
/// Will panic if response body has already been taken
@@ -164,7 +183,7 @@ impl ResponseOutparam {
164183
) -> anyhow::Result<()> {
165184
use futures::SinkExt;
166185
let mut body = response.take_body();
167-
ResponseOutparam::set(self, Ok(response));
186+
self.set(response);
168187
body.send(buffer).await
169188
}
170189
}

sdk/rust/src/http/conversions.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use async_trait::async_trait;
22

3-
use super::{
4-
Fields, Headers, IncomingRequest, IncomingResponse, OutgoingRequest, OutgoingResponse,
5-
};
3+
use super::{Headers, IncomingRequest, IncomingResponse, OutgoingRequest, OutgoingResponse};
64

75
use super::{responses, NonUtf8BodyError, Request, Response};
86

@@ -466,7 +464,7 @@ where
466464
})
467465
.as_ref(),
468466
req.uri().authority().map(|a| a.as_str()),
469-
&Fields::new(&headers),
467+
&Headers::new(&headers),
470468
))
471469
}
472470
}

0 commit comments

Comments
 (0)