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

Implement opt-in change detection bypass #2363

Closed
wants to merge 2 commits 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
28 changes: 28 additions & 0 deletions crates/bevy_ecs/src/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ pub trait DetectChanges {
fn set_changed(&mut self);
}

// TODO: Add some nice docs here
Copy link
Member Author

Choose a reason for hiding this comment

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

If someone would contribute to this point, it'd be highly appreciated

Copy link
Member

Choose a reason for hiding this comment

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

/// When implemented for a component or resource type, this trait allows end users to manually bypass change detection within the ECS using the [`get_untracked`] method on `Query` and `World`.

I think that's where those methods live?

Copy link
Member

Choose a reason for hiding this comment

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

I don't see the get_untracked methods in Query or World.

Are they found elsewhere? Or are they part of an unmerged PR?

Copy link
Member

Choose a reason for hiding this comment

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

It was a "please add these methods too" comment for @TheRawMeatball ;)

pub trait AllowGetUntracked<Marker = ()> {}

macro_rules! change_detection_impl {
($name:ident < $( $generics:tt ),+ >, $target:ty, $($traits:ident)?) => {
impl<$($generics),* $(: $traits)?> DetectChanges for $name<$($generics),*> {
Expand Down Expand Up @@ -116,6 +119,28 @@ macro_rules! impl_into_inner {
};
}

macro_rules! impl_change_detection_circumvention {
($name:ident < $( $generics:tt ),+ >, $target:ty, $($traits:ident)?) => {
impl<$($generics),* $(: $traits)?> $name<$($generics),*> {
#[inline]
pub fn get_untracked(&mut self) -> &mut T
where
T: AllowGetUntracked,
{
self.value
}

#[inline]
pub fn get_untracked_with_token<Token>(&mut self, _: Token) -> &mut T
where
T: AllowGetUntracked<Token>,
{
self.value
}
}
};
}

macro_rules! impl_debug {
($name:ident < $( $generics:tt ),+ >, $($traits:ident)?) => {
impl<$($generics),* $(: $traits)?> std::fmt::Debug for $name<$($generics),*>
Expand Down Expand Up @@ -155,6 +180,7 @@ pub struct ResMut<'a, T: Resource> {

change_detection_impl!(ResMut<'a, T>, T, Resource);
impl_into_inner!(ResMut<'a, T>, T, Resource);
impl_change_detection_circumvention!(ResMut<'a, T>, T, Resource);
impl_debug!(ResMut<'a, T>, Resource);

/// Unique borrow of a non-[`Send`] resource.
Expand All @@ -176,6 +202,7 @@ pub struct NonSendMut<'a, T: 'static> {

change_detection_impl!(NonSendMut<'a, T>, T,);
impl_into_inner!(NonSendMut<'a, T>, T,);
impl_change_detection_circumvention!(NonSendMut<'a, T>, T,);
impl_debug!(NonSendMut<'a, T>,);

/// Unique mutable borrow of an entity's component
Expand All @@ -186,6 +213,7 @@ pub struct Mut<'a, T> {

change_detection_impl!(Mut<'a, T>, T,);
impl_into_inner!(Mut<'a, T>, T,);
impl_change_detection_circumvention!(Mut<'a, T>, T,);
impl_debug!(Mut<'a, T>,);

/// Unique mutable borrow of a Reflected component
Expand Down