Created
February 10, 2020 09:52
-
-
Save narodnik/d67f68fd1a66eba36776ef0a9274f96e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Builder { | |
// I want this trait to return a trait object | |
fn commits(&self) -> Box<dyn Commit>; | |
fn finish(&self); | |
} | |
trait Commit { | |
} | |
struct FooBuilder { | |
} | |
struct FooCommit { | |
} | |
impl Builder for FooBuilder { | |
fn commits(&self) -> Box<dyn Commit> { | |
Box::new(FooCommit{ }) | |
} | |
fn finish(&self) { | |
} | |
} | |
impl Commit for FooCommit { | |
} | |
fn get_commits(b: &dyn Builder) { | |
// trait object returns a trait | |
let c = b.commits(); | |
} | |
fn main() { | |
let b = FooBuilder{}; | |
get_commits(&b); | |
b.finish(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment