bevy_ecs::world

Trait DynamicComponentFetch

source
pub unsafe trait DynamicComponentFetch {
    type Ref<'w>;
    type Mut<'w>;

    // Required methods
    unsafe fn fetch_ref(
        self,
        cell: UnsafeEntityCell<'_>,
    ) -> Result<Self::Ref<'_>, EntityComponentError>;
    unsafe fn fetch_mut(
        self,
        cell: UnsafeEntityCell<'_>,
    ) -> Result<Self::Mut<'_>, EntityComponentError>;
}
Expand description

Types that can be used to fetch components from an entity dynamically by ComponentIds.

Provided implementations are:

  • ComponentId: Returns a single untyped reference.
  • [ComponentId; N] and &[ComponentId; N]: Returns a same-sized array of untyped references.
  • &[ComponentId]: Returns a Vec of untyped references.
  • &HashSet<ComponentId>: Returns a HashMap of IDs to untyped references.

§Performance

  • The slice and array implementations perform an aliased mutability check in DynamicComponentFetch::fetch_mut that is O(N^2).
  • The HashSet implementation performs no such check as the type itself guarantees unique IDs.
  • The single ComponentId implementation performs no such check as only one reference is returned.

§Safety

Implementor must ensure that:

Required Associated Types§

source

type Ref<'w>

The read-only reference type returned by DynamicComponentFetch::fetch_ref.

source

type Mut<'w>

The mutable reference type returned by DynamicComponentFetch::fetch_mut.

Required Methods§

source

unsafe fn fetch_ref( self, cell: UnsafeEntityCell<'_>, ) -> Result<Self::Ref<'_>, EntityComponentError>

Returns untyped read-only reference(s) to the component(s) with the given ComponentIds, as determined by self.

§Safety

It is the caller’s responsibility to ensure that:

  • The given UnsafeEntityCell has read-only access to the fetched components.
  • No other mutable references to the fetched components exist at the same time.
§Errors
source

unsafe fn fetch_mut( self, cell: UnsafeEntityCell<'_>, ) -> Result<Self::Mut<'_>, EntityComponentError>

Returns untyped mutable reference(s) to the component(s) with the given ComponentIds, as determined by self.

§Safety

It is the caller’s responsibility to ensure that:

  • The given UnsafeEntityCell has mutable access to the fetched components.
  • No other references to the fetched components exist at the same time.
§Errors

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl DynamicComponentFetch for &HashSet<ComponentId>

source§

type Ref<'w> = HashMap<ComponentId, Ptr<'w>>

source§

type Mut<'w> = HashMap<ComponentId, MutUntyped<'w>>

source§

unsafe fn fetch_ref( self, cell: UnsafeEntityCell<'_>, ) -> Result<Self::Ref<'_>, EntityComponentError>

source§

unsafe fn fetch_mut( self, cell: UnsafeEntityCell<'_>, ) -> Result<Self::Mut<'_>, EntityComponentError>

source§

impl DynamicComponentFetch for &[ComponentId]

source§

type Ref<'w> = Vec<Ptr<'w>>

source§

type Mut<'w> = Vec<MutUntyped<'w>>

source§

unsafe fn fetch_ref( self, cell: UnsafeEntityCell<'_>, ) -> Result<Self::Ref<'_>, EntityComponentError>

source§

unsafe fn fetch_mut( self, cell: UnsafeEntityCell<'_>, ) -> Result<Self::Mut<'_>, EntityComponentError>

source§

impl<const N: usize> DynamicComponentFetch for &[ComponentId; N]

source§

type Ref<'w> = [Ptr<'w>; N]

source§

type Mut<'w> = [MutUntyped<'w>; N]

source§

unsafe fn fetch_ref( self, cell: UnsafeEntityCell<'_>, ) -> Result<Self::Ref<'_>, EntityComponentError>

source§

unsafe fn fetch_mut( self, cell: UnsafeEntityCell<'_>, ) -> Result<Self::Mut<'_>, EntityComponentError>

source§

impl<const N: usize> DynamicComponentFetch for [ComponentId; N]

source§

type Ref<'w> = [Ptr<'w>; N]

source§

type Mut<'w> = [MutUntyped<'w>; N]

source§

unsafe fn fetch_ref( self, cell: UnsafeEntityCell<'_>, ) -> Result<Self::Ref<'_>, EntityComponentError>

source§

unsafe fn fetch_mut( self, cell: UnsafeEntityCell<'_>, ) -> Result<Self::Mut<'_>, EntityComponentError>

Implementors§