Skip to content

Commit

Permalink
add explicit panic with help message and properly failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
RobWalt committed Jul 8, 2024
1 parent 0ab9bec commit 215b906
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
4 changes: 2 additions & 2 deletions proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ fn impl_trait_query(arg: TokenStream, item: TokenStream) -> Result<TokenStream2>
#[inline]
fn get_state(_: &#imports::Components) -> Option<Self::State> {
// TODO: fix this https://github.com/bevyengine/bevy/issues/13798
None
panic!("transmuting and any other operations concerning the state of a query are currently broken and shouldn't be used. See https://github.com/JoJoJet/bevy-trait-query/issues/59");
}

#[inline]
Expand Down Expand Up @@ -341,7 +341,7 @@ fn impl_trait_query(arg: TokenStream, item: TokenStream) -> Result<TokenStream2>
#[inline]
fn get_state(_: &#imports::Components) -> Option<Self::State> {
// TODO: fix this https://github.com/bevyengine/bevy/issues/13798
None
panic!("transmuting and any other operations concerning the state of a query are currently broken and shouldn't be used. See https://github.com/JoJoJet/bevy-trait-query/issues/59");
}

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions src/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> WorldQuery for All<&'a Trait> {
#[inline]
fn get_state(_: &Components) -> Option<Self::State> {
// TODO: fix this https://github.com/bevyengine/bevy/issues/13798
None
panic!("transmuting and any other operations concerning the state of a query are currently broken and shouldn't be used. See https://github.com/JoJoJet/bevy-trait-query/issues/59");
}

#[inline]
Expand Down Expand Up @@ -673,7 +673,7 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> WorldQuery for All<&'a mut Trait> {
#[inline]
fn get_state(_: &Components) -> Option<Self::State> {
// TODO: fix this https://github.com/bevyengine/bevy/issues/13798
None
panic!("transmuting and any other operations concerning the state of a query are currently broken and shouldn't be used. See https://github.com/JoJoJet/bevy-trait-query/issues/59");
}

#[inline]
Expand Down
10 changes: 5 additions & 5 deletions src/one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> WorldQuery for One<&'a Trait> {
#[inline]
fn get_state(_: &Components) -> Option<Self::State> {
// TODO: fix this https://github.com/bevyengine/bevy/issues/13798
None
panic!("transmuting and any other operations concerning the state of a query are currently broken and shouldn't be used. See https://github.com/JoJoJet/bevy-trait-query/issues/59");
}

#[inline]
Expand Down Expand Up @@ -432,7 +432,7 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> WorldQuery for One<&'a mut Trait> {
#[inline]
fn get_state(_: &Components) -> Option<Self::State> {
// TODO: fix this https://github.com/bevyengine/bevy/issues/13798
None
panic!("transmuting and any other operations concerning the state of a query are currently broken and shouldn't be used. See https://github.com/JoJoJet/bevy-trait-query/issues/59");
}

#[inline]
Expand Down Expand Up @@ -590,7 +590,7 @@ unsafe impl<Trait: ?Sized + TraitQuery> WorldQuery for OneAdded<Trait> {
#[inline]
fn get_state(_: &Components) -> Option<Self::State> {
// TODO: fix this https://github.com/bevyengine/bevy/issues/13798
None
panic!("transmuting and any other operations concerning the state of a query are currently broken and shouldn't be used. See https://github.com/JoJoJet/bevy-trait-query/issues/59");
}

fn matches_component_set(
Expand Down Expand Up @@ -737,7 +737,7 @@ unsafe impl<Trait: ?Sized + TraitQuery> WorldQuery for OneChanged<Trait> {
#[inline]
fn get_state(_: &Components) -> Option<Self::State> {
// TODO: fix this https://github.com/bevyengine/bevy/issues/13798
None
panic!("transmuting and any other operations concerning the state of a query are currently broken and shouldn't be used. See https://github.com/JoJoJet/bevy-trait-query/issues/59");
}

fn matches_component_set(
Expand Down Expand Up @@ -846,7 +846,7 @@ unsafe impl<Trait: ?Sized + TraitQuery> WorldQuery for WithOne<Trait> {
#[inline]
fn get_state(_: &Components) -> Option<Self::State> {
// TODO: fix this https://github.com/bevyengine/bevy/issues/13798
None
panic!("transmuting and any other operations concerning the state of a query are currently broken and shouldn't be used. See https://github.com/JoJoJet/bevy-trait-query/issues/59");
}

#[inline]
Expand Down
31 changes: 30 additions & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ fn query_and_transmute_and_print(
}

#[test]
fn transmute_panics() {
fn transmute_doesnt_panic_if_no_trait_touched() {
let mut world = World::new();
world.init_resource::<Output>();
world
Expand All @@ -712,3 +712,32 @@ fn transmute_panics() {

assert_eq!(world.resource::<Output>().0, &["0v1", "2v1", "3v1"]);
}

fn query_and_transmute_and_print_panic(
mut people: Query<(Entity, One<&dyn Person>)>,
mut output: ResMut<Output>,
) {
for person in people.transmute_lens::<One<&dyn Person>>().query().iter() {
output.0.push(person.name().to_string());
}
}

#[test]
#[should_panic]
fn transmute_panics_if_trait_touched() {
let mut world = World::new();
world.init_resource::<Output>();
world
.register_component_as::<dyn Person, Human>()
.register_component_as::<dyn Person, Dolphin>();

world.spawn(Human("Garbanzo".to_owned(), 7));
world.spawn((Human("Garbanzo".to_owned(), 7), Dolphin(47)));
world.spawn((Human("Garbanzo".to_owned(), 14), Fem));
world.spawn(Dolphin(27));

let mut schedule = Schedule::default();
schedule.add_systems(query_and_transmute_and_print_panic);

schedule.run(&mut world);
}

0 comments on commit 215b906

Please sign in to comment.