Put context object behind a ref rather than mut ref
This commit puts context object, i.e., the implementor of the WASI snapshot, behind a reference `&self` rather than a mutable reference `&mut self`. As suggested by @alexcrichton, this gives the implementor the possibility to determine how it handles its interior mutability.
This commit is contained in:
@@ -39,9 +39,9 @@ pub fn define_module_trait(names: &Names, m: &Module) -> TokenStream {
|
||||
.unwrap_or(quote!(()));
|
||||
|
||||
if is_anonymous {
|
||||
quote!(fn #funcname(&mut self, #(#args),*) -> Result<(#(#rets),*), #err>;)
|
||||
quote!(fn #funcname(&self, #(#args),*) -> Result<(#(#rets),*), #err>;)
|
||||
} else {
|
||||
quote!(fn #funcname<#lifetime>(&mut self, #(#args),*) -> Result<(#(#rets),*), #err>;)
|
||||
quote!(fn #funcname<#lifetime>(&self, #(#args),*) -> Result<(#(#rets),*), #err>;)
|
||||
}
|
||||
});
|
||||
quote! {
|
||||
|
||||
@@ -50,5 +50,5 @@ builtin_type!(u8, i8, u16, i16, u32, i32, u64, i64, f32, f64, usize);
|
||||
pub trait GuestErrorType {
|
||||
type Context;
|
||||
fn success() -> Self;
|
||||
fn from_error(e: GuestError, ctx: &mut Self::Context) -> Self;
|
||||
fn from_error(e: GuestError, ctx: &Self::Context) -> Self;
|
||||
}
|
||||
|
||||
@@ -91,16 +91,17 @@ mod test {
|
||||
}
|
||||
}
|
||||
|
||||
use std::cell::RefCell;
|
||||
use wiggle_runtime::GuestError;
|
||||
|
||||
pub struct WasiCtx {
|
||||
pub guest_errors: Vec<GuestError>,
|
||||
pub guest_errors: RefCell<Vec<GuestError>>,
|
||||
}
|
||||
|
||||
impl WasiCtx {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
guest_errors: vec![],
|
||||
guest_errors: RefCell::new(vec![]),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -117,9 +118,9 @@ macro_rules! impl_errno {
|
||||
fn success() -> $errno {
|
||||
<$errno>::Ok
|
||||
}
|
||||
fn from_error(e: GuestError, ctx: &mut WasiCtx) -> $errno {
|
||||
fn from_error(e: GuestError, ctx: &WasiCtx) -> $errno {
|
||||
eprintln!("GUEST ERROR: {:?}", e);
|
||||
ctx.guest_errors.push(e);
|
||||
ctx.guest_errors.borrow_mut().push(e);
|
||||
types::Errno::InvalidArg
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user