# memkit-bevy
**Bevy ECS integration for the memkit ecosystem.**
> **Version:** 0.1.0-alpha.1
[](https://crates.io/crates/memkit-bevy)
[](https://docs.rs/memkit-bevy)
[](https://opensource.org/licenses/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 Integration** — `MkAllocatorRes` as a Bevy `Resource`
- **System Sets** — `MkSystemSet::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
```rust
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
```rust
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:
```rust
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
```rust
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
| `bevy_prelude` | Re-exports `bevy::prelude::*` for convenience |
## Types
| `MkPlugin` | Bevy plugin for memkit integration |
| `MkAllocatorRes` | Bevy resource wrapping `MkAllocator` |
| `MkSystemSet` | System sets for frame lifecycle ordering |
## Compatibility
| 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](../LICENSE.md) file for details.