autd3_driver/datagram/
with_segment.rs

1use autd3_core::{
2    datagram::{Datagram, DatagramOption, DatagramS, InspectionResult, Segment, TransitionMode},
3    derive::{Geometry, Inspectable},
4};
5
6use derive_more::Deref;
7
8/// A wrapper of [`DatagramS`] to specify the segment to write the data.
9#[derive(Deref, Debug, Clone, Copy, PartialEq, Eq, Hash)]
10pub struct WithSegment<D: DatagramS> {
11    #[deref]
12    /// The original [`DatagramS`]
13    pub inner: D,
14    /// The segment to write the data
15    pub segment: Segment,
16    /// The behavior when switching segments
17    pub transition_mode: Option<TransitionMode>,
18}
19
20impl<D: DatagramS> WithSegment<D> {
21    /// Create a new [`WithSegment`].
22    #[must_use]
23    pub const fn new(inner: D, segment: Segment, transition_mode: Option<TransitionMode>) -> Self {
24        Self {
25            inner,
26            segment,
27            transition_mode,
28        }
29    }
30}
31
32impl<D: DatagramS> Datagram for WithSegment<D> {
33    type G = D::G;
34    type Error = D::Error;
35
36    fn operation_generator(
37        self,
38        geometry: &mut autd3_core::derive::Geometry,
39    ) -> Result<Self::G, Self::Error> {
40        <D as DatagramS>::operation_generator_with_segment(
41            self.inner,
42            geometry,
43            self.segment,
44            self.transition_mode,
45        )
46    }
47
48    fn option(&self) -> DatagramOption {
49        <D as DatagramS>::option(&self.inner)
50    }
51}
52
53#[doc(hidden)]
54pub trait InspectionResultWithSegment {
55    fn with_segment(self, segment: Segment, transition_mode: Option<TransitionMode>) -> Self;
56}
57
58impl<D> Inspectable for WithSegment<D>
59where
60    D: Inspectable + DatagramS,
61    D::Result: InspectionResultWithSegment,
62    <D as DatagramS>::Error: From<<D as Datagram>::Error>,
63{
64    type Result = D::Result;
65
66    fn inspect(
67        self,
68        geometry: &mut Geometry,
69    ) -> Result<InspectionResult<Self::Result>, Self::Error> {
70        Ok(self
71            .inner
72            .inspect(geometry)?
73            .modify(|t| t.with_segment(self.segment, self.transition_mode)))
74    }
75}