shipyard/
not.rs

1use crate::component::Component;
2use crate::tracking::Tracking;
3use crate::views::{View, ViewMut};
4use core::ops::Not as NotOps;
5
6/// Filters out components.
7///
8/// Get and iterators will skip entities that have this component.\
9/// Simply add `!` in front of the view reference at iterator creation.
10///
11/// ### Example
12/// ```
13/// use shipyard::{Component, IntoIter, View, World};
14///
15/// #[derive(Component, Debug, PartialEq, Eq)]
16/// struct U32(u32);
17///
18/// #[derive(Component, Debug, PartialEq, Eq)]
19/// struct USIZE(usize);
20///
21/// let mut world = World::new();
22///
23/// world.add_entity((USIZE(0), U32(1)));
24/// world.add_entity((USIZE(2),));
25///
26/// let (usizes, u32s) = world.borrow::<(View<USIZE>, View<U32>)>().unwrap();
27///
28/// let mut iter = (&usizes, !&u32s).iter();
29/// assert_eq!(iter.next(), Some((&USIZE(2), ())));
30/// assert_eq!(iter.next(), None);
31/// let mut iter = (&usizes, &u32s).iter();
32/// assert_eq!(iter.next(), Some((&USIZE(0), &U32(1))));
33/// assert_eq!(iter.next(), None);
34/// ```
35#[derive(Copy, Clone)]
36pub struct Not<T>(pub(crate) T);
37
38impl<T: Component, Track: Tracking> NotOps for &View<'_, T, Track> {
39    type Output = Not<Self>;
40    fn not(self) -> Self::Output {
41        Not(self)
42    }
43}
44
45impl<T: Component, Track: Tracking> NotOps for &ViewMut<'_, T, Track> {
46    type Output = Not<Self>;
47    fn not(self) -> Self::Output {
48        Not(self)
49    }
50}
51
52impl<T: Component, Track: Tracking> NotOps for &mut ViewMut<'_, T, Track> {
53    type Output = Not<Self>;
54    fn not(self) -> Self::Output {
55        Not(self)
56    }
57}