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::{FirmwareLimits, 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        limits: &FirmwareLimits,
52    ) -> Result<Self::G, Self::Error> {
53        <D as DatagramS>::operation_generator_with_segment(
54            self.inner,
55            geometry,
56            env,
57            filter,
58            limits,
59            self.segment,
60            self.transition_mode.params(),
61        )
62    }
63
64    fn option(&self) -> DatagramOption {
65        <D as DatagramS>::option(&self.inner)
66    }
67}
68
69#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
70pub struct WithSegmentInspectionResult<I, T: TransitionMode> {
71    pub inner: I,
72    pub segment: Segment,
73    pub transition_mode: T,
74}
75
76impl<'a, T, D> Inspectable<'a> for WithSegment<T, D>
77where
78    T: TransitionMode,
79    D: Inspectable<'a> + DatagramS<'a> + HasSegment<T>,
80    <D as DatagramS<'a>>::Error: From<<D as Datagram<'a>>::Error>,
81{
82    type Result = WithSegmentInspectionResult<D::Result, T>;
83
84    fn inspect(
85        self,
86        geometry: &'a Geometry,
87        env: &Environment,
88        filter: &DeviceMask,
89        limits: &FirmwareLimits,
90    ) -> Result<InspectionResult<Self::Result>, Self::Error> {
91        Ok(InspectionResult {
92            result: self
93                .inner
94                .inspect(geometry, env, filter, limits)?
95                .result
96                .into_iter()
97                .map(|r| {
98                    r.map(|r| WithSegmentInspectionResult {
99                        inner: r,
100                        segment: self.segment,
101                        transition_mode: self.transition_mode,
102                    })
103                })
104                .collect(),
105        })
106    }
107}