Skip to main content

bevy_ecs/query/
error.rs

1use bevy_utils::prelude::DebugName;
2
3use crate::{
4    archetype::ArchetypeId,
5    entity::{Entity, EntityNotSpawnedError},
6};
7
8/// An error that occurs when retrieving a specific [`Entity`]'s query result from [`Query`](crate::system::Query) or [`QueryState`](crate::query::QueryState).
9// TODO: return the type_name as part of this error
10#[derive(#[allow(unused_qualifications)]
#[automatically_derived]
impl ::thiserror::__private21::Error for QueryEntityError {
    fn source(&self)
        ->
            ::core::option::Option<&(dyn ::thiserror::__private21::Error +
            'static)> {
        use ::thiserror::__private21::AsDynError as _;

        #[allow(deprecated)]
        match self {
            QueryEntityError::QueryDoesNotMatch { .. } =>
                ::core::option::Option::None,
            QueryEntityError::NotSpawned { 0: source, .. } =>
                ::core::option::Option::Some(source.as_dyn_error()),
            QueryEntityError::AliasedMutability { .. } =>
                ::core::option::Option::None,
        }
    }
}
#[allow(unused_qualifications)]
#[automatically_derived]
impl ::core::fmt::Display for QueryEntityError {
    fn fmt(&self, __formatter: &mut ::core::fmt::Formatter)
        -> ::core::fmt::Result {
        use ::thiserror::__private21::AsDisplay as _;

        #[allow(unused_variables, deprecated, clippy ::
        used_underscore_binding)]
        match self {
            QueryEntityError::QueryDoesNotMatch(_0, _1) =>
                match (_0.as_display(),) {
                    (__display0,) =>
                        __formatter.write_fmt(format_args!("The query does not match entity {0}",
                                __display0)),
                },
            QueryEntityError::NotSpawned(_0) =>
                match (_0.as_display(),) {
                    (__display0,) =>
                        __formatter.write_fmt(format_args!("{0}", __display0)),
                },
            QueryEntityError::AliasedMutability(_0) =>
                match (_0.as_display(),) {
                    (__display0,) =>
                        __formatter.write_fmt(format_args!("The entity with ID {0} was requested mutably more than once",
                                __display0)),
                },
        }
    }
}
QueryEntityError
#[allow(clippy :: redundant_field_names)]
fn from(source: EntityNotSpawnedError) -> Self {
    QueryEntityError::NotSpawned { 0: source }
}thiserror::Error, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for QueryEntityError {
}
#[automatically_derived]impl ::core::clone::Clone for QueryEntityError {
    #[inline]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<Entity>;
        let _: ::core::clone::AssertParamIsClone<ArchetypeId>;
        let _: ::core::clone::AssertParamIsClone<EntityNotSpawnedError>;
        *self
    }
}Clone, #[automatically_derived]impl ::core::marker::Copy for QueryEntityError { }Copy, #[automatically_derived]impl ::core::fmt::Debug for QueryEntityError {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Self::QueryDoesNotMatch(__self_0, __self_1) =>
                ::core::fmt::Formatter::debug_tuple_field2_finish(f,
                    "QueryDoesNotMatch", __self_0, &__self_1),
            Self::NotSpawned(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "NotSpawned", &__self_0),
            Self::AliasedMutability(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "AliasedMutability", &__self_0),
        }
    }
}Debug, #[automatically_derived]impl ::core::marker::StructuralPartialEq for
    QueryEntityError {
}
#[automatically_derived]impl ::core::cmp::PartialEq for QueryEntityError {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        ::core::intrinsics::discriminant_value(self) ==
                ::core::intrinsics::discriminant_value(other) &&
            match (self, other) {
                (Self::QueryDoesNotMatch(__self_0, __self_1),
                    Self::QueryDoesNotMatch(__arg1_0, __arg1_1)) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1,
                (Self::NotSpawned(__self_0), Self::NotSpawned(__arg1_0)) =>
                    __self_0 == __arg1_0,
                (Self::AliasedMutability(__self_0),
                    Self::AliasedMutability(__arg1_0)) => __self_0 == __arg1_0,
                _ => unsafe { ::core::intrinsics::unreachable() }
            }
    }
}PartialEq, #[automatically_derived]impl ::core::cmp::Eq for QueryEntityError {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Entity>;
        let _: ::core::cmp::AssertParamIsEq<ArchetypeId>;
        let _: ::core::cmp::AssertParamIsEq<EntityNotSpawnedError>;
    }
}Eq)]
11pub enum QueryEntityError {
12    /// The given [`Entity`]'s components do not match the query.
13    ///
14    /// Either it does not have a requested component, or it has a component which the query filters out.
15    #[error("The query does not match entity {0}")]
16    QueryDoesNotMatch(Entity, ArchetypeId),
17    /// The given [`Entity`] is not spawned.
18    #[error("{0}")]
19    NotSpawned(#[from] EntityNotSpawnedError),
20    /// The [`Entity`] was requested mutably more than once.
21    ///
22    /// See [`Query::get_many_mut`](crate::system::Query::get_many_mut) for an example.
23    #[error("The entity with ID {0} was requested mutably more than once")]
24    AliasedMutability(Entity),
25}
26
27/// An error that occurs when evaluating a [`Query`](crate::system::Query) or [`QueryState`](crate::query::QueryState) as a single expected result via
28/// [`single`](crate::system::Query::single) or [`single_mut`](crate::system::Query::single_mut).
29#[derive(#[automatically_derived]impl ::core::fmt::Debug for QuerySingleError {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Self::NoEntities(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "NoEntities", &__self_0),
            Self::MultipleEntities(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "MultipleEntities", &__self_0),
        }
    }
}Debug, #[allow(unused_qualifications)]
#[automatically_derived]
impl ::thiserror::__private21::Error for QuerySingleError { }
#[allow(unused_qualifications)]
#[automatically_derived]
impl ::core::fmt::Display for QuerySingleError {
    fn fmt(&self, __formatter: &mut ::core::fmt::Formatter)
        -> ::core::fmt::Result {
        use ::thiserror::__private21::AsDisplay as _;

        #[allow(unused_variables, deprecated, clippy ::
        used_underscore_binding)]
        match self {
            QuerySingleError::NoEntities(_0) =>
                match (_0.as_display(),) {
                    (__display0,) =>
                        __formatter.write_fmt(format_args!("No entities fit the query {0}",
                                __display0)),
                },
            QuerySingleError::MultipleEntities(_0) =>
                match (_0.as_display(),) {
                    (__display0,) =>
                        __formatter.write_fmt(format_args!("Multiple entities fit the query {0}",
                                __display0)),
                },
        }
    }
}thiserror::Error)]
30pub enum QuerySingleError {
31    /// No entity fits the query.
32    #[error("No entities fit the query {0}")]
33    NoEntities(DebugName),
34    /// Multiple entities fit the query.
35    #[error("Multiple entities fit the query {0}")]
36    MultipleEntities(DebugName),
37}
38
39/// An error that occurs when creating a contiguous iterator from a non-dense [`Query`](crate::system::Query) or [`QueryState`](crate::query::QueryState) via
40/// [`contiguous_iter`](crate::system::Query::contiguous_iter) or [`contiguous_iter_mut`](crate::system::Query::contiguous_iter_mut).
41#[derive(#[automatically_derived]impl ::core::fmt::Debug for QueryNotDenseError {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f,
            "QueryNotDenseError", &&self.0)
    }
}Debug, #[allow(unused_qualifications)]
#[automatically_derived]
impl ::thiserror::__private21::Error for QueryNotDenseError { }
#[allow(unused_qualifications)]
#[automatically_derived]
impl ::core::fmt::Display for QueryNotDenseError {
    #[allow(clippy :: used_underscore_binding)]
    fn fmt(&self, __formatter: &mut ::core::fmt::Formatter)
        -> ::core::fmt::Result {
        use ::thiserror::__private21::AsDisplay as _;
        #[allow(unused_variables, deprecated)]
        let Self(_0) = self;
        match (_0.as_display(),) {
            (__display0,) =>
                __formatter.write_fmt(format_args!("Cannot contiguously iterate non-dense query {0}",
                        __display0)),
        }
    }
}thiserror::Error)]
42#[error("Cannot contiguously iterate non-dense query {0}")]
43pub struct QueryNotDenseError(pub DebugName);
44
45#[cfg(test)]
46mod test {
47    use crate::{prelude::World, query::QueryEntityError};
48    use bevy_ecs_macros::Component;
49
50    #[test]
51    fn query_does_not_match() {
52        let mut world = World::new();
53
54        #[derive(Component)]
55        struct Present1;
56        #[derive(Component)]
57        struct Present2;
58        #[derive(Component, Debug, PartialEq)]
59        struct NotPresent;
60
61        let entity = world.spawn((Present1, Present2));
62
63        let (entity, archetype_id) = (entity.id(), entity.archetype().id());
64
65        let result = world.query::<&NotPresent>().get(&world, entity);
66
67        assert_eq!(
68            result,
69            Err(QueryEntityError::QueryDoesNotMatch(entity, archetype_id))
70        );
71    }
72}