Pass an AccessorBuilder instaed of an Accessor to a DMA handler

This commit is contained in:
pjht 2022-11-21 07:19:11 -06:00
parent b5409f3590
commit 2c6894cc7c

View File

@ -1,5 +1,6 @@
use std::fmt::Debug; use std::fmt::Debug;
use std::fmt::Display; use std::fmt::Display;
use std::marker::PhantomData;
use anyhow::anyhow; use anyhow::anyhow;
use itertools::Itertools; use itertools::Itertools;
@ -14,23 +15,41 @@ use crate::{
}; };
pub trait DMAHandler: Debug { pub trait DMAHandler: Debug {
fn handle<'a>(&mut self, backplane: &'a Backplane, card_accessor: DMACardAccessor<'a>); fn handle<'a>(&mut self, backplane: &'a Backplane, card_accessor: DMACardAccessorBuilder<'a>);
} }
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
#[allow(dead_code)] #[allow(dead_code)]
pub struct DMACardAccessor<'a> { pub struct DMACardAccessorBuilder<'a> {
backplane: &'a Backplane, backplane: &'a Backplane,
card_no: usize, card_no: usize,
} }
impl DMACardAccessor<'_> { impl<'a> DMACardAccessorBuilder<'a> {
#[allow(dead_code)] #[allow(dead_code)]
pub fn get<T: Card>(&self) -> Option<MappedMutexGuard<T>> { pub fn build<T: Card>(self) -> DMACardAccessor<'a, T> {
MutexGuard::try_map(self.backplane.cards.lock(), |cards| { DMACardAccessor {
cards[self.card_no].downcast_mut::<T>() backplane: self.backplane,
card_no: self.card_no,
card_type: PhantomData,
}
}
}
#[derive(Copy, Clone, Debug)]
#[allow(dead_code)]
pub struct DMACardAccessor<'a, T> {
backplane: &'a Backplane,
card_no: usize,
card_type: PhantomData<T>,
}
impl<T: Card> DMACardAccessor<'_, T> {
#[allow(dead_code)]
pub fn get(&self) -> MappedMutexGuard<T> {
MutexGuard::map(self.backplane.cards.lock(), |cards| {
cards[self.card_no].downcast_mut::<T>().unwrap()
}) })
.ok()
} }
} }
@ -176,7 +195,7 @@ impl Backplane {
for handler in self.dma_handlers.lock().iter_mut() { for handler in self.dma_handlers.lock().iter_mut() {
handler.1.handle( handler.1.handle(
self, self,
DMACardAccessor { DMACardAccessorBuilder {
backplane: self, backplane: self,
card_no: handler.0, card_no: handler.0,
}, },