Skip to content

Instantly share code, notes, and snippets.

@wdevore
Created February 10, 2019 19:35
Show Gist options
  • Save wdevore/427a1c738229adad5c2aeee0896c080c to your computer and use it in GitHub Desktop.
Save wdevore/427a1c738229adad5c2aeee0896c080c to your computer and use it in GitHub Desktop.
An example of lifetimes and returning a SceneManager with correct lifetime attribute
// See this users response
// https://users.rust-lang.org/t/typical-cant-borrow-issue/24817
trait NodeTrait {
fn get_x(&self) -> f32;
fn add(&mut self, v: f32);
fn step(&mut self);
fn process(&mut self);
fn print(&self) {
println!("node: {}", self.get_x());
}
}
// --------------------------------------------
// Boot
// --------------------------------------------
struct BootScene {
x: f32,
}
impl NodeTrait for BootScene {
fn get_x(&self) -> f32 {
self.x
}
fn add(&mut self, v: f32) {
self.x += v;
}
fn step(&mut self) {
println!("step: {}", self.x);
self.process();
}
fn process(&mut self) {
println!("process: {}", self.x);
self.x += 1.0;
}
}
impl BootScene {
fn new(v: f32) -> Self {
Self { x: v }
}
}
// --------------------------------------------
// Splash
// --------------------------------------------
struct SplashScene {
x: f32,
}
impl NodeTrait for SplashScene {
fn get_x(&self) -> f32 {
self.x
}
fn add(&mut self, v: f32) {
self.x += v;
}
fn step(&mut self) {
println!("step: {}", self.x);
self.process();
}
fn process(&mut self) {
println!("process: {}", self.x);
self.x += 1.0;
}
}
impl SplashScene {
fn new(v: f32) -> Self {
Self { x: v }
}
}
struct SceneManager<'a> {
nodes: Vec<&'a mut NodeTrait>,
}
impl<'a> SceneManager<'a> {
fn new() -> Self {
Self { nodes: Vec::new() }
}
fn add(&mut self, t: &'a mut NodeTrait) {
self.nodes.push(t);
}
fn print(&self) {
for t in &self.nodes {
t.print();
}
}
fn change(&mut self, by: f32) {
for t in &mut self.nodes {
t.add(by);
t.step();
}
}
}
struct Scheduler<'a> {
nodes: Vec<&'a mut NodeTrait>,
}
impl<'a> Scheduler<'a> {
fn new() -> Self {
Self { nodes: Vec::new() }
}
fn add_target(&mut self, target: &'a mut NodeTrait) {
self.nodes.push(target);
}
}
type BuildCallback = fn(&mut World) -> bool;
struct World<'a> {
sm: SceneManager<'a>,
sch: Scheduler<'a>,
}
impl<'a> World<'a> {
fn new() -> Self {
Self {
sm: SceneManager::new(),
sch: Scheduler::new(),
}
}
fn get_sm_mut(&mut self) -> &mut SceneManager<'a> {
&mut self.sm
}
fn add_scene(&mut self, scene: &'a mut NodeTrait) {
self.sm.add(scene);
}
fn add_target(&mut self, target: &'a mut NodeTrait) {
self.sch.add_target(target);
}
fn initialize(&mut self, build: BuildCallback) {
build(self);
}
fn print(&self) {
self.sm.print();
}
}
fn main() {
let mut w = World::new();
w.initialize(builder);
let mut boot = BootScene::new(1.0);
let mut splash = SplashScene::new(2.0);
// sm.add(&mut boot);
// sm.add(&mut splash);
w.add_scene(&mut boot);
w.add_scene(&mut splash);
// w.add_target(&mut splash);
w.print();
let sm = w.get_sm_mut();
sm.change(3.0);
println!("----------------");
w.print();
}
fn builder(_world: &mut World) -> bool {
println!("builder called");
true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment