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

[Merged by Bors] - Use ManuallyDrop instead of forget in insert_resource_with_id #2947

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::{
use std::{
any::TypeId,
fmt,
mem::ManuallyDrop,
sync::atomic::{AtomicU32, Ordering},
};

Expand Down Expand Up @@ -999,13 +1000,13 @@ impl World {
/// # Safety
/// `component_id` must be valid and correspond to a resource component of type T
#[inline]
unsafe fn insert_resource_with_id<T>(&mut self, component_id: ComponentId, mut value: T) {
unsafe fn insert_resource_with_id<T>(&mut self, component_id: ComponentId, value: T) {
Copy link
Member

Choose a reason for hiding this comment

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

This no longer needing mut T is really nice; it makes me much more confident in the semantics of the change being correct.

let change_tick = self.change_tick();
let column = self.initialize_resource_internal(component_id);
if column.is_empty() {
let mut value = ManuallyDrop::new(value);
// SAFE: column is of type T and has been allocated above
let data = (&mut value as *mut T).cast::<u8>();
std::mem::forget(value);
let data = (&mut *value as *mut T).cast::<u8>();
column.push(data, ComponentTicks::new(change_tick));
} else {
// SAFE: column is of type T and has already been allocated
Expand Down