autd3_driver/datagram/
with_segment.rs

1use autd3_core::{
2    datagram::{
3        Datagram, DatagramOption, DatagramS, DeviceMask, Inspectable, InspectionResult,
4        internal::HasSegment,
5    },
6    environment::Environment,
7    firmware::{Segment, transition_mode::TransitionMode},
8    geometry::Geometry,
9};
10
11/// A wrapper of [`DatagramS`] to specify the segment to write the data.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
13pub struct WithSegment<T: TransitionMode, D> {
14    /// The original [`DatagramS`]
15    pub inner: D,
16    /// The segment to write the data
17    pub segment: Segment,
18    /// Whether to transition the segment
19    pub transition_mode: T,
20}
21
22impl<'a, T, D> WithSegment<T, D>
23where
24    T: TransitionMode,
25    D: DatagramS<'a> + HasSegment<T>,
26{
27    /// Create a new [`WithSegment`].
28    #[must_use]
29    pub const fn new(inner: D, segment: Segment, transition_mode: T) -> Self {
30        Self {
31            inner,
32            segment,
33            transition_mode,
34        }
35    }
36}
37
38impl<'a, T, D> Datagram<'a> for WithSegment<T, D>
39where
40    T: TransitionMode,
41    D: DatagramS<'a> + HasSegment<T>,
42{
43    type G = D::G;
44    type Error = D::Error;
45
46    fn operation_generator(
47        self,
48        geometry: &'a Geometry,
49        env: &Environment,
50        filter: &DeviceMask,
51    ) -> Result<Self::G, Self::Error> {
52        <D as DatagramS>::operation_generator_with_segment(
53            self.inner,
54            geometry,
55            env,
56            filter,
57            self.segment,
58            self.transition_mode.params(),
59        )
60    }
61
62    fn option(&self) -> DatagramOption {
63        <D as DatagramS>::option(&self.inner)
64    }
65}
66
67#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
68pub struct WithSegmentInspectionResult<I, T: TransitionMode> {
69    pub inner: I,
70    pub segment: Segment,
71    pub transition_mode: T,
72}
73
74impl<'a, T, D> Inspectable<'a> for WithSegment<T, D>
75where
76    T: TransitionMode,
77    D: Inspectable<'a> + DatagramS<'a> + HasSegment<T>,
78    <D as DatagramS<'a>>::Error: From<<D as Datagram<'a>>::Error>,
79{
80    type Result = WithSegmentInspectionResult<D::Result, T>;
81
82    fn inspect(
83        self,
84        geometry: &'a Geometry,
85        env: &Environment,
86        filter: &DeviceMask,
87    ) -> Result<InspectionResult<Self::Result>, Self::Error> {
88        Ok(InspectionResult {
89            result: self
90                .inner
91                .inspect(geometry, env, filter)?
92                .result
93                .into_iter()
94                .map(|r| {
95                    r.map(|r| WithSegmentInspectionResult {
96                        inner: r,
97                        segment: self.segment,
98                        transition_mode: self.transition_mode,
99                    })
100                })
101                .collect(),
102        })
103    }
104}