rendy_chain/chain/
mod.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
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
//!
//! This module defines types to reason about what resources referenced in what submissions.
//! How commands from those submissions access resources.
//! This information allows to derive synchronization required.
//!

use std::collections::HashMap;

mod link;

use std::ops::BitOr;

use crate::{
    resource::{Buffer, Image, Resource},
    Id,
};

pub use self::link::{Link, LinkNode};

/// This type corresponds to resource category.
/// All resources from the same category must be accessed as permitted by links of the chain.
#[derive(Clone, Debug)]
pub struct Chain<R: Resource> {
    links: Vec<Link<R>>,
}

impl<R> Chain<R>
where
    R: Resource,
{
    /// Get links slice
    pub fn links(&self) -> &[Link<R>] {
        &self.links
    }

    /// Create new empty `Chain`
    pub fn new() -> Self {
        Chain { links: Vec::new() }
    }

    /// Get links slice
    pub fn last_link_mut(&mut self) -> Option<&mut Link<R>> {
        self.links.last_mut()
    }

    /// Add new link to the chain.
    pub fn add_link(&mut self, link: Link<R>) -> &mut Link<R> {
        self.links.push(link);
        self.links.last_mut().unwrap()
    }

    // /// Get link by index.
    // pub(crate) fn link(&self, index: usize) -> &Link<R> {
    //     &self.links[index]
    // }

    // /// Get link by index.
    // pub(crate) fn link_mut(&mut self, index: usize) -> &mut Link<R> {
    //     &mut self.links[index]
    // }

    // /// Get link by index.
    // pub(crate) fn next_link(&self, index: usize) -> &Link<R> {
    //     let index = (index + 1) % self.links.len();
    //     self.link(index)
    // }

    // /// Get link by index.
    // pub(crate) fn next_link_mut(&mut self, index: usize) -> &mut Link<R> {
    //     let index = (index + 1) % self.links.len();
    //     self.link_mut(index)
    // }

    // /// Get link by index.
    // pub(crate) fn prev_link(&self, index: usize) -> &Link<R> {
    //     let index = (index + self.links.len() - 1) % self.links.len();
    //     self.link(index)
    // }

    // /// Get link by index.
    // pub(crate) fn prev_link_mut(&mut self, index: usize) -> &mut Link<R> {
    //     let index = (index + self.links.len() - 1) % self.links.len();
    //     self.link_mut(index)
    // }

    /// Get total usage.
    pub fn usage(&self) -> R::Usage {
        self.links
            .iter()
            .map(Link::usage)
            .fold(R::no_usage(), BitOr::bitor)
    }
}

/// Type alias for map of chains by id for buffers.
pub(crate) type BufferChains = HashMap<Id, Chain<Buffer>>;

/// Type alias for map of chains by id for images.
pub(crate) type ImageChains = HashMap<Id, Chain<Image>>;