From 2b7d6ea1892b381d2baf9e62c298d60f4fb0cf51 Mon Sep 17 00:00:00 2001 From: Graydon Hoare Date: Wed, 6 Sep 2023 22:48:10 -0700 Subject: [PATCH] Make Env::Error: Into, use for errors in bytes.rs --- soroban-env-common/src/bytes.rs | 19 ++++++++++++------- soroban-env-common/src/env.rs | 2 +- soroban-env-common/src/val.rs | 3 +++ soroban-env-host/src/host/error.rs | 6 ++++++ 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/soroban-env-common/src/bytes.rs b/soroban-env-common/src/bytes.rs index 693c4f99b..1c4410186 100644 --- a/soroban-env-common/src/bytes.rs +++ b/soroban-env-common/src/bytes.rs @@ -1,5 +1,7 @@ use crate::{ - declare_tag_based_object_wrapper, ConversionError, Env, Error, TryFromVal, TryIntoVal, Val, + declare_tag_based_object_wrapper, + xdr::{ScErrorCode, ScErrorType}, + Env, Error, TryFromVal, TryIntoVal, Val, }; declare_tag_based_object_wrapper!(BytesObject); @@ -8,14 +10,17 @@ impl TryFromVal for [u8; N] { type Error = Error; fn try_from_val(env: &E, val: &BytesObject) -> Result { - let len: u32 = env.bytes_len(*val).map_err(|_| ConversionError)?.into(); + let len: u32 = env.bytes_len(*val).map_err(Into::into)?.into(); let len = len as usize; if len != N { - return Err(ConversionError.into()); + return Err(Error::from_type_and_code( + ScErrorType::Value, + ScErrorCode::UnexpectedSize, + )); } let mut arr = [0u8; N]; env.bytes_copy_to_slice(*val, Val::U32_ZERO, &mut arr) - .map_err(|_| ConversionError)?; + .map_err(Into::into)?; Ok(arr) } } @@ -34,11 +39,11 @@ impl TryFromVal for Vec { type Error = Error; fn try_from_val(env: &E, val: &BytesObject) -> Result { - let len: u32 = env.bytes_len(*val).map_err(|_| ConversionError)?.into(); + let len: u32 = env.bytes_len(*val).map_err(Into::into)?.into(); let len = len as usize; let mut vec = vec![0u8; len]; env.bytes_copy_to_slice(*val, Val::U32_ZERO, &mut vec) - .map_err(|_| ConversionError)?; + .map_err(Into::into)?; Ok(vec) } } @@ -57,7 +62,7 @@ impl TryFromVal for BytesObject { type Error = Error; #[inline(always)] fn try_from_val(env: &E, v: &&[u8]) -> Result { - Ok(env.bytes_new_from_slice(v).map_err(|_| ConversionError)?) + env.bytes_new_from_slice(v).map_err(Into::into) } } diff --git a/soroban-env-common/src/env.rs b/soroban-env-common/src/env.rs index 4fb35b78f..faa770975 100644 --- a/soroban-env-common/src/env.rs +++ b/soroban-env-common/src/env.rs @@ -32,7 +32,7 @@ pub trait EnvBase: Sized + Clone { /// environment-interface level, and then either directly handle or escalate /// the contained `Error` code to the user as a `Error` or `Result<>` of /// some other type, depending on the API. - type Error: core::fmt::Debug; + type Error: core::fmt::Debug + Into; /// Reject an error from the environment, turning it into a panic but on /// terms that the environment controls (eg. transforming or logging it). diff --git a/soroban-env-common/src/val.rs b/soroban-env-common/src/val.rs index e3caf6d20..843fb071a 100644 --- a/soroban-env-common/src/val.rs +++ b/soroban-env-common/src/val.rs @@ -1,3 +1,6 @@ +// This permits globals prouced by derive(num_enum::TryFromPrimitive) below. +#![cfg_attr(test, allow(non_upper_case_globals))] + use crate::{ declare_tag_based_object_wrapper, declare_tag_based_wrapper, impl_rawval_wrapper_base, impl_tryfroms_and_tryfromvals_delegating_to_rawvalconvertible, Compare, I32Val, SymbolSmall, diff --git a/soroban-env-host/src/host/error.rs b/soroban-env-host/src/host/error.rs index 4aa5d6394..c7056e1ca 100644 --- a/soroban-env-host/src/host/error.rs +++ b/soroban-env-host/src/host/error.rs @@ -29,6 +29,12 @@ pub struct HostError { impl std::error::Error for HostError {} +impl Into for HostError { + fn into(self) -> Error { + self.error + } +} + impl Debug for HostError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { // We do a little trimming here, skipping the first two frames (which