Skip to main content

strat9_kernel/dma/
direction.rs

1use core::fmt::Debug;
2
3/// Compile-time direction constraint for DMA buffers.
4///
5/// Implemented by three marker types:
6/// - [`DmaToDevice`] : write-only from the CPU perspective
7/// - [`DmaFromDevice`] : read-only from the CPU perspective
8/// - [`DmaBidirectional`] : full read/write
9///
10/// The associated constants are checked at compile time via
11/// `const { assert!(D::CAN_READ) }` blocks, preventing e.g.
12/// reading from a write-only buffer at zero runtime cost.
13pub trait DmaDirection: Debug + sealed::Sealed {
14    /// Whether the CPU may read data transferred from the device.
15    const CAN_READ: bool;
16    /// Whether the CPU may write data to be transferred to the device.
17    const CAN_WRITE: bool;
18}
19
20mod sealed {
21    pub trait Sealed {}
22}
23
24/// Write-only: CPU writes data, device reads it.
25#[derive(Debug, Clone, Copy)]
26pub struct DmaToDevice;
27
28impl sealed::Sealed for DmaToDevice {}
29impl DmaDirection for DmaToDevice {
30    const CAN_READ: bool = false;
31    const CAN_WRITE: bool = true;
32}
33
34/// Read-only: device writes data, CPU reads it.
35#[derive(Debug, Clone, Copy)]
36pub struct DmaFromDevice;
37
38impl sealed::Sealed for DmaFromDevice {}
39impl DmaDirection for DmaFromDevice {
40    const CAN_READ: bool = true;
41    const CAN_WRITE: bool = false;
42}
43
44/// Full read/write in both directions.
45#[derive(Debug, Clone, Copy)]
46pub struct DmaBidirectional;
47
48impl sealed::Sealed for DmaBidirectional {}
49impl DmaDirection for DmaBidirectional {
50    const CAN_READ: bool = true;
51    const CAN_WRITE: bool = true;
52}