Streamline code.
This commit is contained in:
@@ -300,55 +300,50 @@ impl Parse for ExportedFn {
|
||||
let visibility = fn_all.vis;
|
||||
|
||||
// Determine if the function requires a call context
|
||||
if let Some(first_arg) = fn_all.sig.inputs.first() {
|
||||
if let syn::FnArg::Typed(syn::PatType { ref ty, .. }) = first_arg {
|
||||
match fn_all.sig.inputs.first() {
|
||||
Some(syn::FnArg::Typed(syn::PatType { ref ty, .. })) => {
|
||||
match flatten_type_groups(ty.as_ref()) {
|
||||
syn::Type::Path(p)
|
||||
if p.path == context_type_path1 || p.path == context_type_path2 =>
|
||||
{
|
||||
pass_context = true;
|
||||
}
|
||||
_ => (),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
let skip_slots = if pass_context { 1 } else { 0 };
|
||||
|
||||
// Determine whether function generates a special calling convention for a mutable receiver.
|
||||
let mut_receiver = {
|
||||
if let Some(first_arg) = fn_all.sig.inputs.iter().skip(skip_slots).next() {
|
||||
match first_arg {
|
||||
syn::FnArg::Receiver(syn::Receiver {
|
||||
reference: Some(_), ..
|
||||
let mut_receiver = match fn_all.sig.inputs.iter().skip(skip_slots).next() {
|
||||
Some(syn::FnArg::Receiver(syn::Receiver {
|
||||
reference: Some(_), ..
|
||||
})) => true,
|
||||
Some(syn::FnArg::Typed(syn::PatType { ref ty, .. })) => {
|
||||
match flatten_type_groups(ty.as_ref()) {
|
||||
syn::Type::Reference(syn::TypeReference {
|
||||
mutability: Some(_),
|
||||
..
|
||||
}) => true,
|
||||
syn::FnArg::Typed(syn::PatType { ref ty, .. }) => {
|
||||
match flatten_type_groups(ty.as_ref()) {
|
||||
syn::Type::Reference(syn::TypeReference {
|
||||
mutability: Some(_),
|
||||
..
|
||||
}) => true,
|
||||
syn::Type::Reference(syn::TypeReference {
|
||||
mutability: None,
|
||||
ref elem,
|
||||
..
|
||||
}) => match flatten_type_groups(elem.as_ref()) {
|
||||
syn::Type::Path(ref p) if p.path == str_type_path => false,
|
||||
_ => {
|
||||
return Err(syn::Error::new(
|
||||
ty.span(),
|
||||
"references from Rhai in this position must be mutable",
|
||||
))
|
||||
}
|
||||
},
|
||||
_ => false,
|
||||
syn::Type::Reference(syn::TypeReference {
|
||||
mutability: None,
|
||||
ref elem,
|
||||
..
|
||||
}) => match flatten_type_groups(elem.as_ref()) {
|
||||
syn::Type::Path(ref p) if p.path == str_type_path => false,
|
||||
_ => {
|
||||
return Err(syn::Error::new(
|
||||
ty.span(),
|
||||
"references from Rhai in this position must be mutable",
|
||||
))
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => false,
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
_ => false,
|
||||
};
|
||||
|
||||
// All arguments after the first must be moved except for &str.
|
||||
@@ -381,22 +376,25 @@ impl Parse for ExportedFn {
|
||||
}
|
||||
|
||||
// Check return type.
|
||||
if let syn::ReturnType::Type(_, ref ret_type) = fn_all.sig.output {
|
||||
match flatten_type_groups(ret_type.as_ref()) {
|
||||
syn::Type::Ptr(_) => {
|
||||
return Err(syn::Error::new(
|
||||
fn_all.sig.output.span(),
|
||||
"Rhai functions cannot return pointers",
|
||||
))
|
||||
match fn_all.sig.output {
|
||||
syn::ReturnType::Type(_, ref ret_type) => {
|
||||
match flatten_type_groups(ret_type.as_ref()) {
|
||||
syn::Type::Ptr(_) => {
|
||||
return Err(syn::Error::new(
|
||||
fn_all.sig.output.span(),
|
||||
"Rhai functions cannot return pointers",
|
||||
))
|
||||
}
|
||||
syn::Type::Reference(_) => {
|
||||
return Err(syn::Error::new(
|
||||
fn_all.sig.output.span(),
|
||||
"Rhai functions cannot return references",
|
||||
))
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
syn::Type::Reference(_) => {
|
||||
return Err(syn::Error::new(
|
||||
fn_all.sig.output.span(),
|
||||
"Rhai functions cannot return references",
|
||||
))
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(ExportedFn {
|
||||
entire_span,
|
||||
@@ -494,10 +492,9 @@ impl ExportedFn {
|
||||
}
|
||||
|
||||
pub fn return_type(&self) -> Option<&syn::Type> {
|
||||
if let syn::ReturnType::Type(_, ref ret_type) = self.signature.output {
|
||||
Some(flatten_type_groups(ret_type))
|
||||
} else {
|
||||
None
|
||||
match self.signature.output {
|
||||
syn::ReturnType::Type(_, ref ret_type) => Some(flatten_type_groups(ret_type)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -622,16 +619,12 @@ impl ExportedFn {
|
||||
let arguments: Vec<syn::Ident> = dynamic_signature
|
||||
.inputs
|
||||
.iter()
|
||||
.filter_map(|fn_arg| {
|
||||
if let syn::FnArg::Typed(syn::PatType { ref pat, .. }) = fn_arg {
|
||||
if let syn::Pat::Ident(ref ident) = pat.as_ref() {
|
||||
Some(ident.ident.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
.filter_map(|fn_arg| match fn_arg {
|
||||
syn::FnArg::Typed(syn::PatType { ref pat, .. }) => match pat.as_ref() {
|
||||
syn::Pat::Ident(ref ident) => Some(ident.ident.clone()),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
|
||||
|
@@ -156,8 +156,11 @@ impl Parse for Module {
|
||||
}) => {
|
||||
// #[cfg] attributes are not allowed on const declarations
|
||||
crate::attrs::deny_cfg_attr(&attrs)?;
|
||||
if let syn::Visibility::Public(_) = vis {
|
||||
consts.push((ident.to_string(), ty.clone(), expr.as_ref().clone()));
|
||||
match vis {
|
||||
syn::Visibility::Public(_) => {
|
||||
consts.push((ident.to_string(), ty.clone(), expr.as_ref().clone()))
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
@@ -170,26 +173,23 @@ impl Parse for Module {
|
||||
sub_modules.reserve(content.len() - fns.len() - consts.len());
|
||||
let mut i = 0;
|
||||
while i < content.len() {
|
||||
if let syn::Item::Mod(_) = &content[i] {
|
||||
let mut item_mod = match content.remove(i) {
|
||||
syn::Item::Mod(m) => m,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let params: ExportedModParams = match crate::attrs::inner_item_attributes(
|
||||
&mut item_mod.attrs,
|
||||
"rhai_mod",
|
||||
) {
|
||||
Ok(p) => p,
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
let module =
|
||||
syn::parse2::<Module>(item_mod.to_token_stream()).and_then(|mut m| {
|
||||
m.set_params(params)?;
|
||||
Ok(m)
|
||||
})?;
|
||||
sub_modules.push(module);
|
||||
} else {
|
||||
i += 1;
|
||||
match content[i] {
|
||||
syn::Item::Mod(_) => {
|
||||
let mut item_mod = match content.remove(i) {
|
||||
syn::Item::Mod(m) => m,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let params: ExportedModParams =
|
||||
crate::attrs::inner_item_attributes(&mut item_mod.attrs, "rhai_mod")?;
|
||||
let module = syn::parse2::<Module>(item_mod.to_token_stream()).and_then(
|
||||
|mut m| {
|
||||
m.set_params(params)?;
|
||||
Ok(m)
|
||||
},
|
||||
)?;
|
||||
sub_modules.push(module);
|
||||
}
|
||||
_ => i += 1,
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user