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

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so 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§