From 30a5204c53b8802f59435123f4341833a92c1684 Mon Sep 17 00:00:00 2001 From: Steve Wooster Date: Thu, 26 Oct 2023 13:19:42 -0700 Subject: [PATCH] Implement Ord, Hash and Default for Expansions iter --- src/expansions.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/expansions.rs b/src/expansions.rs index 27f76e3..9de20ae 100644 --- a/src/expansions.rs +++ b/src/expansions.rs @@ -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 { @@ -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 { @@ -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(); @@ -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()));