Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add conversion from references of tuples to ScVal #318

Merged
merged 2 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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