Open-Source Wikis

/

Bevy

/

Packages

/

bevy_ptr

bevyengine/bevy

bevy_ptr

Type-erased pointer helpers. A single-file crate used by bevy_ecs to manipulate component data without knowing its concrete type at compile time.

Purpose

Bevy's archetypal storage stores components as untyped bytes in a contiguous column. To insert or fetch a component via reflection, the ECS needs a way to wrap an arbitrary T as a typed pointer with the right alignment and lifetime guarantees, without paying for a Box<dyn Any> heap allocation.

bevy_ptr provides the building blocks: Ptr, PtrMut, OwningPtr, ThinSlicePtr, plus alignment-checked casts.

Directory layout

crates/bevy_ptr/src/
└── lib.rs    # Single file. ~500 lines.

Key abstractions

Type File Description
Ptr<'a> crates/bevy_ptr/src/lib.rs Type-erased shared reference.
PtrMut<'a> crates/bevy_ptr/src/lib.rs Type-erased exclusive reference.
OwningPtr<'a> crates/bevy_ptr/src/lib.rs Type-erased owned value. Caller responsible for moving out exactly once.
ThinSlicePtr<'a, T> crates/bevy_ptr/src/lib.rs Slice without length (length tracked elsewhere — used for archetypal storage).
Aligned, Unaligned crates/bevy_ptr/src/lib.rs Type-level marker for alignment guarantees.

How it's used

bevy_ecs::storage::Table stores components in Vec<u8> columns and hands out Ptrs with the right alignment per component type. bevy_ecs::world::EntityMut::get_mut returns a Ptr that the caller downcasts via Ptr::deref::<T>().

The trade-off: tiny, no-overhead, with safety contracts maintained by bevy_ecs (which is the only sane consumer).

Integration points

  • Depends on: Just core.
  • Depended on by: bevy_ecs, bevy_reflect, bevy_render (some bind-group code).

Entry points for modification

  • The crate is one file, full of unsafe. Changes here ripple through bevy_ecs's storage. Edit only with the ECS team's review.

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

bevy_ptr – Bevy wiki | Factory