Skip to content

Commit 66d6322

Browse files
committed
Typed: TypePath and where clause lifetime bounds
1 parent 5de21c0 commit 66d6322

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

crates/bevy_reflect/bevy_reflect_derive/src/utility.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use bevy_macro_utils::BevyManifest;
55
use bit_set::BitSet;
66
use proc_macro2::{Ident, Span};
77
use quote::{quote, ToTokens};
8-
use syn::{spanned::Spanned, LitStr, Member, Path, Type, WhereClause};
8+
use syn::{spanned::Spanned, Lifetime, LitStr, Member, Path, Type, WhereClause};
99

1010
/// Returns the correct path for `bevy_reflect`.
1111
pub(crate) fn get_bevy_reflect_path() -> Path {
@@ -60,6 +60,10 @@ pub(crate) fn ident_or_index(ident: Option<&Ident>, index: usize) -> Member {
6060

6161
/// Options defining how to extend the `where` clause in reflection with any additional bounds needed.
6262
pub(crate) struct WhereClauseOptions {
63+
/// Lifetime parameters that need extra trait bounds.
64+
pub(crate) parameter_lifetimes: Box<[Lifetime]>,
65+
/// Bounds to add to the lifetime parameters.
66+
pub(crate) parameter_lifetime_bounds: proc_macro2::TokenStream,
6367
/// Type parameters that need extra trait bounds.
6468
pub(crate) parameter_types: Box<[Ident]>,
6569
/// Trait bounds to add to the type parameters.
@@ -79,6 +83,13 @@ impl WhereClauseOptions {
7983
pub fn type_path_bounds(meta: &ReflectMeta) -> Self {
8084
let bevy_reflect_path = meta.bevy_reflect_path();
8185
Self {
86+
parameter_lifetimes: meta
87+
.type_path()
88+
.generics()
89+
.lifetimes()
90+
.map(|param| param.lifetime.clone())
91+
.collect(),
92+
parameter_lifetime_bounds: quote! { 'static },
8293
parameter_types: meta
8394
.type_path()
8495
.generics()
@@ -95,10 +106,12 @@ impl Default for WhereClauseOptions {
95106
/// By default, don't add any additional bounds to the `where` clause
96107
fn default() -> Self {
97108
Self {
109+
parameter_lifetimes: Box::new([]),
98110
parameter_types: Box::new([]),
99111
active_types: Box::new([]),
100112
ignored_types: Box::new([]),
101113
parameter_trait_bounds: quote! {},
114+
parameter_lifetime_bounds: quote! {},
102115
active_trait_bounds: quote! {},
103116
ignored_trait_bounds: quote! {},
104117
}
@@ -140,17 +153,23 @@ pub(crate) fn extend_where_clause(
140153
where_clause: Option<&WhereClause>,
141154
where_clause_options: &WhereClauseOptions,
142155
) -> proc_macro2::TokenStream {
156+
let parameter_lifetimes = &where_clause_options.parameter_lifetimes;
143157
let parameter_types = &where_clause_options.parameter_types;
144158
let active_types = &where_clause_options.active_types;
145159
let ignored_types = &where_clause_options.ignored_types;
160+
let parameter_lifetime_bounds = &where_clause_options.parameter_lifetime_bounds;
146161
let parameter_trait_bounds = &where_clause_options.parameter_trait_bounds;
147162
let active_trait_bounds = &where_clause_options.active_trait_bounds;
148163
let ignored_trait_bounds = &where_clause_options.ignored_trait_bounds;
149164

150165
let mut generic_where_clause = if let Some(where_clause) = where_clause {
151166
let predicates = where_clause.predicates.iter();
152167
quote! {where #(#predicates,)*}
153-
} else if !(parameter_types.is_empty() && active_types.is_empty() && ignored_types.is_empty()) {
168+
} else if !(parameter_lifetimes.is_empty()
169+
&& parameter_types.is_empty()
170+
&& active_types.is_empty()
171+
&& ignored_types.is_empty())
172+
{
154173
quote! {where}
155174
} else {
156175
quote!()
@@ -161,6 +180,7 @@ pub(crate) fn extend_where_clause(
161180
// the whole bound by default, resulting in a failure to prove trait
162181
// adherence.
163182
generic_where_clause.extend(quote! {
183+
#(#parameter_lifetimes: #parameter_lifetime_bounds,)*
164184
#((#active_types): #active_trait_bounds,)*
165185
#((#ignored_types): #ignored_trait_bounds,)*
166186
// Leave parameter bounds to the end for more sane error messages.

crates/bevy_reflect/src/type_info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ use std::fmt::Debug;
7171
/// ```
7272
///
7373
/// [utility]: crate::utility
74-
pub trait Typed: Reflect {
74+
pub trait Typed: Reflect + TypePath {
7575
/// Returns the compile-time [info] for the underlying type.
7676
///
7777
/// [info]: TypeInfo

0 commit comments

Comments
 (0)