From 2c6894cc7c2d065853542925f3b6c48324154f74 Mon Sep 17 00:00:00 2001 From: pjht Date: Mon, 21 Nov 2022 07:19:11 -0600 Subject: [PATCH] Pass an AccessorBuilder instaed of an Accessor to a DMA handler --- src/backplane.rs | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/backplane.rs b/src/backplane.rs index 5c48c91..d5f0415 100644 --- a/src/backplane.rs +++ b/src/backplane.rs @@ -1,5 +1,6 @@ use std::fmt::Debug; use std::fmt::Display; +use std::marker::PhantomData; use anyhow::anyhow; use itertools::Itertools; @@ -14,23 +15,41 @@ use crate::{ }; 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)] #[allow(dead_code)] -pub struct DMACardAccessor<'a> { +pub struct DMACardAccessorBuilder<'a> { backplane: &'a Backplane, card_no: usize, } -impl DMACardAccessor<'_> { +impl<'a> DMACardAccessorBuilder<'a> { #[allow(dead_code)] - pub fn get(&self) -> Option> { - MutexGuard::try_map(self.backplane.cards.lock(), |cards| { - cards[self.card_no].downcast_mut::() + pub fn build(self) -> DMACardAccessor<'a, T> { + DMACardAccessor { + 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, +} + +impl DMACardAccessor<'_, T> { + #[allow(dead_code)] + pub fn get(&self) -> MappedMutexGuard { + MutexGuard::map(self.backplane.cards.lock(), |cards| { + cards[self.card_no].downcast_mut::().unwrap() }) - .ok() } } @@ -176,7 +195,7 @@ impl Backplane { for handler in self.dma_handlers.lock().iter_mut() { handler.1.handle( self, - DMACardAccessor { + DMACardAccessorBuilder { backplane: self, card_no: handler.0, },