From: Grégory Burri Date: Fri, 20 Dec 2019 09:05:57 +0000 (+0100) Subject: Use a common trait (TryFrom) X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=6d97ba2022f7fa54fb7cf4c45681423e5b5ab657;p=advent_of_code_2019.git Use a common trait (TryFrom) --- diff --git a/src/day17.rs b/src/day17.rs index c64ed0e..f120506 100644 --- a/src/day17.rs +++ b/src/day17.rs @@ -1,17 +1,20 @@ use super::intcode; use std::collections::HashSet; +use std::convert::TryFrom; #[derive(PartialEq, Eq, Copy, Clone, Debug)] enum Direction { Up, Left, Down, Right } -impl Direction { - fn from_char(c: char) -> Option { +impl TryFrom for Direction { + type Error = (); + + fn try_from(c: char) -> Result { match c { - '^' => Some(Direction::Up), - '<' => Some(Direction::Left), - 'v' => Some(Direction::Down), - '>' => Some(Direction::Right), - _ => None + '^' => Ok(Direction::Up), + '<' => Ok(Direction::Left), + 'v' => Ok(Direction::Down), + '>' => Ok(Direction::Right), + _ => Err(()) } } } @@ -66,7 +69,7 @@ impl RobotTrackingSystem { current_x = 0; } else { let c = (*c as u8) as char; - if let Some(dir) = Direction::from_char(c) { + if let Ok(dir) = Direction::try_from(c) { self.start_position = (current_x, self.board.len() as i32); self.start_dir = dir }