Skip to content

Commit c1d2b5c

Browse files
Fix BytesN to ScVal conversion in testutils (#1442)
### What Change BytesN to ScVal conversion from TryFrom to From in test utilities. ### Why Unwrapping is safe because values in test environment must be valid ScVal. Even if this was wrong, it's a good tradeoff that improves the developer experience. The Bytes->ScVal and String->ScVal conversions already use this pattern and it seems BytesN wasn't updated at the same time to use that pattern.
1 parent d150517 commit c1d2b5c

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

soroban-sdk/src/bytes.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,18 +1076,21 @@ impl<const N: usize> From<&BytesN<N>> for Bytes {
10761076
}
10771077

10781078
#[cfg(not(target_family = "wasm"))]
1079-
impl<const N: usize> TryFrom<&BytesN<N>> for ScVal {
1080-
type Error = ConversionError;
1081-
fn try_from(v: &BytesN<N>) -> Result<Self, ConversionError> {
1082-
Ok(ScVal::try_from_val(&v.0.env, &v.0.obj.to_val())?)
1079+
impl<const N: usize> From<&BytesN<N>> for ScVal {
1080+
fn from(v: &BytesN<N>) -> Self {
1081+
// This conversion occurs only in test utilities, and theoretically all
1082+
// values should convert to an ScVal because the Env won't let the host
1083+
// type to exist otherwise, unwrapping. Even if there are edge cases
1084+
// that don't, this is a trade off for a better test developer
1085+
// experience.
1086+
ScVal::try_from_val(&v.0.env, &v.0.obj.to_val()).unwrap()
10831087
}
10841088
}
10851089

10861090
#[cfg(not(target_family = "wasm"))]
1087-
impl<const N: usize> TryFrom<BytesN<N>> for ScVal {
1088-
type Error = ConversionError;
1089-
fn try_from(v: BytesN<N>) -> Result<Self, ConversionError> {
1090-
(&v).try_into()
1091+
impl<const N: usize> From<BytesN<N>> for ScVal {
1092+
fn from(v: BytesN<N>) -> Self {
1093+
(&v).into()
10911094
}
10921095
}
10931096

0 commit comments

Comments
 (0)