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

Implement Ord, Hash and Default for Expansions iter #49

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Changes from 1 commit
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
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
}
}

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
Loading