Skip to content

Commit ee0458c

Browse files
committed
Return InvertibleComponentIdSet from Access for reads_and_writes
1 parent 29d7fbb commit ee0458c

2 files changed

Lines changed: 43 additions & 17 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: "Reads and writes from `Access` are exposed"
3+
pull_requests: []
4+
---
5+
6+
Removed from [`bevy_ecs::query::Access`] methods that gave `Result<ComponentIdSet, UnboundedAccessError>>`:
7+
8+
- `try_reads_and_writes()`
9+
- `try_writes()`
10+
11+
Added new functions that return a new enum `InvertibleComponentIdSet`:
12+
13+
- `reads_and_writes()`
14+
- `writes()`
15+
16+
The `try_` methods would fail for exclusion queries.

crates/bevy_ecs/src/query/access.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -419,30 +419,22 @@ impl Access {
419419
&self.archetypal
420420
}
421421

422-
/// Returns the set of components with read or write access,
423-
/// or an error if the access is unbounded.
424-
pub fn try_reads_and_writes(&self) -> Result<&ComponentIdSet, UnboundedAccessError> {
422+
/// Returns the set of components with read or write access
423+
pub fn reads_and_writes(&self) -> InvertibleComponentIdSet<'_> {
425424
// writes_inverted is only ever true when read_and_writes_inverted is
426425
// also true. Therefore it is sufficient to check just read_and_writes_inverted.
427426
if self.read_and_writes_inverted {
428-
return Err(UnboundedAccessError {
429-
writes_inverted: self.writes_inverted,
430-
read_and_writes_inverted: self.read_and_writes_inverted,
431-
});
427+
return InvertibleComponentIdSet::Excluded(&self.read_and_writes);
432428
}
433-
Ok(&self.read_and_writes)
429+
InvertibleComponentIdSet::Included(&self.read_and_writes)
434430
}
435431

436-
/// Returns the set of components with write access,
437-
/// or an error if the access is unbounded.
438-
pub fn try_writes(&self) -> Result<&ComponentIdSet, UnboundedAccessError> {
432+
/// Returns the set of components with write access
433+
pub fn writes(&self) -> InvertibleComponentIdSet<'_> {
439434
if self.writes_inverted {
440-
return Err(UnboundedAccessError {
441-
writes_inverted: self.writes_inverted,
442-
read_and_writes_inverted: self.read_and_writes_inverted,
443-
});
435+
return InvertibleComponentIdSet::Excluded(&self.writes);
444436
}
445-
Ok(&self.writes)
437+
InvertibleComponentIdSet::Included(&self.writes)
446438
}
447439

448440
/// Returns an iterator over the component IDs and their [`ComponentAccessKind`].
@@ -478,7 +470,16 @@ impl Access {
478470
pub fn try_iter_access(
479471
&self,
480472
) -> Result<impl Iterator<Item = ComponentAccessKind> + '_, UnboundedAccessError> {
481-
let reads_and_writes = self.try_reads_and_writes()?.iter().map(|index| {
473+
let a = self.reads_and_writes();
474+
475+
let InvertibleComponentIdSet::Included(b) = a else {
476+
return Err(UnboundedAccessError {
477+
writes_inverted: self.writes_inverted,
478+
read_and_writes_inverted: self.read_and_writes_inverted,
479+
});
480+
};
481+
482+
let reads_and_writes = b.iter().map(|index| {
482483
if self.writes.contains(index) {
483484
ComponentAccessKind::Exclusive(index)
484485
} else {
@@ -495,6 +496,15 @@ impl Access {
495496
}
496497
}
497498

499+
/// Either all or nothing in a [`ComponentIdSet`]
500+
#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
501+
pub enum InvertibleComponentIdSet<'a> {
502+
/// All components in the set
503+
Included(&'a ComponentIdSet),
504+
/// All components *not* in the set
505+
Excluded(&'a ComponentIdSet),
506+
}
507+
498508
/// Performs an in-place union of `other` into `self`, where either set may be inverted.
499509
///
500510
/// Each set corresponds to a `FixedBitSet` if `inverted` is `false`,

0 commit comments

Comments
 (0)