noodles_sam/alignment/record/
quality_scores.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use std::io;

/// Alignment record quality scores.
pub trait QualityScores {
    /// Returns whether there are any scores.
    fn is_empty(&self) -> bool;

    /// Returns the number of scores.
    fn len(&self) -> usize;

    /// Returns an iterator over scores.
    fn iter(&self) -> Box<dyn Iterator<Item = io::Result<u8>> + '_>;
}

impl<'a> IntoIterator for &'a dyn QualityScores {
    type Item = io::Result<u8>;
    type IntoIter = Box<dyn Iterator<Item = Self::Item> + 'a>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl QualityScores for Box<dyn QualityScores + '_> {
    fn is_empty(&self) -> bool {
        (**self).is_empty()
    }

    fn len(&self) -> usize {
        (**self).len()
    }

    fn iter(&self) -> Box<dyn Iterator<Item = io::Result<u8>> + '_> {
        (**self).iter()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_into_iter() -> io::Result<()> {
        struct T(Vec<u8>);

        impl QualityScores for T {
            fn is_empty(&self) -> bool {
                self.0.is_empty()
            }

            fn len(&self) -> usize {
                self.0.len()
            }

            fn iter(&self) -> Box<dyn Iterator<Item = io::Result<u8>> + '_> {
                Box::new(self.0.iter().copied().map(Ok))
            }
        }

        let quality_scores: &dyn QualityScores = &T(vec![45, 35, 43, 50]);

        assert_eq!(
            quality_scores.into_iter().collect::<io::Result<Vec<_>>>()?,
            [45, 35, 43, 50]
        );

        Ok(())
    }
}