From 4d2ae8485cd65429e64606119acac3156158ea2b Mon Sep 17 00:00:00 2001 From: Arpit Bhadauria Date: Mon, 4 Dec 2023 22:44:18 +0000 Subject: [PATCH] impl from trait instead of into --- src/uu/expr/src/syntax_tree.rs | 43 ++++++++++++++++------------------ 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/src/uu/expr/src/syntax_tree.rs b/src/uu/expr/src/syntax_tree.rs index 7677b5e7e..ae2a44e52 100644 --- a/src/uu/expr/src/syntax_tree.rs +++ b/src/uu/expr/src/syntax_tree.rs @@ -89,8 +89,8 @@ impl RelationOp { impl NumericOp { fn eval(&self, left: &AstNode, right: &AstNode) -> ExprResult { - let a = >>::into(left.eval()?)?; - let b = >>::into(right.eval()?)?; + let a = ExprResult::::from(left.eval()?)?; + let b = ExprResult::::from(right.eval()?)?; Ok(NumOrStr::Num(match self { Self::Add => a + b, Self::Sub => a - b, @@ -218,29 +218,29 @@ impl From for NumOrStr { } } -impl Into> for NumOrStr { - fn into(self) -> Option { - match self.into() { +impl From for Option { + fn from(s: NumOrStr) -> Self { + match s.into() { Ok(num) => num.to_usize(), Err(_) => None, } } } -impl Into for NumOrStr { - fn into(self) -> String { - match self { - Self::Num(num) => num.to_string(), - Self::Str(str) => str.to_string(), +impl From for String { + fn from(s: NumOrStr) -> Self { + match s { + NumOrStr::Num(num) => num.to_string(), + NumOrStr::Str(str) => str.to_string(), } } } -impl Into> for NumOrStr { - fn into(self) -> ExprResult { - match self { - Self::Num(num) => Ok(num), - Self::Str(str) => str +impl From for ExprResult { + fn from(s: NumOrStr) -> Self { + match s { + NumOrStr::Num(num) => Ok(num), + NumOrStr::Str(str) => str .parse::() .map_err(|_| ExprError::NonIntegerArgument), } @@ -303,9 +303,8 @@ impl AstNode { // // So we coerce errors into 0 to make that the only case we // have to care about. - let pos: usize = >>::into(pos.eval()?).unwrap_or(0); - let length: usize = - >>::into(length.eval()?).unwrap_or(0); + let pos: usize = Option::::from(pos.eval()?).unwrap_or(0); + let length: usize = Option::::from(length.eval()?).unwrap_or(0); let (Some(pos), Some(_)) = (pos.checked_sub(1), length.checked_sub(1)) else { return Ok(NumOrStr::from(String::new())); @@ -315,11 +314,9 @@ impl AstNode { string.chars().skip(pos).take(length).collect::(), )) } - Self::Length { string } => Ok(NumOrStr::from( - >::into(string.eval()?) - .chars() - .count(), - )), + Self::Length { string } => { + Ok(NumOrStr::from(String::from(string.eval()?).chars().count())) + } } } }