From 163fddda5a8c3074cbe6e938c464808c6f7d4548 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sun, 12 Apr 2020 11:26:59 +0100 Subject: [PATCH] implement From for Image --- src/main.rs | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index 01d087d..1eeb0ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -424,23 +424,24 @@ fn example_game_default() -> Game { } } -fn image_from_string(string: String) -> Image { - let string = string.replace("\n", ""); - let pixels: Vec<&str> = string.split("").collect(); - // the above seems to add an extra "" at the start and end of the vec, so strip them below - let pixels = &pixels[1..(pixels.len() - 1)]; - let pixels: Vec = pixels.iter().map(|&pixel| { - pixel.parse::().unwrap() - }).collect(); +impl From for Image { + #[inline] + fn from(string: String) -> Image { + let string = string.replace("\n", ""); + let pixels: Vec<&str> = string.split("").collect(); + // the above seems to add an extra "" at the start and end of the vec, so strip them below + let pixels = &pixels[1..(pixels.len() - 1)]; + let pixels: Vec = pixels.iter().map(|&pixel| { + pixel.parse::().unwrap() + }).collect(); - Image { pixels } + Image { pixels } + } } #[test] fn test_image_from_string() { - let output = image_from_string( - include_str!("../test/resources/image").to_string() - ); + let output = Image::from(include_str!("../test/resources/image").to_string()); let expected = Image { pixels: vec![ @@ -532,7 +533,7 @@ fn tile_from_string(string: String) -> Tile { let animation_frames = lines[1..].join(""); let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect(); let animation_frames: Vec = animation_frames.iter().map(|&frame| { - image_from_string(frame.to_string()) + Image::from(frame.to_string()) }).collect(); Tile {id, name, wall, animation_frames} @@ -751,7 +752,7 @@ fn avatar_from_string(string: String) -> Avatar { let animation_frames: String = lines.join("\n"); let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect(); let animation_frames: Vec = animation_frames.iter().map(|&frame| { - image_from_string(frame.to_string()) + Image::from(frame.to_string()) }).collect(); Avatar { animation_frames, room, position } @@ -816,7 +817,7 @@ fn sprite_from_string(string: String) -> Sprite { let animation_frames = lines[1..].join(""); let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect(); let animation_frames: Vec = animation_frames.iter().map(|&frame| { - image_from_string(frame.to_string()) + Image::from(frame.to_string()) }).collect(); Sprite { id, name, animation_frames, dialogue, room, position } @@ -876,7 +877,7 @@ fn item_from_string(string: String) -> Item { let animation_frames = lines[1..].join(""); let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect(); let animation_frames: Vec = animation_frames.iter().map(|&frame| { - image_from_string(frame.to_string()) + Image::from(frame.to_string()) }).collect(); Item { id, name, animation_frames, dialogue }