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

View File

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

View File

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