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

List and SExp now impl From<Vec<Element>> and FromIterator<Element> #877

Merged
merged 1 commit into from
Dec 11, 2024
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
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(())
}
}
Loading