Skip to content

Fix: remove unnecessary unsafe of ErasedBundleTemplate::apply - #25279

Open
yilin0518 wants to merge 1 commit into
bevyengine:mainfrom
yilin0518:fix_unsafe_apply
Open

Fix: remove unnecessary unsafe of ErasedBundleTemplate::apply#25279
yilin0518 wants to merge 1 commit into
bevyengine:mainfrom
yilin0518:fix_unsafe_apply

Conversation

@yilin0518

Copy link
Copy Markdown
Contributor

Objective

ErasedBundleTemplate::apply in bevy_scene was declared unsafe fn, but its # Safety contract was stale and unenforceable. The documented precondition referenced a bundle_writer parameter that this method does not have — the wording seems copied from ErasedComponentTemplate::apply, which does take a bundle_writer: &mut BundleWriter and uses it inside an unsafe call. On ErasedBundleTemplate::apply there is no bundle_writer, so the stated caller-side obligation cannot be satisfied or verified.

The default blanket impl only performs safe operations:

impl<T: Template<Output: Bundle> + Send + Sync + 'static> ErasedBundleTemplate for T {
    unsafe fn apply(&self, context: &mut TemplateContext) -> Result<(), BevyError> {
        let bundle = self.build_template(context)?;
        context.entity.insert(bundle);
        Ok(())
    }
}

build_template and EntityWorldMut::insert are both safe, so the unsafe marker imposed a caller obligation the body never needed.

Solution

Drop the unnecessary unsafe and remove the stale # Safety documentation, since no real caller-verifiable invariant exists for this method:

Testing

The computer I use don't install the msvc, so I use the following command to test

  • cargo +nightly-x86_64-pc-windows-gnu build -p bevy_scene: Successfully build
  • cargo +nightly-x86_64-pc-windows-gnu test -p bevy_scene: Pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant