Skip to content

Commit

Permalink
Add basic trait object example
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Nov 25, 2024
1 parent 31bafb6 commit 0467377
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions examples/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use actuate::prelude::*;

#[data]
trait MyTrait: Data {
fn run(&self);
}

#[derive(Data)]
struct A<'a> {
my_trait: Box<dyn MyTrait + 'a>,
}

impl Compose for A<'_> {
fn compose(cx: Scope<Self>) -> impl Compose {
cx.me().my_trait.run();
}
}

#[derive(Data)]
struct X;

impl MyTrait for X {
fn run(&self) {
dbg!("X");
}
}

#[derive(Data)]
struct App;

impl Compose for App {
fn compose(_cx: Scope<Self>) -> impl Compose {
A {
my_trait: Box::new(X),
}
}
}

fn main() {}

0 comments on commit 0467377

Please sign in to comment.