Skip to content

Commit

Permalink
List and SExp now impl From<Vec<Element>> and FromIterator<Element>
Browse files Browse the repository at this point in the history
  • Loading branch information
zslayton committed Dec 11, 2024
1 parent 3f2cab7 commit d667ecf
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
2 changes: 0 additions & 2 deletions src/element/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 32 additions & 2 deletions src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,19 @@ impl Display for List {

impl From<Sequence> for List {
fn from(sequence: Sequence) -> Self {
List(sequence)
Self(sequence)
}
}

impl From<Vec<Element>> for List {
fn from(elements: Vec<Element>) -> Self {
Self(elements.into())
}
}

impl FromIterator<Element> for List {
fn from_iter<T: IntoIterator<Item = Element>>(iter: T) -> Self {
Vec::from_iter(iter).into()
}
}

Expand All @@ -90,7 +102,7 @@ impl From<List> 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<()> {
Expand All @@ -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(())
}
}
32 changes: 31 additions & 1 deletion src/types/sexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ impl From<Sequence> for SExp {
}
}

impl From<Vec<Element>> for SExp {
fn from(elements: Vec<Element>) -> Self {
Self(elements.into())
}
}

impl FromIterator<Element> for SExp {
fn from_iter<T: IntoIterator<Item = Element>>(iter: T) -> Self {
Vec::from_iter(iter).into()
}
}

impl From<SExp> for Sequence {
fn from(value: SExp) -> Self {
value.0
Expand All @@ -90,7 +102,7 @@ impl From<SExp> 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<()> {
Expand All @@ -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(())
}
}

0 comments on commit d667ecf

Please sign in to comment.