memkit-bevy

Crates.iomemkit-bevy
lib.rsmemkit-bevy
version0.1.0-alpha.1
created_at2025-12-27 00:40:37.601121+00
updated_at2025-12-27 00:40:37.601121+00
descriptionBevy ECS integration for memkit
homepage
repositoryhttps://github.com/YelenaTor/memkit
max_upload_size
id2006496
size58,225
YoruXIII (YelenaTor)

documentation

https://docs.rs/memkit-bevy

README

memkit-bevy

Bevy ECS integration for the memkit ecosystem.

Version: 0.1.0-alpha.1

Crates.io Documentation License: MPL-2.0

Overview

memkit-bevy provides seamless integration between memkit and the Bevy game engine. It automatically manages frame lifecycle and exposes the allocator as a Bevy resource.

Features

  • Automatic Frame Lifecycle — Frame begin/end tied to Bevy's First/Last schedules
  • Resource IntegrationMkAllocatorRes as a Bevy Resource
  • System SetsMkSystemSet::FrameBegin and MkSystemSet::FrameEnd for ordering
  • Zero Configuration — Just add the plugin and go
  • Optional Prelude — Enable bevy_prelude feature for full Bevy re-exports

Quick Start

use bevy::prelude::*;
use memkit_bevy::{MkPlugin, MkAllocatorRes};

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugins(MkPlugin::default())
        .add_systems(Update, my_system)
        .run();
}

fn my_system(alloc: Res<MkAllocatorRes>) {
    // Frame automatically managed by plugin
    // Allocations are valid until end of frame
    if let Some(data) = alloc.frame_box(MyData::new()) {
        // ... use data ...
    }
}

#[derive(Default)]
struct MyData {
    value: f32,
}

Custom Configuration

use memkit::MkConfig;
use memkit_bevy::MkPlugin;

// Option 1: Custom config
let config = MkConfig {
    frame_arena_size: 32 * 1024 * 1024, // 32 MB
    ..Default::default()
};
App::new()
    .add_plugins(MkPlugin::with_config(config))
    .run();

// Option 2: Just set arena size
App::new()
    .add_plugins(MkPlugin::with_arena_size(64 * 1024 * 1024)) // 64 MB
    .run();

System Ordering

memkit-bevy provides system sets for proper ordering:

use memkit_bevy::{MkPlugin, MkSystemSet};

App::new()
    .add_plugins(MkPlugin::default())
    // Run after frame begins
    .add_systems(First, my_init_system.after(MkSystemSet::FrameBegin))
    // Run before frame ends
    .add_systems(Last, my_cleanup_system.before(MkSystemSet::FrameEnd))
    .run();

Allocation Methods

fn my_system(alloc: Res<MkAllocatorRes>) {
    // Raw pointer allocation (you must initialize)
    let ptr: *mut MyStruct = alloc.frame_alloc::<MyStruct>();
    
    // Boxed allocation (initialized)
    let boxed: Option<MkFrameBox<MyStruct>> = alloc.frame_box(MyStruct::default());
    
    // Slice allocation
    let slice: Option<MkFrameSlice<f32>> = alloc.frame_slice::<f32>(1024);
    
    // Check if frame is active
    if alloc.is_frame_active() {
        // Safe to allocate
    }
}

Feature Flags

Feature Description
bevy_prelude Re-exports bevy::prelude::* for convenience

Types

Type Description
MkPlugin Bevy plugin for memkit integration
MkAllocatorRes Bevy resource wrapping MkAllocator
MkSystemSet System sets for frame lifecycle ordering

Compatibility

memkit-bevy Bevy
0.12.x 0.17.x

How It Works

  1. Frame Begin (First schedule): Calls MkAllocator::begin_frame()
  2. Your Systems (Update, etc.): Use Res<MkAllocatorRes> for frame allocations
  3. Frame End (Last schedule): Calls MkAllocator::end_frame(), resetting the arena

All frame allocations are automatically freed when the frame ends — no manual cleanup needed.

License

This project is licensed under the Mozilla Public License 2.0 - see the LICENSE.md file for details.

Commit count: 0

cargo fmt