Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into mixed-lighting
Browse files Browse the repository at this point in the history
  • Loading branch information
pcwalton committed Dec 11, 2024
2 parents fd04cbf + e5d7fb4 commit 4afac19
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
42 changes: 34 additions & 8 deletions crates/bevy_animation/src/animation_curves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,18 +243,26 @@ where

impl<C: Typed, P, F: Fn(&mut C) -> &mut P + 'static> AnimatedField<C, P, F> {
/// Creates a new instance of [`AnimatedField`]. This operates under the assumption that
/// `C` is a reflect-able struct with named fields, and that `field_name` is a valid field on that struct.
/// `C` is a reflect-able struct, and that `field_name` is a valid field on that struct.
///
/// # Panics
/// If the type of `C` is not a struct with named fields or if the `field_name` does not exist.
/// If the type of `C` is not a struct or if the `field_name` does not exist.
pub fn new_unchecked(field_name: &str, func: F) -> Self {
let TypeInfo::Struct(struct_info) = C::type_info() else {
let field_index;
if let TypeInfo::Struct(struct_info) = C::type_info() {
field_index = struct_info
.index_of(field_name)
.expect("Field name should exist");
} else if let TypeInfo::TupleStruct(struct_info) = C::type_info() {
field_index = field_name
.parse()
.expect("Field name should be a valid tuple index");
if field_index >= struct_info.field_len() {
panic!("Field name should be a valid tuple index");
}
} else {
panic!("Only structs are supported in `AnimatedField::new_unchecked`")
};

let field_index = struct_info
.index_of(field_name)
.expect("Field name should exist");
}

Self {
func,
Expand Down Expand Up @@ -984,3 +992,21 @@ macro_rules! animated_field {
})
};
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_animated_field_tuple_struct_simple_uses() {
#[derive(Clone, Debug, Component, Reflect)]
struct A(f32);
let _ = AnimatedField::new_unchecked("0", |a: &mut A| &mut a.0);

#[derive(Clone, Debug, Component, Reflect)]
struct B(f32, f64, f32);
let _ = AnimatedField::new_unchecked("0", |b: &mut B| &mut b.0);
let _ = AnimatedField::new_unchecked("1", |b: &mut B| &mut b.1);
let _ = AnimatedField::new_unchecked("2", |b: &mut B| &mut b.2);
}
}
5 changes: 4 additions & 1 deletion deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ all-features = true

[advisories]
version = 2
ignore = []
ignore = [
# TODO: #16477 - Delete this once notify-types has been bumped.
"RUSTSEC-2024-0384",
]

[licenses]
version = 2
Expand Down

0 comments on commit 4afac19

Please sign in to comment.