From ca1a207c9944b754cd93475f435291f5ba5ef246 Mon Sep 17 00:00:00 2001 From: "0m.ax" Date: Sat, 19 Jul 2025 01:37:51 +0200 Subject: [PATCH] trait --- src/main.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index b030c7d..f27aa71 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,7 +53,7 @@ struct BouncingImage { trait Drawable { // Associated function signature; `Self` refers to the implementor type. - + fn rate(&self) -> u32; fn draw_and_move(&mut self, display: &mut Display); fn draw_png(&mut self, display: &mut Display, x: i32, y: i32); @@ -88,7 +88,9 @@ impl BouncingImage { } impl Drawable for BouncingImage { - + fn rate(&self) -> u32 { + return self.rate; + } /// Draws a PNG image at the given coordinates. fn draw_png(&mut self, display: &mut Display, x: i32, y: i32) { @@ -195,12 +197,12 @@ impl Display { } fn main() { - let mut images = vec![ - BouncingImage::new("images/unicorn_cc.png", 13, -10, 1, -1, -1), - BouncingImage::new("images/windows_logo.png", -8, 3, 2, -1, -1), - BouncingImage::new("images/spade.png", 32, -12, 1, 0, 0), - BouncingImage::new("images/dvdvideo.png", 20, 6, 5, 1000, 800), - BouncingImage::new("images/hackaday.png", 40, 18, 3, 500, 800), + let mut images:Vec> = vec![ + Box::new(BouncingImage::new("images/unicorn_cc.png", 13, -10, 1, -1, -1)), + Box::new(BouncingImage::new("images/windows_logo.png", -8, 3, 2, -1, -1)), + Box::new(BouncingImage::new("images/spade.png", 32, -12, 1, 0, 0)), + Box::new(BouncingImage::new("images/dvdvideo.png", 20, 6, 5, 1000, 800)), + Box::new(BouncingImage::new("images/hackaday.png", 40, 18, 3, 500, 800)), ]; let mut display = Display::new(DISPLAY_HOST, DISPLAY_PORT); @@ -210,7 +212,7 @@ fn main() { loop { for (i, bb) in images.iter_mut().enumerate() { - if bb.rate > 0 && frame_counter % bb.rate != 0 { + if bb.rate() > 0 && frame_counter % bb.rate() != 0 { continue; } bb.draw_and_move(&mut display);