-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4917282
commit 02f9f37
Showing
4 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::borrow::Cow; | ||
|
||
use super::{Fetch, FetchItem}; | ||
|
||
/// Expect the query to match, panic otherwise | ||
pub struct Expect<Q> { | ||
msg: Cow<'static, str>, | ||
fetch: Q, | ||
} | ||
|
||
impl<Q> Expect<Q> { | ||
/// Expect the query to match, panic otherwise | ||
pub fn new(fetch: Q, msg: impl Into<Cow<'static, str>>) -> Self { | ||
Self { | ||
fetch, | ||
msg: msg.into(), | ||
} | ||
} | ||
} | ||
|
||
impl<'q, Q: FetchItem<'q>> FetchItem<'q> for Expect<Q> { | ||
type Item = Q::Item; | ||
} | ||
|
||
impl<'w, Q: Fetch<'w>> Fetch<'w> for Expect<Q> { | ||
const MUTABLE: bool = Q::MUTABLE; | ||
|
||
type Prepared = Q::Prepared; | ||
|
||
fn prepare(&'w self, data: super::FetchPrepareData<'w>) -> Option<Self::Prepared> { | ||
Some(self.fetch.prepare(data).expect(&self.msg)) | ||
} | ||
|
||
fn filter_arch(&self, _: super::FetchAccessData) -> bool { | ||
true | ||
} | ||
|
||
fn access(&self, data: super::FetchAccessData, dst: &mut Vec<crate::system::Access>) { | ||
self.fetch.access(data, dst) | ||
} | ||
|
||
fn describe(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
f.write_str("expect ")?; | ||
self.fetch.describe(f) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use flax::{components::name, Entity, FetchExt, Query, World}; | ||
|
||
#[test] | ||
#[should_panic(expected = "name must be present")] | ||
fn expect_present() { | ||
let mut world = World::new(); | ||
|
||
Entity::builder().spawn(&mut world); | ||
let mut query = Query::new(name().cloned().expect("name must be present")); | ||
|
||
let _ = query.collect_vec(&world); | ||
} |