Skip to content

Commit

Permalink
Merge pull request #369 from toyboot4e/no-question
Browse files Browse the repository at this point in the history
Remove `?` operators from proc-macro generated code
  • Loading branch information
mrDIMAS authored Sep 27, 2022
2 parents 902eab5 + ab2a44a commit 54898cc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
6 changes: 5 additions & 1 deletion fyrox-core-derive/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,11 @@ fn gen_impl(
}

fn set(&mut self, value: Box<dyn Reflect>) -> Result<Box<dyn Reflect>, Box<dyn Reflect>> {
let this = std::mem::replace(self, value.take()?);
let value = match value.take() {
Ok(x) => x,
Err(err) => return Err(err),
};
let this = std::mem::replace(self, value);
Ok(Box::new(this))
}

Expand Down
30 changes: 20 additions & 10 deletions fyrox-core-derive/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ fn impl_visit_struct(
let visit_fn_body = if field_args.style == ast::Style::Unit {
quote! { Ok(()) }
} else {
// `field.visit(..);` parts
// `field.visit(..)?;` parts
let field_visits =
utils::create_field_visits(None, field_args.fields.iter(), field_args.style);
utils::create_field_visits(true, field_args.fields.iter(), field_args.style);

quote! {
let mut region = visitor.enter_region(name)?;
#(self.#field_visits)*
let mut region = match visitor.enter_region(name) {
Ok(x) => x,
Err(err) => return Err(err),
};
#(#field_visits)*
Ok(())
}
};
Expand Down Expand Up @@ -152,7 +155,7 @@ fn impl_visit_enum(ty_args: &args::TypeArgs, variant_args: &[args::VariantArgs])

match style {
ast::Style::Struct => {
let field_visits = utils::create_field_visits(None, fields.iter(), style);
let field_visits = utils::create_field_visits(false, fields.iter(), style);

let idents = fields.iter().map(|field| {
let ident = &field.ident;
Expand All @@ -166,8 +169,7 @@ fn impl_visit_enum(ty_args: &args::TypeArgs, variant_args: &[args::VariantArgs])
}
}
ast::Style::Tuple => {
let field_visits =
utils::create_field_visits(parse_quote!(f), fields.iter(), style);
let field_visits = utils::create_field_visits(false, fields.iter(), style);

let idents = (0..fields.len()).map(|i| format_ident!("f{}", Index::from(i)));

Expand All @@ -187,13 +189,21 @@ fn impl_visit_enum(ty_args: &args::TypeArgs, variant_args: &[args::VariantArgs])
ty_args,
variant_args.iter().flat_map(|v| v.fields.iter()).cloned(),
quote! {
let mut region = visitor.enter_region(name)?;
let mut region = match visitor.enter_region(name) {
Ok(x) => x,
Err(err) => return Err(err),
};

let mut id = id(self);
id.visit("Id", &mut region)?;
if let Err(err) = id.visit("Id", &mut region) {
return Err(err);
};

if region.is_reading() {
*self = from_id(id)?;
*self = match from_id(id) {
Ok(x) => x,
Err(s) => return Err(s.into()),
};
}

match self {
Expand Down
33 changes: 19 additions & 14 deletions fyrox-core-derive/src/visit/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub fn create_impl(
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

quote! {
#[allow(clippy::question_mark)]
impl #impl_generics Visit for #ty_ident #ty_generics #where_clause {
fn visit(
&mut self,
Expand Down Expand Up @@ -47,15 +48,15 @@ fn create_impl_generics(
generics
}

/// `<prefix>field.visit("name", visitor);`
/// `<prefix>field.visit("name", visitor)?;`
pub fn create_field_visits<'a>(
// None or `f` when bindings tuple variants. NOTE: We can't use `prefix: Ident`
prefix: Option<Ident>,
// false if enum variant
is_struct: bool,
fields: impl Iterator<Item = &'a args::FieldArgs>,
field_style: ast::Style,
) -> Vec<TokenStream2> {
if field_style == ast::Style::Unit {
// `Unit` (struct/enum variant) has no field to visit.
// `Unit` struct/enum variant has no field to visit.
// We won't even enter this region:
return vec![];
}
Expand All @@ -76,14 +77,14 @@ pub fn create_field_visits<'a>(
}
// `Tuple(f32, ..)`
ast::Style::Tuple => {
let ident = Index::from(field_index);

let ident = match prefix {
Some(ref prefix) => {
let ident = format_ident!("{}{}", prefix, ident);
quote!(#ident)
}
None => quote!(#ident),
let index = Index::from(field_index);
let ident = if is_struct {
// accessed with `self.<index>`
quote!(#index)
} else {
// named as `f<index>`
let ident = format_ident!("f{}", index);
quote!(#ident)
};

(ident, format!("{}", field_index))
Expand Down Expand Up @@ -114,16 +115,20 @@ pub fn create_field_visits<'a>(
}
}

let prefix = if is_struct { Some(quote!(self.)) } else { None };

visit_args
.iter()
.map(|(ident, name, optional)| {
if *optional {
quote! {
#ident.visit(#name, &mut region).ok();
#prefix #ident.visit(#name, &mut region).ok();
}
} else {
quote! {
#ident.visit(#name, &mut region)?;
if let Err(err) = #prefix #ident.visit(#name, &mut region) {
return Err(err);
}
}
}
})
Expand Down

0 comments on commit 54898cc

Please sign in to comment.