Simplify code.

This commit is contained in:
Stephen Chung 2020-12-23 23:29:19 +08:00
parent 7d58324ad4
commit 9fd7e01197
4 changed files with 53 additions and 41 deletions

View File

@ -97,7 +97,7 @@ pub(crate) fn print_type(ty: &syn::Type) -> String {
#[derive(Debug, Default)]
pub(crate) struct ExportedFnParams {
pub name: Option<Vec<String>>,
pub name: Vec<String>,
pub return_raw: bool,
pub skip: bool,
pub special: FnSpecialAccess,
@ -252,7 +252,7 @@ impl ExportedParams for ExportedFnParams {
}
Ok(ExportedFnParams {
name: if name.is_empty() { None } else { Some(name) },
name,
return_raw,
skip,
special,
@ -455,16 +455,12 @@ impl ExportedFn {
}
pub(crate) fn exported_names(&self) -> Vec<syn::LitStr> {
let mut literals = self
let mut literals: Vec<_> = self
.params
.name
.as_ref()
.map(|v| {
v.iter()
.map(|s| syn::LitStr::new(s, proc_macro2::Span::call_site()))
.collect()
})
.unwrap_or_else(|| Vec::new());
.iter()
.map(|s| syn::LitStr::new(s, proc_macro2::Span::call_site()))
.collect();
if let Some((s, _, span)) = self.params.special.get_fn_name() {
literals.push(syn::LitStr::new(&s, span));
@ -481,11 +477,10 @@ impl ExportedFn {
}
pub(crate) fn exported_name<'n>(&'n self) -> Cow<'n, str> {
if let Some(ref name) = self.params.name {
name.last().unwrap().as_str().into()
} else {
self.signature.ident.to_string().into()
}
self.params.name.last().map_or_else(
|| self.signature.ident.to_string().into(),
|s| s.as_str().into(),
)
}
pub(crate) fn arg_list(&self) -> impl Iterator<Item = &syn::FnArg> {
@ -712,10 +707,12 @@ impl ExportedFn {
pub fn generate_impl(&self, on_type_name: &str) -> proc_macro2::TokenStream {
let sig_name = self.name().clone();
let name = self.params.name.as_ref().map_or_else(
|| self.name().to_string(),
|names| names.last().unwrap().clone(),
);
let name = self
.params
.name
.last()
.cloned()
.unwrap_or_else(|| self.name().to_string());
let arg_count = self.arg_count();
let is_method_call = self.mutable_receiver();

View File

@ -243,13 +243,13 @@ pub(crate) fn check_rename_collisions(fns: &Vec<ExportedFn>) -> Result<(), syn::
let mut fn_defs = HashMap::<String, proc_macro2::Span>::new();
for itemfn in fns.iter() {
if itemfn.params().name.is_some() || itemfn.params().special != FnSpecialAccess::None {
let mut names = itemfn
if !itemfn.params().name.is_empty() || itemfn.params().special != FnSpecialAccess::None {
let mut names: Vec<_> = itemfn
.params()
.name
.as_ref()
.map(|v| v.iter().map(|n| (n.clone(), n.clone())).collect())
.unwrap_or_else(|| Vec::new());
.iter()
.map(|n| (n.clone(), n.clone()))
.collect();
if let Some((s, n, _)) = itemfn.params().special.get_fn_name() {
names.push((s, n));

View File

@ -79,9 +79,9 @@ pub struct FuncInfo {
/// Number of parameters.
pub params: usize,
/// Parameter types (if applicable).
pub param_types: Option<StaticVec<TypeId>>,
pub param_types: StaticVec<TypeId>,
/// Parameter names (if available).
pub param_names: Option<StaticVec<ImmutableString>>,
pub param_names: StaticVec<ImmutableString>,
}
impl FuncInfo {
@ -89,13 +89,19 @@ impl FuncInfo {
pub fn gen_signature(&self) -> String {
let mut sig = format!("{}(", self.name);
if let Some(ref names) = self.param_names {
let mut params: Vec<_> = names.iter().map(ImmutableString::to_string).collect();
if !self.param_names.is_empty() {
let mut params: Vec<_> = self
.param_names
.iter()
.map(ImmutableString::to_string)
.collect();
let return_type = params.pop().unwrap_or_else(|| "()".to_string());
sig.push_str(&params.join(", "));
if return_type != "()" {
sig.push_str(") -> ");
sig.push_str(&return_type);
} else if self.func.is_script() {
sig.push_str(") -> Dynamic");
} else {
sig.push_str(")");
}
@ -106,7 +112,12 @@ impl FuncInfo {
sig.push_str(", ");
}
}
sig.push_str(") -> Dynamic");
if self.func.is_script() {
sig.push_str(") -> Dynamic");
} else {
sig.push_str(") -> ?");
}
}
sig
@ -419,8 +430,8 @@ impl Module {
namespace: FnNamespace::Internal,
access: fn_def.access,
params: num_params,
param_types: None,
param_names: Some(param_names),
param_types: Default::default(),
param_names,
func: fn_def.into(),
},
);
@ -556,7 +567,7 @@ impl Module {
arg_names: impl AsRef<[&'a str]>,
) -> &mut Self {
if let Some(f) = self.functions.get_mut(&hash_fn) {
f.param_names = Some(arg_names.as_ref().iter().map(|&n| n.into()).collect());
f.param_names = arg_names.as_ref().iter().map(|&n| n.into()).collect();
}
self
}
@ -593,7 +604,7 @@ impl Module {
let hash_fn = crate::calc_native_fn_hash(empty(), &name, arg_types.iter().cloned());
let params = arg_types
let param_types = arg_types
.into_iter()
.cloned()
.map(|id| {
@ -611,9 +622,13 @@ impl Module {
name,
namespace,
access,
params: params.len(),
param_types: Some(params),
param_names: arg_names.map(|p| p.iter().map(|&v| v.into()).collect()),
params: param_types.len(),
param_types,
param_names: if let Some(p) = arg_names {
p.iter().map(|&v| v.into()).collect()
} else {
Default::default()
},
func: func.into(),
},
);
@ -1781,7 +1796,7 @@ impl Module {
name,
namespace,
params,
param_types: types,
param_types,
func,
..
},
@ -1795,7 +1810,7 @@ impl Module {
let hash_qualified_script =
crate::calc_script_fn_hash(qualifiers.iter().cloned(), name, *params);
if let Some(param_types) = types {
if !func.is_script() {
assert_eq!(*params, param_types.len());
// Namespace-qualified Rust functions are indexed in two steps:

View File

@ -90,7 +90,7 @@ struct FnMetadata {
pub return_type: Option<String>,
pub signature: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub doc_comments: Option<Vec<String>>,
pub doc_comments: Vec<String>,
}
impl PartialOrd for FnMetadata {
@ -152,9 +152,9 @@ impl From<&crate::module::FuncInfo> for FnMetadata {
},
signature: info.gen_signature(),
doc_comments: if info.func.is_script() {
Some(info.func.get_fn_def().comments.clone())
info.func.get_fn_def().comments.clone()
} else {
None
Default::default()
},
}
}