allocator: general impls for Arc<Mutex<_>> and Rc<RefCell<_>>

This commit is contained in:
i509VCB 2021-11-29 12:18:05 -06:00
parent 769fc8cfd2
commit 210ab8fb21
1 changed files with 37 additions and 0 deletions

View File

@ -22,6 +22,12 @@ pub mod dumb;
pub mod gbm;
mod swapchain;
use std::{
cell::RefCell,
rc::Rc,
sync::{Arc, Mutex},
};
use crate::utils::{Buffer as BufferCoords, Size};
pub use swapchain::{Slot, Swapchain};
@ -60,3 +66,34 @@ pub trait Allocator<B: Buffer> {
modifiers: &[Modifier],
) -> Result<B, Self::Error>;
}
// General implementations for interior mutability.
impl<A: Allocator<B>, B: Buffer> Allocator<B> for Arc<Mutex<A>> {
type Error = A::Error;
fn create_buffer(
&mut self,
width: u32,
height: u32,
fourcc: Fourcc,
modifiers: &[Modifier],
) -> Result<B, Self::Error> {
let mut guard = self.lock().unwrap();
guard.create_buffer(width, height, fourcc, modifiers)
}
}
impl<A: Allocator<B>, B: Buffer> Allocator<B> for Rc<RefCell<A>> {
type Error = A::Error;
fn create_buffer(
&mut self,
width: u32,
height: u32,
fourcc: Fourcc,
modifiers: &[Modifier],
) -> Result<B, Self::Error> {
self.borrow_mut().create_buffer(width, height, fourcc, modifiers)
}
}