Skip to content

Commit

Permalink
Add conversion from references of tuples to ScVal (#318)
Browse files Browse the repository at this point in the history
Co-authored-by: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com>
  • Loading branch information
brson and leighmcculloch authored Nov 4, 2023
1 parent 2597c26 commit c7c46d1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/curr/scval_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,18 @@ macro_rules! impl_for_tuple {
}
}

#[cfg(feature = "alloc")]
impl<$($typ),*> TryFrom<&($($typ,)*)> for ScVec
where
$($typ: TryInto<ScVal> + Clone),*
{
type Error = ();
fn try_from(v: &($($typ,)*)) -> Result<Self, Self::Error> {
let vec: Vec<ScVal> = vec![$(v.$idx.clone().try_into().map_err(|_| ())?),+];
Ok(ScVec(vec.try_into()?))
}
}

#[cfg(feature = "alloc")]
impl<$($typ),*> TryFrom<($($typ,)*)> for ScVal
where
Expand All @@ -584,6 +596,17 @@ macro_rules! impl_for_tuple {
}
}

#[cfg(feature = "alloc")]
impl<$($typ),*> TryFrom<&($($typ,)*)> for ScVal
where
$($typ: TryInto<ScVal> + Clone),*
{
type Error = ();
fn try_from(v: &($($typ,)*)) -> Result<Self, ()> {
Ok(ScVal::Vec(Some(<_ as TryInto<ScVec>>::try_into(v)?)))
}
}

impl<$($typ),*> TryFrom<ScVec> for ($($typ,)*)
where
// TODO: Consider removing the Clone constraint by changing the
Expand Down Expand Up @@ -795,6 +818,18 @@ mod test {
assert_eq!(v, roundtrip);
}

#[cfg(feature = "alloc")]
#[test]
fn tuple_refs() {
extern crate alloc;
use alloc::vec;
use alloc::vec::Vec;
let v = &(1i32, 2i64, vec![true, false]);
let val: ScVal = v.try_into().unwrap();
let roundtrip: (i32, i64, Vec<bool>) = val.try_into().unwrap();
assert_eq!(v, &roundtrip);
}

#[test]
fn option() {
let v: Option<i64> = Some(1);
Expand Down
35 changes: 35 additions & 0 deletions src/next/scval_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,18 @@ macro_rules! impl_for_tuple {
}
}

#[cfg(feature = "alloc")]
impl<$($typ),*> TryFrom<&($($typ,)*)> for ScVec
where
$($typ: TryInto<ScVal> + Clone),*
{
type Error = ();
fn try_from(v: &($($typ,)*)) -> Result<Self, Self::Error> {
let vec: Vec<ScVal> = vec![$(v.$idx.clone().try_into().map_err(|_| ())?),+];
Ok(ScVec(vec.try_into()?))
}
}

#[cfg(feature = "alloc")]
impl<$($typ),*> TryFrom<($($typ,)*)> for ScVal
where
Expand All @@ -584,6 +596,17 @@ macro_rules! impl_for_tuple {
}
}

#[cfg(feature = "alloc")]
impl<$($typ),*> TryFrom<&($($typ,)*)> for ScVal
where
$($typ: TryInto<ScVal> + Clone),*
{
type Error = ();
fn try_from(v: &($($typ,)*)) -> Result<Self, ()> {
Ok(ScVal::Vec(Some(<_ as TryInto<ScVec>>::try_into(v)?)))
}
}

impl<$($typ),*> TryFrom<ScVec> for ($($typ,)*)
where
// TODO: Consider removing the Clone constraint by changing the
Expand Down Expand Up @@ -795,6 +818,18 @@ mod test {
assert_eq!(v, roundtrip);
}

#[cfg(feature = "alloc")]
#[test]
fn tuple_refs() {
extern crate alloc;
use alloc::vec;
use alloc::vec::Vec;
let v = &(1i32, 2i64, vec![true, false]);
let val: ScVal = v.try_into().unwrap();
let roundtrip: (i32, i64, Vec<bool>) = val.try_into().unwrap();
assert_eq!(v, &roundtrip);
}

#[test]
fn option() {
let v: Option<i64> = Some(1);
Expand Down

0 comments on commit c7c46d1

Please sign in to comment.