From cd54261adb93d455bb038818d3cb1cb81ef56b44 Mon Sep 17 00:00:00 2001 From: destinyF**ker Date: Thu, 11 Apr 2024 14:32:10 +0800 Subject: [PATCH] update --- exercises/conversions/as_ref_mut.rs | 19 ++++++++---- exercises/conversions/try_from_into.rs | 42 ++++++++++++++++++++++++-- exercises/tests/Cargo.lock | 7 +++++ exercises/tests/Cargo.toml | 7 +++++ exercises/tests/build.rs | 12 +++----- exercises/tests/tests5.rs | 9 ++++-- exercises/tests/tests7.rs | 2 -- exercises/tests/tests8.rs | 2 -- 8 files changed, 78 insertions(+), 22 deletions(-) create mode 100644 exercises/tests/Cargo.lock create mode 100644 exercises/tests/Cargo.toml diff --git a/exercises/conversions/as_ref_mut.rs b/exercises/conversions/as_ref_mut.rs index 626a36c..a0c1fdf 100644 --- a/exercises/conversions/as_ref_mut.rs +++ b/exercises/conversions/as_ref_mut.rs @@ -7,25 +7,32 @@ // 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 +where + T: AsRef, +{ 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 +where + T: AsRef, +{ 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) +where + T: AsMut, +{ // TODO: Implement the function body. - ??? + *arg.as_mut() *= *arg.as_mut() } #[cfg(test)] diff --git a/exercises/conversions/try_from_into.rs b/exercises/conversions/try_from_into.rs index 32d6ef3..d936920 100644 --- a/exercises/conversions/try_from_into.rs +++ b/exercises/conversions/try_from_into.rs @@ -27,8 +27,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 +39,21 @@ enum IntoColorError { impl TryFrom<(i16, i16, i16)> for Color { type Error = IntoColorError; fn try_from(tuple: (i16, i16, i16)) -> Result { + if tuple.0 < 0 || tuple.0 > 255 { + return Err(IntoColorError::IntConversion); + } + if tuple.1 < 0 || tuple.1 > 255 { + return Err(IntoColorError::IntConversion); + } + if tuple.2 < 0 || tuple.2 > 255 { + return Err(IntoColorError::IntConversion); + } + + Ok(Color { + red: tuple.0 as u8, + green: tuple.1 as u8, + blue: tuple.2 as u8, + }) } } @@ -48,6 +61,17 @@ impl TryFrom<(i16, i16, i16)> for Color { impl TryFrom<[i16; 3]> for Color { type Error = IntoColorError; fn try_from(arr: [i16; 3]) -> Result { + for item in arr { + if item < 0 || item > 255 { + return Err(IntoColorError::IntConversion); + } + } + + Ok(Color { + red: arr[0] as u8, + green: arr[1] as u8, + blue: arr[2] as u8, + }) } } @@ -55,6 +79,20 @@ 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); + } + for item in slice { + if *item < 0 || *item > 255 { + return Err(IntoColorError::IntConversion); + } + } + + Ok(Color { + red: slice[0] as u8, + green: slice[1] as u8, + blue: slice[2] as u8, + }) } } diff --git a/exercises/tests/Cargo.lock b/exercises/tests/Cargo.lock new file mode 100644 index 0000000..5727585 --- /dev/null +++ b/exercises/tests/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "tests8" +version = "0.0.1" diff --git a/exercises/tests/Cargo.toml b/exercises/tests/Cargo.toml new file mode 100644 index 0000000..646d1f0 --- /dev/null +++ b/exercises/tests/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "tests8" +version = "0.0.1" +edition = "2021" +[[bin]] +name = "tests8" +path = "tests8.rs" \ No newline at end of file diff --git a/exercises/tests/build.rs b/exercises/tests/build.rs index aa518ce..07a953d 100644 --- a/exercises/tests/build.rs +++ b/exercises/tests/build.rs @@ -10,15 +10,13 @@ fn main() { .duration_since(std::time::UNIX_EPOCH) .unwrap() .as_secs(); // What's the use of this timestamp here? - let your_command = format!( - "Your command here with {}, please checkout exercises/tests/build.rs", - timestamp - ); - println!("cargo:{}", your_command); + let your_command = format!("rustc-env=TEST_FOO={}", timestamp); + println!("cargo::{}", your_command); + // println!("cargo::rustc-env=TEST_FOO=destinyFvcker"); // In tests8, we should enable "pass" feature to make the // testcase return early. Fill in the command to tell // Cargo about that. - let your_command = "Your command here, please checkout exercises/tests/build.rs"; - println!("cargo:{}", your_command); + let your_command = "rustc-cfg=feature=\"pass\""; + println!("cargo::{}", your_command); } diff --git a/exercises/tests/tests5.rs b/exercises/tests/tests5.rs index ca26d5b..fddb60f 100644 --- a/exercises/tests/tests5.rs +++ b/exercises/tests/tests5.rs @@ -22,8 +22,6 @@ // Execute `rustlings hint tests5` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - /// # Safety /// /// The `address` must contain a mutable reference to a valid `u32` value. @@ -31,7 +29,12 @@ unsafe fn modify_by_address(address: usize) { // TODO: Fill your safety notice of the code block below to match your // code's behavior and the contract of this function. You may use the // comment of the test below as your format reference. - unsafe { todo!("Your code goes here") } + unsafe { + // let mut point = Box::from_raw(address as *mut u32); + // *point = 0xAABBCCDD; + let point: *mut u32 = address as *mut u32; + *point = 0xAABBCCDD; + } } #[cfg(test)] diff --git a/exercises/tests/tests7.rs b/exercises/tests/tests7.rs index 66b37b7..79883a9 100644 --- a/exercises/tests/tests7.rs +++ b/exercises/tests/tests7.rs @@ -34,8 +34,6 @@ // Execute `rustlings hint tests7` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - fn main() {} #[cfg(test)] diff --git a/exercises/tests/tests8.rs b/exercises/tests/tests8.rs index ce7e35d..dfa37d7 100644 --- a/exercises/tests/tests8.rs +++ b/exercises/tests/tests8.rs @@ -7,8 +7,6 @@ // Execute `rustlings hint tests8` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - fn main() {} #[cfg(test)]