Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 732bfd9

Browse files
author
Pat Hickey
committed
uniform view trait
1 parent bf4c634 commit 732bfd9

File tree

5 files changed

+20
-11
lines changed

5 files changed

+20
-11
lines changed

host/src/main.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,16 @@ async fn main() -> Result<()> {
101101
.inherit_network(cap_std::ambient_authority())
102102
.build();
103103
impl WasiSocketsView for CommandCtx {
104-
fn table(&mut self) -> &mut Table {
104+
fn table(&self) -> &Table {
105+
&self.table
106+
}
107+
fn table_mut(&mut self) -> &mut Table {
105108
&mut self.table
106109
}
107-
fn ctx(&mut self) -> &mut WasiSocketsCtx {
110+
fn ctx(&self) -> &WasiSocketsCtx {
111+
&self.sockets
112+
}
113+
fn ctx_mut(&mut self) -> &mut WasiSocketsCtx {
108114
&mut self.sockets
109115
}
110116
}

wasi-sockets/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ impl WasiSocketsCtx {
7979
}
8080

8181
pub trait WasiSocketsView: Send {
82-
fn ctx(&mut self) -> &mut WasiSocketsCtx;
83-
fn table(&mut self) -> &mut Table;
82+
fn table(&self) -> &Table;
83+
fn table_mut(&mut self) -> &mut Table;
84+
fn ctx(&self) -> &WasiSocketsCtx;
85+
fn ctx_mut(&mut self) -> &mut WasiSocketsCtx;
8486
}
8587

8688
pub fn add_to_linker<T: WasiSocketsView>(

wasi-sockets/src/network_impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub(crate) fn convert(_error: wasi_common::Error) -> anyhow::Error {
1212
#[async_trait::async_trait]
1313
impl<T: WasiSocketsView> network::Host for T {
1414
async fn drop_network(&mut self, this: Network) -> anyhow::Result<()> {
15-
let table = self.table();
15+
let table = self.table_mut();
1616
if !table.delete::<Box<dyn WasiNetwork>>(this).is_ok() {
1717
anyhow::bail!("{this} is not a network");
1818
}
@@ -25,7 +25,7 @@ impl<T: WasiSocketsView> instance_network::Host for T {
2525
async fn instance_network(&mut self) -> anyhow::Result<Network> {
2626
let ctx = self.ctx();
2727
let network = (ctx.network_creator)(ctx.pool.clone())?;
28-
let table = self.table();
28+
let table = self.table_mut();
2929
let network = table.push(Box::new(network)).map_err(convert)?;
3030
Ok(network)
3131
}

wasi-sockets/src/tcp.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl<T: WasiSocketsView> tcp::Host for T {
3939
&mut self,
4040
socket: TcpSocket,
4141
) -> anyhow::Result<Result<(TcpSocket, InputStream, OutputStream), Error>> {
42-
let table = self.table();
42+
let table = self.table_mut();
4343
let socket = table.get_tcp_socket(socket)?;
4444

4545
let (connection, input_stream, output_stream, _addr) = socket.accept(false).await?;
@@ -57,7 +57,7 @@ impl<T: WasiSocketsView> tcp::Host for T {
5757
network: Network,
5858
remote_address: IpSocketAddress,
5959
) -> anyhow::Result<Result<(InputStream, OutputStream), Error>> {
60-
let table = self.table();
60+
let table = self.table_mut();
6161
let socket = table.get_tcp_socket(socket)?;
6262
let network = table.get_network(network)?;
6363

@@ -260,7 +260,7 @@ impl<T: WasiSocketsView> tcp::Host for T {
260260
}
261261

262262
async fn drop_tcp_socket(&mut self, this: TcpSocket) -> anyhow::Result<()> {
263-
let table = self.table();
263+
let table = self.table_mut();
264264
if !table.delete::<Box<dyn WasiTcpSocket>>(this).is_ok() {
265265
anyhow::bail!("{this} is not a socket");
266266
}
@@ -276,7 +276,7 @@ impl<T: WasiSocketsView> tcp_create_socket::Host for T {
276276
) -> anyhow::Result<Result<TcpSocket, Error>> {
277277
let ctx = self.ctx();
278278
let socket = (ctx.tcp_socket_creator)(address_family.into())?;
279-
let table = self.table();
279+
let table = self.table_mut();
280280
let socket = table.push(Box::new(socket)).map_err(convert)?;
281281
Ok(Ok(socket))
282282
}

wasi-sockets/src/udp.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ impl<T: WasiSocketsView> udp::Host for T {
122122
this: UdpSocket,
123123
value: bool,
124124
) -> anyhow::Result<Result<(), Error>> {
125-
let this = self.table().get_udp_socket_mut(this)?;
125+
let table = self.table_mut();
126+
let this = table.get_udp_socket_mut(this)?;
126127
this.set_nonblocking(value)?;
127128
Ok(Ok(()))
128129
}

0 commit comments

Comments
 (0)