1 //! Traits for trying to obtain a queueable, writable buffer from its index.
2 //!
3 //! `try_get_buffer()` returns the buffer with specified `index`, provided that
4 //! this buffer is currently available for use.
5 //!
6 //! The returned buffer shall not outlive the object that produced it.
7 
8 use thiserror::Error;
9 
10 use crate::memory::BufferHandles;
11 
12 use super::{CaptureQueueableProvider, OutputQueueableProvider};
13 
14 #[derive(Debug, Error)]
15 pub enum TryGetBufferError {
16     #[error("buffer with provided index {0} does not exist")]
17     InvalidIndex(usize),
18     #[error("buffer is already in use")]
19     AlreadyUsed,
20 }
21 
22 pub trait GetOutputBufferByIndex<'a, P: BufferHandles, ErrorType = TryGetBufferError>
23 where
24     Self: OutputQueueableProvider<'a, P>,
25 {
try_get_buffer(&'a self, index: usize) -> Result<Self::Queueable, ErrorType>26     fn try_get_buffer(&'a self, index: usize) -> Result<Self::Queueable, ErrorType>;
27 }
28 
29 pub trait GetCaptureBufferByIndex<'a, P: BufferHandles, ErrorType = TryGetBufferError>
30 where
31     Self: CaptureQueueableProvider<'a, P>,
32 {
try_get_buffer(&'a self, index: usize) -> Result<Self::Queueable, ErrorType>33     fn try_get_buffer(&'a self, index: usize) -> Result<Self::Queueable, ErrorType>;
34 }
35