Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change World::try_despawn and World::try_insert_batch to return Result #17376

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

JaySpruce
Copy link
Contributor

Objective

Most try methods on World return a Result, but try_despawn and try_insert_batch don't. Since Bevy's error handling is advancing, these should be brought in line.

Solution

  • Added TryDespawnError and TryInsertBatchError.
  • try_despawn, try_insert_batch, and try_insert_batch_if_new now return their respective errors.
  • Modified internals of try_insert_batch_with_caller to support collecting invalid entities.

Migration Guide

  • World::try_despawn now returns a Result rather than a bool.
  • World::try_insert_batch and World::try_insert_batch_if_new now return a Result where they previously returned nothing.

@alice-i-cecile alice-i-cecile added this to the 0.16 milestone Jan 14, 2025
@BenjaminBrienen BenjaminBrienen added C-Feature A new feature, making something new possible A-ECS Entities, components, systems, and events D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jan 14, 2025
Comment on lines +2616 to +2617
// This is effectively in an `else` block, since it can only be reached if the
// above `if` fails.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// This is effectively in an `else` block, since it can only be reached if the
// above `if` fails.

/// [`World::try_despawn`]: crate::world::World::try_despawn
#[derive(Error, Debug, Clone, Copy)]
#[error("Could not despawn the entity with ID {0} because it {1}")]
pub struct TryDespawnError(pub Entity, pub EntityDoesNotExistDetails);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Named structs instead of tuple structs please. Operations on tuple structs are much less clear, so we avoid them as a matter of style unless they're 1-element newtypes.

@@ -2332,7 +2354,7 @@ impl World {
///
/// This function will panic if any of the associated entities do not exist.
///
/// For the non-panicking version, see [`World::try_insert_batch`].
/// For the fallible version, see [`World::try_insert_batch`].
#[track_caller]
pub fn insert_batch<I, B>(&mut self, batch: I)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to merge all of the fallible / infallible variants, but that should be done in follow-up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Feature A new feature, making something new possible D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants