Skip to content

Instantly share code, notes, and snippets.

@pimeys
Created January 27, 2019 20:39
Show Gist options
  • Save pimeys/d9922265cf8a7cb3d2cc7d784a21be89 to your computer and use it in GitHub Desktop.
Save pimeys/d9922265cf8a7cb3d2cc7d784a21be89 to your computer and use it in GitHub Desktop.
use std::boxed::Box;
trait Drawable {
fn draw(&self);
}
struct Button {
pub size: i32,
}
struct Scrollbar {
pub location: i32,
}
impl Drawable for Button {
fn draw(&self) {
dbg!("Drawing a button");
}
}
impl Drawable for Scrollbar {
fn draw(&self) {
dbg!("Drawing a scrollbar");
}
}
struct DynScreen {
drawables: Vec<Box<dyn Drawable>>,
}
impl DynScreen {
pub fn new() -> Self {
DynScreen {
drawables: Vec::new(),
}
}
pub fn add(&mut self, c: Box<dyn Drawable>) {
self.drawables.push(c);
}
pub fn draw(&self) {
for c in self.drawables.iter() {
c.draw();
}
}
}
struct StaticScreen<T: Drawable> {
drawables: Vec<T>,
}
impl<T: Drawable> StaticScreen<T> {
pub fn new() -> Self {
StaticScreen {
drawables: Vec::new(),
}
}
pub fn add(&mut self, c: T) {
self.drawables.push(c);
}
pub fn draw(&self) {
for c in self.drawables.iter() {
c.draw();
}
}
}
fn main() {
// Why this works?
let mut screen = DynScreen::new();
let button = Button { size: 32 };
let sb = Scrollbar { location: 64 };
screen.add(Box::new(button));
screen.add(Box::new(sb));
screen.draw();
// But this doesn't?
let mut screen = StaticScreen::new();
let button = Button { size: 32 };
let sb = Scrollbar { location: 64 };
screen.add(button);
screen.add(sb);
screen.draw();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment