wiggle: convenient syntax for marking all funcs async
This commit is contained in:
@@ -47,7 +47,9 @@ impl Parse for ConfigField {
|
|||||||
} else if lookahead.peek(Token![async]) {
|
} else if lookahead.peek(Token![async]) {
|
||||||
input.parse::<Token![async]>()?;
|
input.parse::<Token![async]>()?;
|
||||||
input.parse::<Token![:]>()?;
|
input.parse::<Token![:]>()?;
|
||||||
Ok(ConfigField::Async(input.parse()?))
|
Ok(ConfigField::Async(AsyncConf {
|
||||||
|
functions: input.parse()?,
|
||||||
|
}))
|
||||||
} else {
|
} else {
|
||||||
Err(lookahead.error())
|
Err(lookahead.error())
|
||||||
}
|
}
|
||||||
@@ -282,21 +284,37 @@ impl Parse for ErrorConfField {
|
|||||||
#[derive(Clone, Default, Debug)]
|
#[derive(Clone, Default, Debug)]
|
||||||
/// Modules and funcs that have async signatures
|
/// Modules and funcs that have async signatures
|
||||||
pub struct AsyncConf {
|
pub struct AsyncConf {
|
||||||
functions: HashMap<String, Vec<String>>,
|
functions: AsyncFunctions,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub enum AsyncFunctions {
|
||||||
|
Some(HashMap<String, Vec<String>>),
|
||||||
|
All,
|
||||||
|
}
|
||||||
|
impl Default for AsyncFunctions {
|
||||||
|
fn default() -> Self {
|
||||||
|
AsyncFunctions::Some(HashMap::default())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AsyncConf {
|
impl AsyncConf {
|
||||||
pub fn is_async(&self, module: &str, function: &str) -> bool {
|
pub fn is_async(&self, module: &str, function: &str) -> bool {
|
||||||
self.functions
|
match &self.functions {
|
||||||
|
AsyncFunctions::Some(fs) => fs
|
||||||
.get(module)
|
.get(module)
|
||||||
.and_then(|fs| fs.iter().find(|f| *f == function))
|
.and_then(|fs| fs.iter().find(|f| *f == function))
|
||||||
.is_some()
|
.is_some(),
|
||||||
|
AsyncFunctions::All => true,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse for AsyncConf {
|
impl Parse for AsyncFunctions {
|
||||||
fn parse(input: ParseStream) -> Result<Self> {
|
fn parse(input: ParseStream) -> Result<Self> {
|
||||||
let content;
|
let content;
|
||||||
|
let lookahead = input.lookahead1();
|
||||||
|
if lookahead.peek(syn::token::Brace) {
|
||||||
let _ = braced!(content in input);
|
let _ = braced!(content in input);
|
||||||
let items: Punctuated<AsyncConfField, Token![,]> =
|
let items: Punctuated<AsyncConfField, Token![,]> =
|
||||||
content.parse_terminated(Parse::parse)?;
|
content.parse_terminated(Parse::parse)?;
|
||||||
@@ -315,7 +333,13 @@ impl Parse for AsyncConf {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(AsyncConf { functions })
|
Ok(AsyncFunctions::Some(functions))
|
||||||
|
} else if lookahead.peek(Token![*]) {
|
||||||
|
let _: Token![*] = input.parse().unwrap();
|
||||||
|
Ok(AsyncFunctions::All)
|
||||||
|
} else {
|
||||||
|
Err(lookahead.error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,9 +7,7 @@ use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx};
|
|||||||
|
|
||||||
wiggle::from_witx!({
|
wiggle::from_witx!({
|
||||||
witx: ["$CARGO_MANIFEST_DIR/tests/atoms.witx"],
|
witx: ["$CARGO_MANIFEST_DIR/tests/atoms.witx"],
|
||||||
async: {
|
async: *,
|
||||||
atoms::{int_float_args, double_int_return_float}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
impl_errno!(types::Errno);
|
impl_errno!(types::Errno);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use wiggle_generate::config::AsyncConfField;
|
use wiggle_generate::config::AsyncFunctions;
|
||||||
use {
|
use {
|
||||||
proc_macro2::Span,
|
proc_macro2::Span,
|
||||||
std::collections::HashMap,
|
std::collections::HashMap,
|
||||||
@@ -66,13 +66,17 @@ impl Parse for ConfigField {
|
|||||||
} else if lookahead.peek(Token![async]) {
|
} else if lookahead.peek(Token![async]) {
|
||||||
input.parse::<Token![async]>()?;
|
input.parse::<Token![async]>()?;
|
||||||
input.parse::<Token![:]>()?;
|
input.parse::<Token![:]>()?;
|
||||||
Ok(ConfigField::Async(input.parse()?))
|
Ok(ConfigField::Async(AsyncConf {
|
||||||
|
blocking: false,
|
||||||
|
functions: input.parse()?,
|
||||||
|
}))
|
||||||
} else if lookahead.peek(kw::block_on) {
|
} else if lookahead.peek(kw::block_on) {
|
||||||
input.parse::<kw::block_on>()?;
|
input.parse::<kw::block_on>()?;
|
||||||
input.parse::<Token![:]>()?;
|
input.parse::<Token![:]>()?;
|
||||||
let mut async_conf: AsyncConf = input.parse()?;
|
Ok(ConfigField::Async(AsyncConf {
|
||||||
async_conf.blocking = true;
|
blocking: true,
|
||||||
Ok(ConfigField::Async(async_conf))
|
functions: input.parse()?,
|
||||||
|
}))
|
||||||
} else {
|
} else {
|
||||||
Err(lookahead.error())
|
Err(lookahead.error())
|
||||||
}
|
}
|
||||||
@@ -273,7 +277,7 @@ impl Parse for ModulesConf {
|
|||||||
/// Modules and funcs that have async signatures
|
/// Modules and funcs that have async signatures
|
||||||
pub struct AsyncConf {
|
pub struct AsyncConf {
|
||||||
blocking: bool,
|
blocking: bool,
|
||||||
functions: HashMap<String, Vec<String>>,
|
functions: AsyncFunctions,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||||
@@ -297,47 +301,24 @@ impl Asyncness {
|
|||||||
|
|
||||||
impl AsyncConf {
|
impl AsyncConf {
|
||||||
pub fn is_async(&self, module: &str, function: &str) -> Asyncness {
|
pub fn is_async(&self, module: &str, function: &str) -> Asyncness {
|
||||||
if self
|
let a = if self.blocking {
|
||||||
.functions
|
Asyncness::Blocking
|
||||||
|
} else {
|
||||||
|
Asyncness::Async
|
||||||
|
};
|
||||||
|
match &self.functions {
|
||||||
|
AsyncFunctions::Some(fs) => {
|
||||||
|
if fs
|
||||||
.get(module)
|
.get(module)
|
||||||
.and_then(|fs| fs.iter().find(|f| *f == function))
|
.and_then(|fs| fs.iter().find(|f| *f == function))
|
||||||
.is_some()
|
.is_some()
|
||||||
{
|
{
|
||||||
if self.blocking {
|
a
|
||||||
Asyncness::Blocking
|
|
||||||
} else {
|
|
||||||
Asyncness::Async
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
Asyncness::Sync
|
Asyncness::Sync
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
AsyncFunctions::All => a,
|
||||||
|
|
||||||
impl Parse for AsyncConf {
|
|
||||||
fn parse(input: ParseStream) -> Result<Self> {
|
|
||||||
let content;
|
|
||||||
let _ = braced!(content in input);
|
|
||||||
let items: Punctuated<AsyncConfField, Token![,]> =
|
|
||||||
content.parse_terminated(Parse::parse)?;
|
|
||||||
let mut functions: HashMap<String, Vec<String>> = HashMap::new();
|
|
||||||
use std::collections::hash_map::Entry;
|
|
||||||
for i in items {
|
|
||||||
let function_names = i
|
|
||||||
.function_names
|
|
||||||
.iter()
|
|
||||||
.map(|i| i.to_string())
|
|
||||||
.collect::<Vec<String>>();
|
|
||||||
match functions.entry(i.module_name.to_string()) {
|
|
||||||
Entry::Occupied(o) => o.into_mut().extend(function_names),
|
|
||||||
Entry::Vacant(v) => {
|
|
||||||
v.insert(function_names);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Ok(AsyncConf {
|
|
||||||
functions,
|
|
||||||
blocking: false,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user