diff --git a/triton-vm/src/op_stack.rs b/triton-vm/src/op_stack.rs index 99dc77b29..3f64acfbb 100644 --- a/triton-vm/src/op_stack.rs +++ b/triton-vm/src/op_stack.rs @@ -1,9 +1,9 @@ use std::fmt::Display; use std::fmt::Formatter; use std::fmt::Result as FmtResult; -use std::result; use anyhow::anyhow; +use anyhow::bail; use anyhow::Result; use get_size::GetSize; use num_traits::Zero; @@ -182,9 +182,9 @@ impl From<&OpStackElement> for u32 { } impl TryFrom for OpStackElement { - type Error = String; + type Error = anyhow::Error; - fn try_from(stack_index: u32) -> result::Result { + fn try_from(stack_index: u32) -> Result { match stack_index { 0 => Ok(ST0), 1 => Ok(ST1), @@ -202,9 +202,7 @@ impl TryFrom for OpStackElement { 13 => Ok(ST13), 14 => Ok(ST14), 15 => Ok(ST15), - _ => Err(format!( - "Index {stack_index} is out of range for `OpStackElement`." - )), + _ => bail!("Index {stack_index} is out of range for `OpStackElement`."), } } } @@ -216,12 +214,10 @@ impl From for u64 { } impl TryFrom for OpStackElement { - type Error = String; + type Error = anyhow::Error; - fn try_from(stack_index: u64) -> result::Result { - let stack_index = u32::try_from(stack_index) - .map_err(|_| format!("Index {stack_index} is out of range for `OpStackElement`."))?; - stack_index.try_into() + fn try_from(stack_index: u64) -> Result { + u32::try_from(stack_index)?.try_into() } } @@ -238,12 +234,10 @@ impl From<&OpStackElement> for usize { } impl TryFrom for OpStackElement { - type Error = String; + type Error = anyhow::Error; - fn try_from(stack_index: usize) -> result::Result { - let stack_index = - u32::try_from(stack_index).map_err(|_| "Cannot convert usize to u32.".to_string())?; - stack_index.try_into() + fn try_from(stack_index: usize) -> Result { + u32::try_from(stack_index)?.try_into() } }