Use &trait/&mut trait instead of Box variants

This commit is contained in:
Stephen Seo 2023-02-16 17:01:40 +09:00
parent 71fc3de042
commit f14841e27e

View file

@ -250,12 +250,12 @@ impl Window {
} }
} }
pub fn get_gi(&self) -> &Box<dyn GameInterface> { pub fn get_gi(&self) -> &dyn GameInterface {
&self.gi self.gi.as_ref()
} }
pub fn get_gi_mut(&mut self) -> &mut Box<dyn GameInterface> { pub fn get_gi_mut(&mut self) -> &mut dyn GameInterface {
&mut self.gi self.gi.as_mut()
} }
pub fn load_image(&mut self, path: &Path, name: String) -> Result<(), String> { pub fn load_image(&mut self, path: &Path, name: String) -> Result<(), String> {
@ -282,60 +282,68 @@ impl Window {
Ok(()) Ok(())
} }
pub fn get_image(&self, name: &str) -> Result<&Box<dyn ImageInterface>, String> { pub fn get_image(&self, name: &str) -> Result<&dyn ImageInterface, String> {
Ok(self Ok(self
.images .images
.get(name) .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<dyn ImageInterface>, String> { pub fn get_image_mut(&self, name: &str) -> Result<&mut dyn ImageInterface, String> {
Ok(self Ok(self
.images .images
.get_mut(name) .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<dyn FontInterface>, String> { pub fn get_font(&self, name: &str) -> Result<&dyn FontInterface, String> {
Ok(self Ok(self
.fonts .fonts
.get(name) .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<dyn FontInterface>, String> { pub fn get_font_mut(&self, name: &str) -> Result<&mut dyn FontInterface, String> {
Ok(self Ok(self
.fonts .fonts
.get_mut(name) .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<dyn SoundInterface>, String> { pub fn get_sound(&self, name: &str) -> Result<&dyn SoundInterface, String> {
Ok(self Ok(self
.sounds .sounds
.get(name) .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<dyn SoundInterface>, String> { pub fn get_sound_mut(&self, name: &str) -> Result<&mut dyn SoundInterface, String> {
Ok(self Ok(self
.sounds .sounds
.get_mut(name) .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<dyn MusicInterface>, String> { pub fn get_music(&self, name: &str) -> Result<&dyn MusicInterface, String> {
Ok(self Ok(self
.music .music
.get(name) .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<dyn MusicInterface>, String> { pub fn get_music_mut(&self, name: &str) -> Result<&mut dyn MusicInterface, String> {
Ok(self Ok(self
.music .music
.get_mut(name) .get_mut(name)
.ok_or_else(|| format!("Music \"{name}\" not found"))?) .ok_or_else(|| format!("Music \"{name}\" not found"))?
.as_mut())
} }
} }