rendy_chain/schedule/
family.rs

1use super::{
2    queue::{Queue, QueueId},
3    submission::{Submission, SubmissionId},
4};
5
6/// Instances of this type contains array of `Queue`s.
7/// All contained queues has identical capabilities.
8#[derive(Clone, Debug)]
9pub struct Family<S> {
10    id: rendy_core::hal::queue::QueueFamilyId,
11    queues: Vec<Queue<S>>,
12}
13
14impl<S> Family<S> {
15    /// Create new empty `Family`
16    pub fn new(id: rendy_core::hal::queue::QueueFamilyId) -> Self {
17        Family {
18            id,
19            queues: Vec::default(),
20        }
21    }
22
23    /// Get id of the family.
24    pub fn id(&self) -> rendy_core::hal::queue::QueueFamilyId {
25        self.id
26    }
27
28    /// Get reference to `Queue` instance by the id.
29    ///
30    /// # Panic
31    ///
32    /// This function will panic if requested queue isn't part of this family.
33    ///
34    pub fn queue(&self, qid: QueueId) -> Option<&Queue<S>> {
35        assert_eq!(self.id, qid.family());
36        self.queues.get(qid.index())
37    }
38
39    /// Get mutable reference to `Queue` instance by the id.
40    ///
41    /// # Panic
42    ///
43    /// This function will panic if requested queue isn't part of this family.
44    ///
45    pub fn queue_mut(&mut self, qid: QueueId) -> Option<&mut Queue<S>> {
46        assert_eq!(self.id, qid.family());
47        self.queues.get_mut(qid.index())
48    }
49
50    /// Get mutable reference to `Queue` instance by the id.
51    /// This function will grow queues array if index is out of bounds.
52    ///
53    /// # Panic
54    ///
55    /// This function will panic if requested queue isn't part of this family.
56    ///
57    pub fn ensure_queue(&mut self, qid: QueueId) -> &mut Queue<S> {
58        assert_eq!(self.id, qid.family());
59        let len = self.queues.len();
60        self.queues
61            .extend((len..qid.index() + 1).map(|i| Queue::new(QueueId::new(qid.family(), i))));
62        &mut self.queues[qid.index()]
63    }
64
65    /// Get reference to `Submission<S>` instance by id.
66    ///
67    /// # Panic
68    ///
69    /// This function will panic if requested submission isn't part of this family.
70    ///
71    pub fn submission(&self, sid: SubmissionId) -> Option<&Submission<S>> {
72        assert_eq!(self.id, sid.family());
73        self.queue(sid.queue())
74            .and_then(|queue| queue.submission(sid))
75    }
76
77    /// Get mutable reference to `Submission<S>` instance by id.
78    ///
79    /// # Panic
80    ///
81    /// This function will panic if requested submission isn't part of this family.
82    ///
83    pub fn submission_mut(&mut self, sid: SubmissionId) -> Option<&mut Submission<S>> {
84        assert_eq!(self.id, sid.family());
85        self.queue_mut(sid.queue())
86            .and_then(|queue| queue.submission_mut(sid))
87    }
88
89    /// Iterate over queues.
90    pub fn iter(&self) -> impl Iterator<Item = &Queue<S>> {
91        self.queues.iter()
92    }
93
94    /// Iterate over queues.
95    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Queue<S>> {
96        self.queues.iter_mut()
97    }
98
99    /// The number of queues in this schedule.
100    pub fn queue_count(&self) -> usize {
101        self.queues.len()
102    }
103}