aboutsummaryrefslogtreecommitdiff
path: root/stockton-render/src/draw_passes/util.rs
blob: 7e82209ac3796a157d505317df845445b9645d75 (plain)
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
//! Utility structs & functions

use anyhow::Result;

/// Keeps a given resource for each swapchain image
pub struct TargetSpecificResources<T> {
    elements: Vec<T>,
    next_idx: usize,
}

impl<T> TargetSpecificResources<T> {
    /// Create a new set of resources, given a function to generate them and the count
    /// In most cases, count should be swapchain_properties.image_count
    pub fn new<F>(mut generator: F, count: usize) -> Result<Self>
    where
        F: FnMut() -> Result<T>,
    {
        let mut elements = Vec::with_capacity(count);
        for _ in 0..count {
            elements.push(generator()?);
        }

        Ok(TargetSpecificResources {
            elements,
            next_idx: 0,
        })
    }

    /// Get the next resource, wrapping around if necessary.
    pub fn get_next(&mut self) -> &T {
        let el = &self.elements[self.next_idx];
        self.next_idx = (self.next_idx + 1) % self.elements.len();
        el
    }

    /// Dissolve the resource set, returning an iterator over each item.
    /// In most cases, each item will need deactivated.
    pub fn dissolve(self) -> impl Iterator<Item = T> {
        self.elements.into_iter()
    }
}