rendy_chain/chain/mod.rs
1//!
2//! This module defines types to reason about what resources referenced in what submissions.
3//! How commands from those submissions access resources.
4//! This information allows to derive synchronization required.
5//!
6
7use std::collections::HashMap;
8
9mod link;
10
11use std::ops::BitOr;
12
13use crate::{
14 resource::{Buffer, Image, Resource},
15 Id,
16};
17
18pub use self::link::{Link, LinkNode};
19
20/// This type corresponds to resource category.
21/// All resources from the same category must be accessed as permitted by links of the chain.
22#[derive(Clone, Debug)]
23pub struct Chain<R: Resource> {
24 links: Vec<Link<R>>,
25}
26
27impl<R> Chain<R>
28where
29 R: Resource,
30{
31 /// Get links slice
32 pub fn links(&self) -> &[Link<R>] {
33 &self.links
34 }
35
36 /// Create new empty `Chain`
37 pub fn new() -> Self {
38 Chain { links: Vec::new() }
39 }
40
41 /// Get links slice
42 pub fn last_link_mut(&mut self) -> Option<&mut Link<R>> {
43 self.links.last_mut()
44 }
45
46 /// Add new link to the chain.
47 pub fn add_link(&mut self, link: Link<R>) -> &mut Link<R> {
48 self.links.push(link);
49 self.links.last_mut().unwrap()
50 }
51
52 // /// Get link by index.
53 // pub(crate) fn link(&self, index: usize) -> &Link<R> {
54 // &self.links[index]
55 // }
56
57 // /// Get link by index.
58 // pub(crate) fn link_mut(&mut self, index: usize) -> &mut Link<R> {
59 // &mut self.links[index]
60 // }
61
62 // /// Get link by index.
63 // pub(crate) fn next_link(&self, index: usize) -> &Link<R> {
64 // let index = (index + 1) % self.links.len();
65 // self.link(index)
66 // }
67
68 // /// Get link by index.
69 // pub(crate) fn next_link_mut(&mut self, index: usize) -> &mut Link<R> {
70 // let index = (index + 1) % self.links.len();
71 // self.link_mut(index)
72 // }
73
74 // /// Get link by index.
75 // pub(crate) fn prev_link(&self, index: usize) -> &Link<R> {
76 // let index = (index + self.links.len() - 1) % self.links.len();
77 // self.link(index)
78 // }
79
80 // /// Get link by index.
81 // pub(crate) fn prev_link_mut(&mut self, index: usize) -> &mut Link<R> {
82 // let index = (index + self.links.len() - 1) % self.links.len();
83 // self.link_mut(index)
84 // }
85
86 /// Get total usage.
87 pub fn usage(&self) -> R::Usage {
88 self.links
89 .iter()
90 .map(Link::usage)
91 .fold(R::no_usage(), BitOr::bitor)
92 }
93}
94
95/// Type alias for map of chains by id for buffers.
96pub(crate) type BufferChains = HashMap<Id, Chain<Buffer>>;
97
98/// Type alias for map of chains by id for images.
99pub(crate) type ImageChains = HashMap<Id, Chain<Image>>;