Skip to content

Commit

Permalink
migrate to bevy 0.15
Browse files Browse the repository at this point in the history
  • Loading branch information
RobWalt committed Oct 24, 2024
1 parent 215b906 commit deb3063
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 84 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ bevy-trait-query-impl = { path = "proc-macro", version = "0.5.0" }
tracing = "0.1"

[dependencies.bevy_ecs]
version = "0.14.0"
version = "0.15.0-rc.1"

[dependencies.bevy_app]
version = "0.14.0"
version = "0.15.0-rc.1"
optional = true

[dependencies.bevy_core]
version = "0.14.0"
version = "0.15.0-rc.1"
optional = true

[dev-dependencies]
criterion = "0.5"

[dev-dependencies.bevy]
version = "0.14.0"
version = "0.15.0-rc.1"
default-features = false

[[bench]]
Expand Down
10 changes: 10 additions & 0 deletions proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ fn impl_trait_query(arg: TokenStream, item: TokenStream) -> Result<TokenStream2>
) -> bool {
<#my_crate::All<&#trait_object> as #imports::WorldQuery>::matches_component_set(state, set_contains_id)
}

#[inline]
fn shrink_fetch<'wlong: 'wshort, 'wshort>(fetch: Self::Fetch<'wlong>) -> Self::Fetch<'wshort> {
fetch
}
}

unsafe impl #impl_generics_with_lifetime #imports::QueryData for &'__a mut #trait_object
Expand Down Expand Up @@ -351,6 +356,11 @@ fn impl_trait_query(arg: TokenStream, item: TokenStream) -> Result<TokenStream2>
) -> bool {
<#my_crate::All<&mut #trait_object> as #imports::WorldQuery>::matches_component_set(state, set_contains_id)
}

#[inline]
fn shrink_fetch<'wlong: 'wshort, 'wshort>(fetch: Self::Fetch<'wlong>) -> Self::Fetch<'wshort> {
fetch
}
}
};

Expand Down
93 changes: 55 additions & 38 deletions src/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,26 @@ impl<'a, Trait: ?Sized + TraitQuery> Iterator for ReadTableTraitsIter<'a, Trait>
fn next(&mut self) -> Option<Self::Item> {
// Iterate the remaining table components that are registered,
// until we find one that exists in the table.
let (column, meta) = unsafe { zip_exact(&mut self.components, &mut self.meta) }
.find_map(|(&component, meta)| self.table.get_column(component).zip(Some(meta)))?;
// SAFETY: We have shared access to the entire column.
let ptr = unsafe {
column
.get_data_ptr()
.byte_add(self.table_row.as_usize() * meta.size_bytes)
};
let (ptr, component, meta) = unsafe { zip_exact(&mut self.components, &mut self.meta) }
.find_map(|(&component, meta)| {
// SAFETY: we know that the `table_row` is a valid index.
let ptr = unsafe { self.table.get_component(component, self.table_row) }?;
Some((ptr, component, meta))
})?;
let trait_object = unsafe { meta.dyn_ctor.cast(ptr) };

// SAFETY: we know that the `table_row` is a valid index.
// SAFETY:
// Read access has been registered, so we can dereference it immutably.
let added_tick = unsafe { column.get_added_tick_unchecked(self.table_row).deref() };
let changed_tick = unsafe { column.get_changed_tick_unchecked(self.table_row).deref() };
let added_tick = unsafe {
self.table
.get_added_tick(component, self.table_row)?
.deref()
};
let changed_tick = unsafe {
self.table
.get_changed_tick(component, self.table_row)?
.deref()
};

Some(Ref::new(
trait_object,
Expand Down Expand Up @@ -94,13 +100,12 @@ impl<'a, Trait: ?Sized + TraitQuery> Iterator for ReadSparseTraitsIter<'a, Trait
fn next(&mut self) -> Option<Self::Item> {
// Iterate the remaining sparse set components that are registered,
// until we find one that exists in the archetype.
let ((ptr, ticks_ptr), meta) = unsafe { zip_exact(&mut self.components, &mut self.meta) }
let (ptr, ticks_ptr, meta) = unsafe { zip_exact(&mut self.components, &mut self.meta) }
.find_map(|(&component, meta)| {
self.sparse_sets
.get(component)
.and_then(|set| set.get_with_ticks(self.entity))
.zip(Some(meta))
})?;
let set = self.sparse_sets.get(component)?;
let (ptr, ticks, _) = set.get_with_ticks(self.entity)?;
Some((ptr, ticks, meta))
})?;
let trait_object = unsafe { meta.dyn_ctor.cast(ptr) };
let added_tick = unsafe { ticks_ptr.added.deref() };
let changed_tick = unsafe { ticks_ptr.changed.deref() };
Expand Down Expand Up @@ -241,13 +246,12 @@ impl<'a, Trait: ?Sized + TraitQuery> Iterator for WriteTableTraitsIter<'a, Trait
fn next(&mut self) -> Option<Self::Item> {
// Iterate the remaining table components that are registered,
// until we find one that exists in the table.
let (column, meta) = unsafe { zip_exact(&mut self.components, &mut self.meta) }
.find_map(|(&component, meta)| self.table.get_column(component).zip(Some(meta)))?;
let ptr = unsafe {
column
.get_data_ptr()
.byte_add(self.table_row.as_usize() * meta.size_bytes)
};
let (ptr, component, meta) = unsafe { zip_exact(&mut self.components, &mut self.meta) }
.find_map(|(&component, meta)| {
// SAFETY: we know that the `table_row` is a valid index.
let ptr = unsafe { self.table.get_component(component, self.table_row) }?;
Some((ptr, component, meta))
})?;
// SAFETY: The instance of `WriteTraits` that created this iterator
// has exclusive access to all table components registered with the trait.
//
Expand All @@ -257,10 +261,14 @@ impl<'a, Trait: ?Sized + TraitQuery> Iterator for WriteTableTraitsIter<'a, Trait
let trait_object = unsafe { meta.dyn_ctor.cast_mut(ptr) };
// SAFETY: We have exclusive access to the component, so by extension
// we have exclusive access to the corresponding `ComponentTicks`.
let added = unsafe { column.get_added_tick_unchecked(self.table_row).deref_mut() };
let added = unsafe {
self.table
.get_added_tick(component, self.table_row)?
.deref_mut()
};
let changed = unsafe {
column
.get_changed_tick_unchecked(self.table_row)
self.table
.get_changed_tick(component, self.table_row)?
.deref_mut()
};
Some(Mut::new(
Expand Down Expand Up @@ -291,13 +299,12 @@ impl<'a, Trait: ?Sized + TraitQuery> Iterator for WriteSparseTraitsIter<'a, Trai
fn next(&mut self) -> Option<Self::Item> {
// Iterate the remaining sparse set components we have registered,
// until we find one that exists in the archetype.
let ((ptr, component_ticks), meta) =
let (ptr, component_ticks, meta) =
unsafe { zip_exact(&mut self.components, &mut self.meta) }.find_map(
|(&component, meta)| {
self.sparse_sets
.get(component)
.and_then(|set| set.get_with_ticks(self.entity))
.zip(Some(meta))
let set = self.sparse_sets.get(component)?;
let (ptr, ticks, _) = set.get_with_ticks(self.entity)?;
Some((ptr, ticks, meta))
},
)?;

Expand Down Expand Up @@ -526,18 +533,18 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> WorldQuery for All<&'a Trait> {
let mut new_access = access.clone();
for &component in &*state.components {
assert!(
!access.access().has_write(component),
!access.access().has_component_write(component),
"&{} conflicts with a previous access in this query. Shared access cannot coincide with exclusive access.",
std::any::type_name::<Trait>(),
);
if not_first {
let mut intermediate = access.clone();
intermediate.add_read(component);
intermediate.add_component_read(component);
new_access.append_or(&intermediate);
new_access.extend_access(&intermediate);
} else {
new_access.and_with(component);
new_access.access_mut().add_read(component);
new_access.access_mut().add_component_read(component);
not_first = true;
}
}
Expand All @@ -562,6 +569,11 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> WorldQuery for All<&'a Trait> {
) -> bool {
state.matches_component_set_any(set_contains_id)
}

#[inline]
fn shrink_fetch<'wlong: 'wshort, 'wshort>(fetch: Self::Fetch<'wlong>) -> Self::Fetch<'wshort> {
fetch
}
}

unsafe impl<'a, Trait: ?Sized + TraitQuery> QueryData for All<&'a mut Trait> {
Expand Down Expand Up @@ -647,18 +659,18 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> WorldQuery for All<&'a mut Trait> {
let mut new_access = access.clone();
for &component in &*state.components {
assert!(
!access.access().has_write(component),
!access.access().has_component_write(component),
"&mut {} conflicts with a previous access in this query. Mutable component access must be unique.",
std::any::type_name::<Trait>(),
);
if not_first {
let mut intermediate = access.clone();
intermediate.add_write(component);
intermediate.add_component_write(component);
new_access.append_or(&intermediate);
new_access.extend_access(&intermediate);
} else {
new_access.and_with(component);
new_access.access_mut().add_write(component);
new_access.access_mut().add_component_write(component);
not_first = true;
}
}
Expand All @@ -683,4 +695,9 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> WorldQuery for All<&'a mut Trait> {
) -> bool {
state.matches_component_set_any(set_contains_id)
}

#[inline]
fn shrink_fetch<'wlong: 'wshort, 'wshort>(fetch: Self::Fetch<'wlong>) -> Self::Fetch<'wshort> {
fetch
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ impl RegisterExt for World {
where
(C,): TraitQueryMarker<Trait, Covered = C>,
{
let component_id = self.init_component::<C>();
let component_id = self.register_component::<C>();
let registry = self
.get_resource_or_insert_with::<TraitImplRegistry<Trait>>(Default::default)
.into_inner();
Expand Down
Loading

0 comments on commit deb3063

Please sign in to comment.