add a lifetime to the wiggle_runtime::GuestErrorType trait (#41)

* add a lifetime to the wiggle_runtime::GuestErrorType trait, wiggle_tests::WasiCtx struct

* wiggle-generate: make config parsing public so it can be reused in lucet
This commit is contained in:
Pat Hickey
2020-03-10 14:48:57 -07:00
committed by GitHub
parent 2139020d6d
commit cd484e4993
14 changed files with 46 additions and 34 deletions

View File

@@ -14,25 +14,32 @@ pub struct Config {
pub ctx: CtxConf,
}
enum ConfigField {
#[derive(Debug, Clone)]
pub enum ConfigField {
Witx(WitxConf),
Ctx(CtxConf),
}
impl ConfigField {
pub fn parse_pair(ident: &str, value: ParseStream, err_loc: Span) -> Result<Self> {
match ident {
"witx" => Ok(ConfigField::Witx(value.parse()?)),
"ctx" => Ok(ConfigField::Ctx(value.parse()?)),
_ => Err(Error::new(err_loc, "expected `witx` or `ctx`")),
}
}
}
impl Parse for ConfigField {
fn parse(input: ParseStream) -> Result<Self> {
let id: Ident = input.parse()?;
let _colon: Token![:] = input.parse()?;
match id.to_string().as_ref() {
"witx" => Ok(ConfigField::Witx(input.parse()?)),
"ctx" => Ok(ConfigField::Ctx(input.parse()?)),
_ => Err(Error::new(id.span(), "expected `witx` or `ctx`")),
}
Self::parse_pair(id.to_string().as_ref(), input, id.span())
}
}
impl Config {
fn build(fields: impl Iterator<Item = ConfigField>, err_loc: Span) -> Result<Self> {
pub fn build(fields: impl Iterator<Item = ConfigField>, err_loc: Span) -> Result<Self> {
let mut witx = None;
let mut ctx = None;
for f in fields {

View File

@@ -1,4 +1,4 @@
mod config;
pub mod config;
mod funcs;
mod lifetimes;
mod module_trait;

View File

@@ -1,7 +1,7 @@
use crate::{GuestError, GuestPtr};
use std::mem;
pub trait GuestErrorType {
pub trait GuestErrorType<'a> {
type Context;
fn success() -> Self;
fn from_error(e: GuestError, ctx: &Self::Context) -> Self;

View File

@@ -1,5 +1,6 @@
use proptest::prelude::*;
use std::cell::UnsafeCell;
use std::marker;
use wiggle_runtime::GuestMemory;
#[derive(Debug, Clone)]
@@ -290,14 +291,18 @@ mod test {
use std::cell::RefCell;
use wiggle_runtime::GuestError;
pub struct WasiCtx {
// In lucet, our Ctx struct needs a lifetime, so we're using one
// on the test as well.
pub struct WasiCtx<'a> {
pub guest_errors: RefCell<Vec<GuestError>>,
lifetime: marker::PhantomData<&'a ()>,
}
impl WasiCtx {
impl<'a> WasiCtx<'a> {
pub fn new() -> Self {
Self {
guest_errors: RefCell::new(vec![]),
lifetime: marker::PhantomData,
}
}
}
@@ -309,8 +314,8 @@ impl WasiCtx {
#[macro_export]
macro_rules! impl_errno {
( $errno:ty ) => {
impl wiggle_runtime::GuestErrorType for $errno {
type Context = WasiCtx;
impl<'a> wiggle_runtime::GuestErrorType<'a> for $errno {
type Context = WasiCtx<'a>;
fn success() -> $errno {
<$errno>::Ok
}