Dynasty is a powerful inheritance system for Rust, designed specifically for game engines and complex application architectures. It provides a natural class-based inheritance model while maintaining Rust's safety guarantees and performance characteristics.
- Safe Inheritance: Implement class hierarchies with compile-time safety guarantees
- Runtime Type Information: Robust RTTI system for type checking and reflection
- Smart Downcasting: Safe and ergonomic downcasting between types in the hierarchy
- Reflection API: Optional reflection system for runtime introspection
- Serialization Support: Optional integration with serde for serialization
- Zero-Cost Abstractions: Most features compile away to zero overhead
- Game Engine Ready: Designed for high-performance game development scenarios
Add Dynasty to your Cargo.toml
:
[dependencies]
dynasty = "0.1.0"
Create your class hierarchy:
use dynasty::prelude::*;
// Define a base class
#[derive(Class, Debug)]
pub struct Entity {
id: u64,
name: String,
}
// Create a derived class
#[inherit(Entity)]
#[derive(Debug)]
pub struct Character {
base: Entity,
health: f32,
level: u32,
}
// Multiple levels of inheritance
#[inherit(Character)]
#[derive(Debug)]
pub struct Player {
base: Character,
experience: u32,
}
fn main() {
// Create instances
let player = Player {
base: Character {
base: Entity {
id: 1,
name: "Hero".to_string(),
},
health: 100.0,
level: 1,
},
experience: 0,
};
// Use safe downcasting
let as_character: &Character = player.as_parent();
println!("Character level: {}", as_character.level);
}
Enable the reflection feature to access runtime type information:
[dependencies]
dynasty = { version = "0.1.0", features = ["reflection"] }
use dynasty::prelude::*;
#[derive(Class)]
struct Transform {
position: (f32, f32, f32),
}
// Implement reflection
impl Reflect for Transform {
fn get_field(&self, name: &str) -> Option<&dyn Any> {
match name {
"position" => Some(&self.position),
_ => None,
}
}
// TODO: implement other reflection methods
}
Enable serialization support:
[dependencies]
dynasty = { version = "0.1.0", features = ["serialization"] }
use dynasty::prelude::*;
use serde::{Serialize, Deserialize};
#[derive(Class, Serialize, Deserialize)]
struct GameObject {
id: u64,
components: Vec<Box<dyn Component>>,
}
Dynasty is organized as a workspace with two main crates:
dynasty
: The core runtime library providing the inheritance systemdynasty-macros
: Procedural macros for the derive and attribute implementations
Dynasty is designed with performance in mind:
- Zero-cost abstractions for most features
- Compile-time resolution of inheritance relationships
- Minimal runtime overhead for type checking
- Efficient virtual dispatch using Rust's trait system
Check out these examples to see Dynasty in action:
use dynasty::prelude::*;
#[derive(Class)]
struct Component {
enabled: bool,
}
#[inherit(Component)]
struct RigidBody {
base: Component,
mass: f32,
velocity: (f32, f32, f32),
}
#[inherit(Component)]
struct MeshRenderer {
base: Component,
mesh: String,
material: String,
}
use dynasty::prelude::*;
#[derive(Class)]
struct Event {
timestamp: f64,
}
#[inherit(Event)]
struct CollisionEvent {
base: Event,
entity_a: u64,
entity_b: u64,
point: (f32, f32, f32),
}
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
To get started:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
- Built with and for the Rust community
- Special thanks to all contributors
Built with ❤️ by Tristan J. Poland