From d667ecf4b8640dd69f346f73b5bca2d4f884e9e2 Mon Sep 17 00:00:00 2001 From: Zack Slayton Date: Wed, 11 Dec 2024 11:55:55 -0500 Subject: [PATCH] List and SExp now impl From> and FromIterator --- src/element/sequence.rs | 2 -- src/types/list.rs | 34 ++++++++++++++++++++++++++++++++-- src/types/sexp.rs | 32 +++++++++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/element/sequence.rs b/src/element/sequence.rs index a081b386c..7a09cbd23 100644 --- a/src/element/sequence.rs +++ b/src/element/sequence.rs @@ -157,8 +157,6 @@ impl<'a> IntoIterator for &'a Sequence { } } -// TODO: This currently clones elements. We should change `Sequence` to wrap a VecDeque so we can -// pop from the front. impl IntoIterator for Sequence { type Item = Element; // TODO: Change once `impl Trait` type aliases are stable diff --git a/src/types/list.rs b/src/types/list.rs index 8aa2d3a09..a26a2523f 100644 --- a/src/types/list.rs +++ b/src/types/list.rs @@ -78,7 +78,19 @@ impl Display for List { impl From for List { fn from(sequence: Sequence) -> Self { - List(sequence) + Self(sequence) + } +} + +impl From> for List { + fn from(elements: Vec) -> Self { + Self(elements.into()) + } +} + +impl FromIterator for List { + fn from_iter>(iter: T) -> Self { + Vec::from_iter(iter).into() } } @@ -90,7 +102,7 @@ impl From for Sequence { #[cfg(test)] mod tests { - use crate::{ion_list, IonResult}; + use crate::{ion_list, Element, IonResult, List}; #[test] fn for_element_in_list() -> IonResult<()> { @@ -103,4 +115,22 @@ mod tests { assert_eq!(sum, 6i64); Ok(()) } + + #[test] + fn list_from_vec() -> IonResult<()> { + let elements = vec![Element::int(1), Element::int(2), Element::int(3)]; + let actual: List = elements.into(); + let expected = ion_list![1, 2, 3]; + assert_eq!(actual, expected); + Ok(()) + } + + #[test] + fn list_from_iter() -> IonResult<()> { + let elements = vec![Element::int(1), Element::int(2), Element::int(3)]; + let actual: List = elements.into_iter().collect(); + let expected = ion_list![1, 2, 3]; + assert_eq!(actual, expected); + Ok(()) + } } diff --git a/src/types/sexp.rs b/src/types/sexp.rs index 31478c939..d822ed3e5 100644 --- a/src/types/sexp.rs +++ b/src/types/sexp.rs @@ -82,6 +82,18 @@ impl From for SExp { } } +impl From> for SExp { + fn from(elements: Vec) -> Self { + Self(elements.into()) + } +} + +impl FromIterator for SExp { + fn from_iter>(iter: T) -> Self { + Vec::from_iter(iter).into() + } +} + impl From for Sequence { fn from(value: SExp) -> Self { value.0 @@ -90,7 +102,7 @@ impl From for Sequence { #[cfg(test)] mod tests { - use crate::{ion_sexp, IonResult}; + use crate::{ion_sexp, Element, IonResult, SExp}; #[test] fn for_element_in_sexp() -> IonResult<()> { @@ -103,4 +115,22 @@ mod tests { assert_eq!(sum, 6i64); Ok(()) } + + #[test] + fn sexp_from_vec() -> IonResult<()> { + let elements = vec![Element::int(1), Element::int(2), Element::int(3)]; + let actual: SExp = elements.into(); + let expected = ion_sexp!(1 2 3); + assert_eq!(actual, expected); + Ok(()) + } + + #[test] + fn sexp_from_iter() -> IonResult<()> { + let elements = vec![Element::int(1), Element::int(2), Element::int(3)]; + let actual: SExp = elements.into_iter().collect(); + let expected = ion_sexp!(1 2 3); + assert_eq!(actual, expected); + Ok(()) + } }