From f14841e27e1b8479a784de5e4b3c787b77a83924 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 16 Feb 2023 17:01:40 +0900 Subject: [PATCH] Use &trait/&mut trait instead of Box variants --- src/faux_quicksilver.rs | 48 ++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/src/faux_quicksilver.rs b/src/faux_quicksilver.rs index 55e47a6..38bb617 100644 --- a/src/faux_quicksilver.rs +++ b/src/faux_quicksilver.rs @@ -250,12 +250,12 @@ impl Window { } } - pub fn get_gi(&self) -> &Box { - &self.gi + pub fn get_gi(&self) -> &dyn GameInterface { + self.gi.as_ref() } - pub fn get_gi_mut(&mut self) -> &mut Box { - &mut self.gi + pub fn get_gi_mut(&mut self) -> &mut dyn GameInterface { + self.gi.as_mut() } pub fn load_image(&mut self, path: &Path, name: String) -> Result<(), String> { @@ -282,60 +282,68 @@ impl Window { Ok(()) } - pub fn get_image(&self, name: &str) -> Result<&Box, String> { + pub fn get_image(&self, name: &str) -> Result<&dyn ImageInterface, String> { Ok(self .images .get(name) - .ok_or_else(|| format!("Image \"{name}\" not found"))?) + .ok_or_else(|| format!("Image \"{name}\" not found"))? + .as_ref()) } - pub fn get_image_mut(&self, name: &str) -> Result<&mut Box, String> { + pub fn get_image_mut(&self, name: &str) -> Result<&mut dyn ImageInterface, String> { Ok(self .images .get_mut(name) - .ok_or_else(|| format!("Image \"{name}\" not found"))?) + .ok_or_else(|| format!("Image \"{name}\" not found"))? + .as_mut()) } - pub fn get_font(&self, name: &str) -> Result<&Box, String> { + pub fn get_font(&self, name: &str) -> Result<&dyn FontInterface, String> { Ok(self .fonts .get(name) - .ok_or_else(|| format!("Font \"{name}\" not found"))?) + .ok_or_else(|| format!("Font \"{name}\" not found"))? + .as_ref()) } - pub fn get_font_mut(&self, name: &str) -> Result<&mut Box, String> { + pub fn get_font_mut(&self, name: &str) -> Result<&mut dyn FontInterface, String> { Ok(self .fonts .get_mut(name) - .ok_or_else(|| format!("Font \"{name}\" not found"))?) + .ok_or_else(|| format!("Font \"{name}\" not found"))? + .as_mut()) } - pub fn get_sound(&self, name: &str) -> Result<&Box, String> { + pub fn get_sound(&self, name: &str) -> Result<&dyn SoundInterface, String> { Ok(self .sounds .get(name) - .ok_or_else(|| format!("Sound \"{name}\" not found"))?) + .ok_or_else(|| format!("Sound \"{name}\" not found"))? + .as_ref()) } - pub fn get_sound_mut(&self, name: &str) -> Result<&mut Box, String> { + pub fn get_sound_mut(&self, name: &str) -> Result<&mut dyn SoundInterface, String> { Ok(self .sounds .get_mut(name) - .ok_or_else(|| format!("Sound \"{name}\" not found"))?) + .ok_or_else(|| format!("Sound \"{name}\" not found"))? + .as_mut()) } - pub fn get_music(&self, name: &str) -> Result<&Box, String> { + pub fn get_music(&self, name: &str) -> Result<&dyn MusicInterface, String> { Ok(self .music .get(name) - .ok_or_else(|| format!("Music \"{name}\" not found"))?) + .ok_or_else(|| format!("Music \"{name}\" not found"))? + .as_ref()) } - pub fn get_music_mut(&self, name: &str) -> Result<&mut Box, String> { + pub fn get_music_mut(&self, name: &str) -> Result<&mut dyn MusicInterface, String> { Ok(self .music .get_mut(name) - .ok_or_else(|| format!("Music \"{name}\" not found"))?) + .ok_or_else(|| format!("Music \"{name}\" not found"))? + .as_mut()) } }