Skip to content

Commit 60abe0e

Browse files
committed
Add get_fields and remove FieldMapMutability
Clean up the interface to immutable fields by adding a different accessor.
1 parent 952c946 commit 60abe0e

File tree

2 files changed

+38
-52
lines changed

2 files changed

+38
-52
lines changed

crates/wasi-http/src/types.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -323,18 +323,6 @@ impl TryFrom<HostOutgoingResponse> for hyper::Response<HyperOutgoingBody> {
323323
}
324324
}
325325

326-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
327-
pub enum FieldMapMutability {
328-
Mutable,
329-
Immutable,
330-
}
331-
332-
impl FieldMapMutability {
333-
pub fn is_immutable(&self) -> bool {
334-
*self == Self::Immutable
335-
}
336-
}
337-
338326
pub type FieldMap = hyper::HeaderMap;
339327

340328
pub enum HostFields {

crates/wasi-http/src/types_impl.rs

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
bindings::http::types::{self, Error, Headers, Method, Scheme, StatusCode, Trailers},
33
body::{FinishMessage, HostFutureTrailers, HostIncomingBody, HostOutgoingBody},
44
types::{
5-
FieldMap, FieldMapMutability, HostFields, HostFutureIncomingResponse, HostIncomingRequest,
5+
FieldMap, HostFields, HostFutureIncomingResponse, HostIncomingRequest,
66
HostIncomingResponse, HostOutgoingRequest, HostOutgoingResponse, HostResponseOutparam,
77
},
88
WasiHttpView,
@@ -35,25 +35,35 @@ fn move_fields(table: &mut Table, id: Resource<HostFields>) -> wasmtime::Result<
3535
}
3636
}
3737

38-
fn get_fields_mut<'a>(
38+
fn get_fields<'a>(
3939
table: &'a mut Table,
4040
id: &Resource<HostFields>,
41-
) -> wasmtime::Result<(FieldMapMutability, &'a mut FieldMap)> {
41+
) -> wasmtime::Result<&'a FieldMap> {
4242
let fields = table.get(&id)?;
4343
if let HostFields::Ref { parent, get_fields } = *fields {
4444
let entry = table.get_any_mut(parent)?;
45-
return Ok((FieldMapMutability::Immutable, get_fields(entry)));
45+
return Ok(get_fields(entry));
4646
}
4747

4848
match table.get_mut(&id)? {
49-
HostFields::Owned { fields } => Ok((FieldMapMutability::Mutable, fields)),
49+
HostFields::Owned { fields } => Ok(fields),
5050
// NB: ideally the `if let` above would go here instead. That makes
5151
// the borrow-checker unhappy. Unclear why. If you, dear reader, can
5252
// refactor this to remove the `unreachable!` please do.
5353
HostFields::Ref { .. } => unreachable!(),
5454
}
5555
}
5656

57+
fn get_fields_mut<'a>(
58+
table: &'a mut Table,
59+
id: &Resource<HostFields>,
60+
) -> wasmtime::Result<Result<&'a mut FieldMap, types::HeaderError>> {
61+
match table.get_mut(&id)? {
62+
HostFields::Owned { fields } => Ok(Ok(fields)),
63+
HostFields::Ref { .. } => Ok(Err(types::HeaderError::Immutable)),
64+
}
65+
}
66+
5767
fn is_forbidden_header<T: WasiHttpView>(view: &mut T, name: &HeaderName) -> bool {
5868
static FORBIDDEN_HEADERS: [HeaderName; 9] = [
5969
hyper::header::CONNECTION,
@@ -131,9 +141,8 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostFields for T {
131141
Err(_) => return Ok(vec![]),
132142
};
133143

134-
let res = get_fields_mut(self.table(), &fields)
144+
let res = get_fields(self.table(), &fields)
135145
.context("[fields_get] getting fields")?
136-
.1
137146
.get_all(header)
138147
.into_iter()
139148
.map(|val| val.as_bytes().to_owned())
@@ -164,18 +173,15 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostFields for T {
164173
}
165174
}
166175

167-
let (mutability, m) =
168-
get_fields_mut(self.table(), &fields).context("[fields_set] getting mutable fields")?;
169-
if mutability.is_immutable() {
170-
return Ok(Err(types::HeaderError::Immutable));
171-
}
172-
173-
m.remove(&header);
174-
for value in values {
175-
m.append(&header, value);
176-
}
177-
178-
Ok(Ok(()))
176+
Ok(get_fields_mut(self.table(), &fields)
177+
.context("[fields_set] getting mutable fields")?
178+
.map(|fields| {
179+
fields.remove(&header);
180+
for value in values {
181+
fields.append(&header, value);
182+
}
183+
()
184+
}))
179185
}
180186

181187
fn delete(
@@ -192,13 +198,10 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostFields for T {
192198
return Ok(Err(types::HeaderError::Forbidden));
193199
}
194200

195-
let (mutability, m) = get_fields_mut(self.table(), &fields)?;
196-
if mutability.is_immutable() {
197-
return Ok(Err(types::HeaderError::Immutable));
198-
}
199-
200-
m.remove(header);
201-
Ok(Ok(()))
201+
Ok(get_fields_mut(self.table(), &fields)?.map(|fields| {
202+
fields.remove(header);
203+
()
204+
}))
202205
}
203206

204207
fn append(
@@ -221,32 +224,27 @@ impl<T: WasiHttpView> crate::bindings::http::types::HostFields for T {
221224
Err(_) => return Ok(Err(types::HeaderError::InvalidSyntax)),
222225
};
223226

224-
let (mutability, m) = get_fields_mut(self.table(), &fields)
225-
.context("[fields_append] getting mutable fields")?;
226-
if mutability.is_immutable() {
227-
return Ok(Err(types::HeaderError::Immutable));
228-
}
229-
230-
m.append(header, value);
231-
Ok(Ok(()))
227+
Ok(get_fields_mut(self.table(), &fields)
228+
.context("[fields_append] getting mutable fields")?
229+
.map(|fields| {
230+
fields.append(header, value);
231+
()
232+
}))
232233
}
233234

234235
fn entries(
235236
&mut self,
236237
fields: Resource<HostFields>,
237238
) -> wasmtime::Result<Vec<(String, Vec<u8>)>> {
238-
let (_, fields) = get_fields_mut(self.table(), &fields)?;
239-
let result = fields
239+
Ok(get_fields(self.table(), &fields)?
240240
.iter()
241241
.map(|(name, value)| (name.as_str().to_owned(), value.as_bytes().to_owned()))
242-
.collect();
243-
Ok(result)
242+
.collect())
244243
}
245244

246245
fn clone(&mut self, fields: Resource<HostFields>) -> wasmtime::Result<Resource<HostFields>> {
247-
let fields = get_fields_mut(self.table(), &fields)
246+
let fields = get_fields(self.table(), &fields)
248247
.context("[fields_clone] getting fields")?
249-
.1
250248
.clone();
251249

252250
let id = self

0 commit comments

Comments
 (0)