Skip to content

Commit

Permalink
Implement Ord, Hash and Default for Expansions iter (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
swooster authored Oct 26, 2023
1 parent a5a4f1f commit 0d015f2
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/expansions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct Ambiguity {
}

/// Unambiguous DNA expansion produced by [`Expansions`] iterator.
#[derive(Clone, PartialEq, Eq)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Expansion(Arc<[Nucleotide]>);

impl Expansions {
Expand All @@ -56,6 +56,13 @@ impl Expansions {
began: false,
}
}

pub fn empty() -> Self {
// TODO: Possibly make this skip allocation by reusing Arc?
let mut this = Self::new(&[]);
this.next(); // `this` contains a single empty expansion; skip it
this
}
}

impl Iterator for Expansions {
Expand Down Expand Up @@ -100,6 +107,12 @@ impl Iterator for Expansions {
}
}

impl Default for Expansions {
fn default() -> Self {
Self::empty()
}
}

impl std::fmt::Debug for Expansions {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let dna = self.buf.iter().map(|&nuc| nuc.into()).collect();
Expand Down Expand Up @@ -273,6 +286,13 @@ mod test {
assert_eq!(expansions, expected);
}

#[test]
fn empty_expansions() {
let mut empty = Expansions::empty();
assert_eq!(empty.size_hint(), (0, Some(0)));
assert_eq!(empty.next(), None);
}

#[test]
fn debug_expansion() {
let expansion = Expansion(Arc::from(dna("ATCG").as_slice()));
Expand Down

0 comments on commit 0d015f2

Please sign in to comment.