Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address comments, fix tests.
  • Loading branch information
brendandburns committed Apr 19, 2023
commit a72d8e0978d4179ab41acaddd29cade9914c46a9
2 changes: 1 addition & 1 deletion crates/wasi-http/src/http_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl WasiHttp {
let body = Full::<Bytes>::new(
self.streams
.get(&request.body)
.unwrap_or(&Stream::new())
.unwrap_or(&Stream::default())
.data
.clone(),
);
Expand Down
28 changes: 8 additions & 20 deletions crates/wasi-http/src/streams_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::poll::Pollable;
use crate::r#struct::Stream;
use crate::streams::{InputStream, OutputStream, StreamError};
use crate::WasiHttp;
use anyhow::{anyhow, bail};
Expand Down Expand Up @@ -74,34 +73,23 @@ impl crate::streams::Host for WasiHttp {
this: OutputStream,
buf: Vec<u8>,
) -> wasmtime::Result<Result<u64, StreamError>> {
let len = buf.len();
match self.streams.get(&this) {
Some(st) => {
if st.closed {
bail!("stream is dropped!");
}
let data = &st.data;
let mut new = bytes::BytesMut::with_capacity(data.len() + buf.len());
new.put(data.clone());
new.put(bytes::Bytes::from(buf.clone()));
self.streams.insert(
this,
Stream {
closed: false,
data: new.freeze(),
},
);
let new_len = st.data.len() + len;
let mut new = bytes::BytesMut::with_capacity(new_len);
Copy link
Member

@rvolosatovs rvolosatovs Apr 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From #6228 (comment), as you said, I think I'd just use BytesMut and https://docs.rs/bytes/latest/bytes/struct.BytesMut.html#method.extend_from_slice here

Since that would be quite a bit shorter, it could probably also make sense to remove the match altogether and instead rely on https://doc.rust-lang.org/std/collections/hash_map/enum.Entry.html#method.and_modify (see example there with or_insert)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I needed to use .or_default().extend_from_slice(...) but I think it achieves the same goal (and thanks for the suggestion of implementing Default that made it possible :)

new.put(st.data.clone());
new.put(bytes::Bytes::from(buf));
self.streams.insert(this, new.freeze().into());
}
None => {
self.streams.insert(
this,
Stream {
closed: false,
data: bytes::Bytes::from(buf.clone()),
},
);
self.streams.insert(this, bytes::Bytes::from(buf).into());
}
}
Ok(Ok(buf.len().try_into()?))
Ok(Ok(len.try_into()?))
}

fn write_zeroes(
Expand Down
10 changes: 8 additions & 2 deletions crates/wasi-http/src/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::types::{Method, Scheme};
use bytes::Bytes;
use std::collections::HashMap;

#[derive(Clone)]
#[derive(Clone, Default)]
pub struct Stream {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the usages of this struct, it seems that a From<Bytes> implementation could really clear this up

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added.

pub closed: bool,
pub data: Bytes,
Expand Down Expand Up @@ -74,9 +74,15 @@ impl ActiveResponse {

impl Stream {
pub fn new() -> Self {
Self::default()
}
}

impl From<Bytes> for Stream {
fn from(bytes: Bytes) -> Self {
Self {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we had a Default trait impl, this could have just called Self::default()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

closed: false,
data: Bytes::new(),
data: bytes,
}
}
}
Expand Down
23 changes: 9 additions & 14 deletions crates/wasi-http/src/types_impl.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::poll::Pollable;
use crate::r#struct::ActiveRequest;
use crate::r#struct::{ActiveRequest, Stream};
use crate::types::{
Error, Fields, FutureIncomingResponse, Headers, IncomingRequest, IncomingResponse,
IncomingStream, Method, OutgoingRequest, OutgoingResponse, OutgoingStream, ResponseOutparam,
Scheme, StatusCode, Trailers,
};
use crate::WasiHttp;
use anyhow::{anyhow, bail};
use std::collections::HashMap;
use std::collections::{hash_map::Entry, HashMap};

impl crate::types::Host for WasiHttp {
fn drop_fields(&mut self, fields: Fields) -> wasmtime::Result<()> {
Expand Down Expand Up @@ -123,12 +123,9 @@ impl crate::types::Host for WasiHttp {
bail!("unimplemented: drop_incoming_request")
}
fn drop_outgoing_request(&mut self, request: OutgoingRequest) -> wasmtime::Result<()> {
match self.requests.get(&request) {
Some(r) => {
self.streams.remove(&r.body);
self.requests.remove(&request);
}
None => { /* pass */ }
if let Entry::Occupied(e) = self.requests.entry(request) {
let r = e.remove();
self.streams.remove(&r.body);
}
Ok(())
}
Expand Down Expand Up @@ -198,6 +195,7 @@ impl crate::types::Host for WasiHttp {
if req.body == 0 {
req.body = self.streams_id_base;
self.streams_id_base = self.streams_id_base + 1;
self.streams.insert(req.body, Stream::default());
}
Ok(Ok(req.body))
}
Expand All @@ -212,12 +210,9 @@ impl crate::types::Host for WasiHttp {
bail!("unimplemented: set_response_outparam")
}
fn drop_incoming_response(&mut self, response: IncomingResponse) -> wasmtime::Result<()> {
match self.responses.get(&response) {
Some(r) => {
self.streams.remove(&r.body);
self.responses.remove(&response);
}
None => { /* pass */ }
if let Entry::Occupied(e) = self.responses.entry(response) {
let r = e.remove();
self.streams.remove(&r.body);
}
Ok(())
}
Expand Down