Skip to content

Commit 9dea402

Browse files
committed
Docs and trigger_hooks cleanup
1 parent 8e09d06 commit 9dea402

File tree

2 files changed

+60
-39
lines changed

2 files changed

+60
-39
lines changed

crates/bevy_ecs/src/bundle.rs

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -637,26 +637,31 @@ impl<'w> BundleInserter<'w> {
637637
location: EntityLocation,
638638
bundle: T,
639639
) -> EntityLocation {
640-
/// # Safety
641-
/// Must
642-
unsafe fn trigger_hooks(
640+
#[inline]
641+
fn trigger_hooks(
643642
entity: Entity,
644643
bundle_info: &BundleInfo,
645644
add_bundle: &AddBundle,
646645
archetype: &Archetype,
647646
mut world: DeferredWorld,
648647
) {
649648
if archetype.has_on_add() {
650-
world.trigger_on_add(
651-
entity,
652-
bundle_info
653-
.iter_components()
654-
.zip(add_bundle.bundle_status.iter())
655-
.filter(|(_, &status)| status == ComponentStatus::Added)
656-
.map(|(id, _)| id),
657-
);
649+
// SAFETY: All components in the bundle are guarenteed to exist in the World
650+
// as they must be initialized before creating the BundleInfo.
651+
unsafe {
652+
world.trigger_on_add(
653+
entity,
654+
bundle_info
655+
.iter_components()
656+
.zip(add_bundle.bundle_status.iter())
657+
.filter(|(_, &status)| status == ComponentStatus::Added)
658+
.map(|(id, _)| id),
659+
);
660+
}
658661
}
659662
if archetype.has_on_insert() {
663+
// SAFETY: All components in the bundle are guarenteed to exist in the World
664+
// as they must be initialized before creating the BundleInfo.
660665
unsafe { world.trigger_on_insert(entity, bundle_info.iter_components()) }
661666
}
662667
}
@@ -686,15 +691,8 @@ impl<'w> BundleInserter<'w> {
686691
);
687692
}
688693
// SAFETY: We have no outstanding mutable references to world as they were dropped
689-
unsafe {
690-
trigger_hooks(
691-
entity,
692-
bundle_info,
693-
add_bundle,
694-
archetype,
695-
self.world.into_deferred(),
696-
);
697-
}
694+
let deferred_world = unsafe { self.world.into_deferred() };
695+
trigger_hooks(entity, bundle_info, add_bundle, archetype, deferred_world);
698696
location
699697
}
700698
InsertBundleResult::NewArchetypeSameTable { new_archetype } => {
@@ -737,15 +735,8 @@ impl<'w> BundleInserter<'w> {
737735
};
738736

739737
// SAFETY: We have no outstanding mutable references to world as they were dropped
740-
unsafe {
741-
trigger_hooks(
742-
entity,
743-
bundle_info,
744-
add_bundle,
745-
archetype,
746-
self.world.into_deferred(),
747-
);
748-
}
738+
let deferred_world = unsafe { self.world.into_deferred() };
739+
trigger_hooks(entity, bundle_info, add_bundle, archetype, deferred_world);
749740

750741
new_location
751742
}
@@ -839,15 +830,8 @@ impl<'w> BundleInserter<'w> {
839830
};
840831

841832
// SAFETY: We have no outstanding mutable references to world as they were dropped
842-
unsafe {
843-
trigger_hooks(
844-
entity,
845-
bundle_info,
846-
add_bundle,
847-
archetype,
848-
self.world.into_deferred(),
849-
);
850-
}
833+
let deferred_world = unsafe { self.world.into_deferred() };
834+
trigger_hooks(entity, bundle_info, add_bundle, archetype, deferred_world);
851835

852836
new_location
853837
}

crates/bevy_ptr/src/lib.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,45 @@ mod sealed {
3333
pub struct ConstNonNull<T: ?Sized>(NonNull<T>);
3434

3535
impl<T: ?Sized> ConstNonNull<T> {
36+
/// Returns a shared reference to the value.
37+
///
38+
/// # Safety
39+
///
40+
/// When calling this method, you have to ensure that all of the following is true:
41+
///
42+
/// * The pointer must be properly aligned.
43+
///
44+
/// * It must be "dereferenceable" in the sense defined in [the module documentation].
45+
///
46+
/// * The pointer must point to an initialized instance of `T`.
47+
///
48+
/// * You must enforce Rust's aliasing rules, since the returned lifetime `'a` is
49+
/// arbitrarily chosen and does not necessarily reflect the actual lifetime of the data.
50+
/// In particular, while this reference exists, the memory the pointer points to must
51+
/// not get mutated (except inside `UnsafeCell`).
52+
///
53+
/// This applies even if the result of this method is unused!
54+
/// (The part about being initialized is not yet fully decided, but until
55+
/// it is, the only safe approach is to ensure that they are indeed initialized.)
56+
///
57+
/// # Examples
58+
///
59+
/// ```
60+
/// use std::ptr::NonNull;
61+
///
62+
/// let mut x = 0u32;
63+
/// let ptr = NonNull::new(&mut x as *mut _).expect("ptr is null!");
64+
///
65+
/// let ref_x = unsafe { ptr.as_ref() };
66+
/// println!("{ref_x}");
67+
/// ```
68+
///
69+
/// [the module documentation]: core::ptr#safety
70+
#[inline]
3671
pub unsafe fn as_ref<'a>(&self) -> &'a T {
37-
self.0.as_ref()
72+
// SAFETY: This function's safety invariants are identical to `NonNull::as_ref`
73+
// The caller must satsify all of them.
74+
unsafe { self.0.as_ref() }
3875
}
3976
}
4077

0 commit comments

Comments
 (0)