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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
use bitflags::bitflags;

bitflags! {
    /// Returned from [RenderingContext::supported_samples], describes which sampling grid sizes
    /// are available.
    ///
    /// See [RenderingContext::supported_samples] for details.
    pub struct SupportedSamples: u8 {
        const NONE = 0b00000000;
        const SAMPLES_2 = 0b00000010;
        const SAMPLES_4 = 0b00000100;
        const SAMPLES_8 = 0b00001000;
        const SAMPLES_16 = 0b00010000;
    }
}

impl SupportedSamples {
    /// The maximum sampling grid size that is supported.
    pub fn max_samples(&self) -> Option<u8> {
        if self.contains(SupportedSamples::SAMPLES_16) {
            Some(16)
        } else if self.contains(SupportedSamples::SAMPLES_8) {
            Some(8)
        } else if self.contains(SupportedSamples::SAMPLES_4) {
            Some(4)
        } else if self.contains(SupportedSamples::SAMPLES_2) {
            Some(2)
        } else {
            None
        }
    }
}

impl IntoIterator for SupportedSamples {
    type Item = u8;
    type IntoIter = SupportedSamplesIter;

    fn into_iter(self) -> Self::IntoIter {
        let current = self.max_samples().unwrap_or(0);

        SupportedSamplesIter {
            supported_samples: self,
            current,
        }
    }
}

/// Iterator over the available sampling grid sizes in a [SupportedSamples] value.
pub struct SupportedSamplesIter {
    supported_samples: SupportedSamples,
    current: u8,
}

impl Iterator for SupportedSamplesIter {
    type Item = u8;

    fn next(&mut self) -> Option<Self::Item> {
        if self.current > 0 {
            let current = self.current;

            loop {
                self.current >>= 1;

                if self.current & self.supported_samples.bits() != 0 || self.current == 0 {
                    break;
                }
            }

            Some(current)
        } else {
            None
        }
    }
}

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

    #[test]
    fn test_supported_samples() {
        let supported_samples = SupportedSamples::SAMPLES_8 | SupportedSamples::SAMPLES_4;

        assert_eq!(supported_samples.max_samples(), Some(8));
        assert_eq!(
            supported_samples.into_iter().collect::<Vec<_>>(),
            vec![8, 4]
        )
    }

    #[test]
    fn test_supported_samples_none() {
        let supported_samples = SupportedSamples::NONE;

        assert_eq!(supported_samples.max_samples(), None);
        assert!(supported_samples
            .into_iter()
            .collect::<Vec<u8>>()
            .is_empty())
    }
}