Skip to content

Commit

Permalink
Remove unnecessary Results
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGVSV committed Aug 1, 2023
1 parent 7675529 commit b5f8c15
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 71 deletions.
4 changes: 2 additions & 2 deletions crates/bevy_reflect/bevy_reflect_derive/src/derive_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ impl<'a> ReflectStruct<'a> {
&self.fields
}

pub fn where_clause_options(&self) -> Result<WhereClauseOptions, syn::Error> {
pub fn where_clause_options(&self) -> WhereClauseOptions {
WhereClauseOptions::new(self.meta())
}
}
Expand All @@ -493,7 +493,7 @@ impl<'a> ReflectEnum<'a> {
&self.variants
}

pub fn where_clause_options(&self) -> Result<WhereClauseOptions, syn::Error> {
pub fn where_clause_options(&self) -> WhereClauseOptions {
WhereClauseOptions::new(self.meta())
}
}
Expand Down
34 changes: 14 additions & 20 deletions crates/bevy_reflect/bevy_reflect_derive/src/from_reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,32 @@ use quote::{quote, ToTokens};
use syn::{Field, Ident, Lit, LitInt, LitStr, Member};

/// Implements `FromReflect` for the given struct
pub(crate) fn impl_struct(
reflect_struct: &ReflectStruct,
) -> Result<proc_macro2::TokenStream, syn::Error> {
pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> proc_macro2::TokenStream {
impl_struct_internal(reflect_struct, false)
}

/// Implements `FromReflect` for the given tuple struct
pub(crate) fn impl_tuple_struct(
reflect_struct: &ReflectStruct,
) -> Result<proc_macro2::TokenStream, syn::Error> {
pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> proc_macro2::TokenStream {
impl_struct_internal(reflect_struct, true)
}

pub(crate) fn impl_value(meta: &ReflectMeta) -> Result<proc_macro2::TokenStream, syn::Error> {
pub(crate) fn impl_value(meta: &ReflectMeta) -> proc_macro2::TokenStream {
let type_path = meta.type_path();
let bevy_reflect_path = meta.bevy_reflect_path();
let (impl_generics, ty_generics, where_clause) = type_path.generics().split_for_impl();
let where_from_reflect_clause =
extend_where_clause(where_clause, &WhereClauseOptions::new(meta)?);
Ok(quote! {
extend_where_clause(where_clause, &WhereClauseOptions::new(meta));
quote! {
impl #impl_generics #bevy_reflect_path::FromReflect for #type_path #ty_generics #where_from_reflect_clause {
fn from_reflect(reflect: &dyn #bevy_reflect_path::Reflect) -> #FQOption<Self> {
#FQOption::Some(#FQClone::clone(<dyn #FQAny>::downcast_ref::<#type_path #ty_generics>(<dyn #bevy_reflect_path::Reflect>::as_any(reflect))?))
}
}
})
}
}

/// Implements `FromReflect` for the given enum type
pub(crate) fn impl_enum(
reflect_enum: &ReflectEnum,
) -> Result<proc_macro2::TokenStream, syn::Error> {
pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> proc_macro2::TokenStream {
let fqoption = FQOption.into_token_stream();

let enum_path = reflect_enum.meta().type_path();
Expand All @@ -57,9 +51,9 @@ pub(crate) fn impl_enum(

// Add FromReflect bound for each active field
let where_from_reflect_clause =
extend_where_clause(where_clause, &WhereClauseOptions::new(reflect_enum.meta())?);
extend_where_clause(where_clause, &WhereClauseOptions::new(reflect_enum.meta()));

Ok(quote! {
quote! {
impl #impl_generics #bevy_reflect_path::FromReflect for #enum_path #ty_generics #where_from_reflect_clause {
fn from_reflect(#ref_value: &dyn #bevy_reflect_path::Reflect) -> #FQOption<Self> {
if let #bevy_reflect_path::ReflectRef::Enum(#ref_value) = #bevy_reflect_path::Reflect::reflect_ref(#ref_value) {
Expand All @@ -72,7 +66,7 @@ pub(crate) fn impl_enum(
}
}
}
})
}
}

/// Container for a struct's members (field name or index) and their
Expand All @@ -88,7 +82,7 @@ impl MemberValuePair {
fn impl_struct_internal(
reflect_struct: &ReflectStruct,
is_tuple: bool,
) -> Result<proc_macro2::TokenStream, syn::Error> {
) -> proc_macro2::TokenStream {
let fqoption = FQOption.into_token_stream();

let struct_path = reflect_struct.meta().type_path();
Expand Down Expand Up @@ -138,10 +132,10 @@ fn impl_struct_internal(
// Add FromReflect bound for each active field
let where_from_reflect_clause = extend_where_clause(
where_clause,
&WhereClauseOptions::new(reflect_struct.meta())?,
&WhereClauseOptions::new(reflect_struct.meta()),
);

Ok(quote! {
quote! {
impl #impl_generics #bevy_reflect_path::FromReflect for #struct_path #ty_generics #where_from_reflect_clause {
fn from_reflect(reflect: &dyn #bevy_reflect_path::Reflect) -> #FQOption<Self> {
if let #bevy_reflect_path::ReflectRef::#ref_struct_type(#ref_struct) = #bevy_reflect_path::Reflect::reflect_ref(reflect) {
Expand All @@ -151,7 +145,7 @@ fn impl_struct_internal(
}
}
}
})
}
}

/// Get the collection of ignored field definitions
Expand Down
10 changes: 4 additions & 6 deletions crates/bevy_reflect/bevy_reflect_derive/src/impls/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ use proc_macro2::{Ident, Span};
use quote::quote;
use syn::Fields;

pub(crate) fn impl_enum(
reflect_enum: &ReflectEnum,
) -> Result<proc_macro2::TokenStream, syn::Error> {
pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> proc_macro2::TokenStream {
let bevy_reflect_path = reflect_enum.meta().bevy_reflect_path();
let enum_path = reflect_enum.meta().type_path();

let ref_name = Ident::new("__name_param", Span::call_site());
let ref_index = Ident::new("__index_param", Span::call_site());
let ref_value = Ident::new("__value_param", Span::call_site());

let where_clause_options = reflect_enum.where_clause_options()?;
let where_clause_options = reflect_enum.where_clause_options();

let EnumImpls {
variant_info,
Expand Down Expand Up @@ -98,7 +96,7 @@ pub(crate) fn impl_enum(

let where_reflect_clause = extend_where_clause(where_clause, &where_clause_options);

Ok(quote! {
quote! {
#get_type_registration_impl

#typed_impl
Expand Down Expand Up @@ -292,7 +290,7 @@ pub(crate) fn impl_enum(

#debug_fn
}
})
}
}

struct EnumImpls {
Expand Down
10 changes: 4 additions & 6 deletions crates/bevy_reflect/bevy_reflect_derive/src/impls/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use crate::ReflectStruct;
use quote::{quote, ToTokens};

/// Implements `Struct`, `GetTypeRegistration`, and `Reflect` for the given derive data.
pub(crate) fn impl_struct(
reflect_struct: &ReflectStruct,
) -> Result<proc_macro2::TokenStream, syn::Error> {
pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> proc_macro2::TokenStream {
let fqoption = FQOption.into_token_stream();

let bevy_reflect_path = reflect_struct.meta().bevy_reflect_path();
Expand Down Expand Up @@ -82,7 +80,7 @@ pub(crate) fn impl_struct(
}
};

let where_clause_options = reflect_struct.where_clause_options()?;
let where_clause_options = reflect_struct.where_clause_options();
let typed_impl = impl_typed(
reflect_struct.meta(),
&where_clause_options,
Expand All @@ -105,7 +103,7 @@ pub(crate) fn impl_struct(

let where_reflect_clause = extend_where_clause(where_clause, &where_clause_options);

Ok(quote! {
quote! {
#get_type_registration_impl

#typed_impl
Expand Down Expand Up @@ -246,5 +244,5 @@ pub(crate) fn impl_struct(

#debug_fn
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use quote::{quote, ToTokens};
use syn::{Index, Member};

/// Implements `TupleStruct`, `GetTypeRegistration`, and `Reflect` for the given derive data.
pub(crate) fn impl_tuple_struct(
reflect_struct: &ReflectStruct,
) -> Result<proc_macro2::TokenStream, syn::Error> {
pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> proc_macro2::TokenStream {
let fqoption = FQOption.into_token_stream();

let bevy_reflect_path = reflect_struct.meta().bevy_reflect_path();
Expand All @@ -22,7 +20,7 @@ pub(crate) fn impl_tuple_struct(
let field_count = field_idents.len();
let field_indices = (0..field_count).collect::<Vec<usize>>();

let where_clause_options = reflect_struct.where_clause_options()?;
let where_clause_options = reflect_struct.where_clause_options();
let get_type_registration_impl = reflect_struct.get_type_registration(&where_clause_options);

let hash_fn = reflect_struct
Expand Down Expand Up @@ -96,7 +94,7 @@ pub(crate) fn impl_tuple_struct(

let where_reflect_clause = extend_where_clause(where_clause, &where_clause_options);

Ok(quote! {
quote! {
#get_type_registration_impl

#typed_impl
Expand Down Expand Up @@ -215,5 +213,5 @@ pub(crate) fn impl_tuple_struct(

#debug_fn
}
})
}
}
8 changes: 4 additions & 4 deletions crates/bevy_reflect/bevy_reflect_derive/src/impls/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::ReflectMeta;
use quote::quote;

/// Implements `GetTypeRegistration` and `Reflect` for the given type data.
pub(crate) fn impl_value(meta: &ReflectMeta) -> Result<proc_macro2::TokenStream, syn::Error> {
pub(crate) fn impl_value(meta: &ReflectMeta) -> proc_macro2::TokenStream {
let bevy_reflect_path = meta.bevy_reflect_path();
let type_path = meta.type_path();

Expand All @@ -21,7 +21,7 @@ pub(crate) fn impl_value(meta: &ReflectMeta) -> Result<proc_macro2::TokenStream,
#[cfg(not(feature = "documentation"))]
let with_docs: Option<proc_macro2::TokenStream> = None;

let where_clause_options = WhereClauseOptions::new(meta)?;
let where_clause_options = WhereClauseOptions::new(meta);
let typed_impl = impl_typed(
meta,
&where_clause_options,
Expand All @@ -37,7 +37,7 @@ pub(crate) fn impl_value(meta: &ReflectMeta) -> Result<proc_macro2::TokenStream,
let where_reflect_clause = extend_where_clause(where_clause, &where_clause_options);
let get_type_registration_impl = meta.get_type_registration(&where_clause_options);

Ok(quote! {
quote! {
#get_type_registration_impl

#type_path_impl
Expand Down Expand Up @@ -124,5 +124,5 @@ pub(crate) fn impl_value(meta: &ReflectMeta) -> Result<proc_macro2::TokenStream,

#debug_fn
}
})
}
}
30 changes: 7 additions & 23 deletions crates/bevy_reflect/bevy_reflect_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,6 @@ pub fn derive_reflect(input: TokenStream) -> TokenStream {
),
};

let reflect_impls = reflect_impls.unwrap_or_else(syn::Error::into_compile_error);
let from_reflect_impl =
from_reflect_impl.map(|impls| impls.unwrap_or_else(syn::Error::into_compile_error));

TokenStream::from(quote! {
#reflect_impls
#from_reflect_impl
Expand Down Expand Up @@ -260,7 +256,6 @@ pub fn derive_from_reflect(input: TokenStream) -> TokenStream {
ReflectDerive::Enum(meta) => from_reflect::impl_enum(&meta),
ReflectDerive::Value(meta) => from_reflect::impl_value(&meta),
}
.unwrap_or_else(syn::Error::into_compile_error)
.into()
}

Expand All @@ -287,10 +282,7 @@ pub fn derive_type_path(input: TokenStream) -> TokenStream {
Err(err) => return err.into_compile_error().into(),
};

let where_clause_options = match WhereClauseOptions::new_type_path(derive_data.meta()) {
Ok(options) => options,
Err(err) => return err.into_compile_error().into(),
};
let where_clause_options = WhereClauseOptions::new_type_path(derive_data.meta());

impls::impl_type_path(
derive_data.meta(),
Expand Down Expand Up @@ -411,9 +403,8 @@ pub fn impl_reflect_value(input: TokenStream) -> TokenStream {
#[cfg(feature = "documentation")]
let meta = meta.with_docs(documentation::Documentation::from_attributes(&def.attrs));

let reflect_impls = impls::impl_value(&meta).unwrap_or_else(syn::Error::into_compile_error);
let from_reflect_impl =
from_reflect::impl_value(&meta).unwrap_or_else(syn::Error::into_compile_error);
let reflect_impls = impls::impl_value(&meta);
let from_reflect_impl = from_reflect::impl_value(&meta);

TokenStream::from(quote! {
#reflect_impls
Expand Down Expand Up @@ -472,10 +463,8 @@ pub fn impl_reflect_struct(input: TokenStream) -> TokenStream {
.into();
}

let impl_struct =
impls::impl_struct(&struct_data).unwrap_or_else(syn::Error::into_compile_error);
let impl_from_struct = from_reflect::impl_struct(&struct_data)
.unwrap_or_else(syn::Error::into_compile_error);
let impl_struct = impls::impl_struct(&struct_data);
let impl_from_struct = from_reflect::impl_struct(&struct_data);

TokenStream::from(quote! {
#impl_struct
Expand Down Expand Up @@ -538,9 +527,7 @@ pub fn impl_from_reflect_value(input: TokenStream) -> TokenStream {
}
};

from_reflect::impl_value(&ReflectMeta::new(type_path, def.traits.unwrap_or_default()))
.unwrap_or_else(syn::Error::into_compile_error)
.into()
from_reflect::impl_value(&ReflectMeta::new(type_path, def.traits.unwrap_or_default())).into()
}

/// A replacement for [deriving `TypePath`] for use on foreign types.
Expand Down Expand Up @@ -601,10 +588,7 @@ pub fn impl_type_path(input: TokenStream) -> TokenStream {
};

let meta = ReflectMeta::new(type_path, ReflectTraits::default());
let where_clause_options = match WhereClauseOptions::new_type_path(&meta) {
Ok(options) => options,
Err(err) => return err.into_compile_error().into(),
};
let where_clause_options = WhereClauseOptions::new_type_path(&meta);

impls::impl_type_path(&meta, &where_clause_options).into()
}
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_reflect/bevy_reflect_derive/src/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl Default for WhereClauseOptions {

impl WhereClauseOptions {
/// Create [`WhereClauseOptions`] for a reflected struct or enum type.
pub fn new(meta: &ReflectMeta) -> Result<Self, syn::Error> {
pub fn new(meta: &ReflectMeta) -> Self {
let bevy_reflect_path = meta.bevy_reflect_path();
let is_from_reflect = meta.from_reflect().should_auto_derive();

Expand All @@ -104,7 +104,7 @@ impl WhereClauseOptions {
}

/// Create [`WhereClauseOptions`] with the minimum bounds needed to fulfill `TypePath`.
pub fn new_type_path(meta: &ReflectMeta) -> Result<Self, syn::Error> {
pub fn new_type_path(meta: &ReflectMeta) -> Self {
let bevy_reflect_path = meta.bevy_reflect_path();

Self::new_with_bounds(
Expand All @@ -122,7 +122,7 @@ impl WhereClauseOptions {
meta: &ReflectMeta,
active_bounds: impl Fn(&TypeParam) -> Option<proc_macro2::TokenStream>,
ignored_bounds: impl Fn(&TypeParam) -> Option<proc_macro2::TokenStream>,
) -> Result<Self, syn::Error> {
) -> Self {
let mut options = WhereClauseOptions::default();

for param in meta.type_path().generics().type_params() {
Expand All @@ -142,7 +142,7 @@ impl WhereClauseOptions {
}
}

Ok(options)
options
}
}

Expand Down

0 comments on commit b5f8c15

Please sign in to comment.