Struct SystemContext

Source
pub struct SystemContext<'scope> { /* private fields */ }
Expand description

Thin wrapper over hecs::World, can prepare queries using a QueryMarker.

Instantiating one directly is only useful when calling systems as plain functions, and can be done either by SystemContext::new() or converting a &hecs::World or &mut hecs::World:

fn some_system(_context: SystemContext, _resources: (), _queries: ()) {}

let mut world = hecs::World::new();

some_system(SystemContext::new(&world), (), ());
some_system((&world).into(), (), ());
some_system((&mut world).into(), (), ());

Implementations§

Source§

impl<'scope> SystemContext<'scope>

Source

pub fn new(world: &'scope World) -> Self

Wraps a &hecs::World. See documentation for SystemContext itself.

Source

pub fn id(&self) -> Option<SystemId>

Returns a debug-printable SystemId if the system is ran in an Executor, with printed number reflecting the order of insertion into the ExecutorBuilder.

Source

pub fn query<Q>(&self, _: QueryMarker<Q>) -> QueryBorrow<'_, Q>
where Q: Query + Send + Sync,

Prepares a query using the given QueryMarker; see hecs::World::query().

§Example
fn some_system(
    context: SystemContext,
    _resources: (),
    query: QueryMarker<(&mut Pos, &Vel)>
) {
    for (_entity, (pos, vel)) in context.query(query).iter() {
        *pos += *vel;
    }
};
Source

pub fn query_one<Q>( &self, _: QueryMarker<Q>, entity: Entity, ) -> Result<QueryOne<'_, Q>, NoSuchEntity>
where Q: Query + Send + Sync,

Prepares a query against a single entity using the given QueryMarker; see hecs::World::query_one().

§Example
fn some_system(
    context: SystemContext,
    _resources: (),
    query: QueryMarker<(&mut Pos, &Vel)>
) {
    let mut max_velocity = Vel::default();
    let mut max_velocity_entity = None;
    for (entity, (pos, vel)) in context.query(query).iter() {
        *pos += *vel;
        if *vel > max_velocity {
            max_velocity = *vel;
            max_velocity_entity = Some(entity);
        }
    }
    if let Some(entity) = max_velocity_entity {
        let mut query_one = context
            .query_one(query, entity)
            .expect("no such entity");
        let (pos, _vel) = query_one
            .get()
            .expect("some components are missing");
        *pos = Pos::default();
    }
};

Trait Implementations§

Source§

impl<'scope> From<&'scope World> for SystemContext<'scope>

Source§

fn from(world: &'scope World) -> Self

Converts to this type from the input type.
Source§

impl<'scope> From<&'scope mut World> for SystemContext<'scope>

Source§

fn from(world: &'scope mut World) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'scope> Freeze for SystemContext<'scope>

§

impl<'scope> !RefUnwindSafe for SystemContext<'scope>

§

impl<'scope> Send for SystemContext<'scope>

§

impl<'scope> Sync for SystemContext<'scope>

§

impl<'scope> Unpin for SystemContext<'scope>

§

impl<'scope> !UnwindSafe for SystemContext<'scope>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Component for T
where T: Send + Sync + 'static,