From 57cbf55b08d3d85878f92af8d65b45c42e39abd5 Mon Sep 17 00:00:00 2001 From: horaoen Date: Fri, 2 Feb 2024 11:53:06 +0800 Subject: [PATCH] finish conversions --- exercises/23_conversions/as_ref_mut.rs | 10 +++--- exercises/23_conversions/try_from_into.rs | 42 +++++++++++++++++++++-- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/exercises/23_conversions/as_ref_mut.rs b/exercises/23_conversions/as_ref_mut.rs index 2ba9e3f0..9ba6df16 100644 --- a/exercises/23_conversions/as_ref_mut.rs +++ b/exercises/23_conversions/as_ref_mut.rs @@ -7,25 +7,23 @@ // Execute `rustlings hint as_ref_mut` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - // Obtain the number of bytes (not characters) in the given argument. // TODO: Add the AsRef trait appropriately as a trait bound. -fn byte_counter(arg: T) -> usize { +fn byte_counter>(arg: T) -> usize { arg.as_ref().as_bytes().len() } // Obtain the number of characters (not bytes) in the given argument. // TODO: Add the AsRef trait appropriately as a trait bound. -fn char_counter(arg: T) -> usize { +fn char_counter>(arg: T) -> usize { arg.as_ref().chars().count() } // Squares a number using as_mut(). // TODO: Add the appropriate trait bound. -fn num_sq(arg: &mut T) { +fn num_sq>(arg: &mut T) { // TODO: Implement the function body. - ??? + *arg.as_mut() = *arg.as_mut() * *arg.as_mut() } #[cfg(test)] diff --git a/exercises/23_conversions/try_from_into.rs b/exercises/23_conversions/try_from_into.rs index 32d6ef39..ad587e99 100644 --- a/exercises/23_conversions/try_from_into.rs +++ b/exercises/23_conversions/try_from_into.rs @@ -9,6 +9,7 @@ // Execute `rustlings hint try_from_into` or use the `hint` watch subcommand for // a hint. +use core::slice; use std::convert::{TryFrom, TryInto}; #[derive(Debug, PartialEq)] @@ -27,8 +28,6 @@ enum IntoColorError { IntConversion, } -// I AM NOT DONE - // Your task is to complete this implementation and return an Ok result of inner // type Color. You need to create an implementation for a tuple of three // integers, an array of three integers, and a slice of integers. @@ -41,6 +40,16 @@ enum IntoColorError { impl TryFrom<(i16, i16, i16)> for Color { type Error = IntoColorError; fn try_from(tuple: (i16, i16, i16)) -> Result { + let (r, g, b) = tuple; + if r < 0 || g < 0 || b < 0 || r > 255 || g > 255 || b > 255 { + Err(IntoColorError::IntConversion) + } else { + Ok(Self { + red: r as u8, + green: g as u8, + blue: b as u8, + }) + } } } @@ -48,6 +57,19 @@ impl TryFrom<(i16, i16, i16)> for Color { impl TryFrom<[i16; 3]> for Color { type Error = IntoColorError; fn try_from(arr: [i16; 3]) -> Result { + let r = arr[0]; + let g = arr[1]; + let b = arr[2]; + + if r < 0 || g < 0 || b < 0 || r > 255 || g > 255 || b > 255 { + Err(IntoColorError::IntConversion) + } else { + Ok(Self { + red: r as u8, + green: g as u8, + blue: b as u8, + }) + } } } @@ -55,6 +77,22 @@ impl TryFrom<[i16; 3]> for Color { impl TryFrom<&[i16]> for Color { type Error = IntoColorError; fn try_from(slice: &[i16]) -> Result { + if slice.len() != 3 { + return Err(IntoColorError::BadLen); + } + let r = slice[0]; + let g = slice[1]; + let b = slice[2]; + + if r < 0 || g < 0 || b < 0 || r > 255 || g > 255 || b > 255 { + Err(IntoColorError::IntConversion) + } else { + Ok(Self { + red: r as u8, + green: g as u8, + blue: b as u8, + }) + } } }