autd3_driver/firmware/operation/
boxed.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use autd3_core::{datagram::Operation, geometry::Device};

use crate::error::AUTDDriverError;

trait DOperation: Send + Sync {
    fn required_size(&self, device: &Device) -> usize;
    fn pack(&mut self, device: &Device, tx: &mut [u8]) -> Result<usize, AUTDDriverError>;
    fn is_done(&self) -> bool;
}

impl<E, O: Operation<Error = E>> DOperation for O
where
    AUTDDriverError: From<E>,
{
    fn required_size(&self, device: &Device) -> usize {
        O::required_size(self, device)
    }

    fn pack(&mut self, device: &Device, tx: &mut [u8]) -> Result<usize, AUTDDriverError> {
        Ok(O::pack(self, device, tx)?)
    }

    fn is_done(&self) -> bool {
        O::is_done(self)
    }
}

#[doc(hidden)]
pub struct BoxedOperation {
    inner: Box<dyn DOperation>,
}

impl BoxedOperation {
    pub fn new<E, O: Operation<Error = E> + 'static>(op: O) -> Self
    where
        AUTDDriverError: From<E>,
    {
        Self {
            inner: Box::new(op),
        }
    }
}

impl Operation for BoxedOperation {
    type Error = AUTDDriverError;

    fn required_size(&self, device: &Device) -> usize {
        self.inner.required_size(device)
    }

    fn pack(&mut self, device: &Device, tx: &mut [u8]) -> Result<usize, Self::Error> {
        self.inner.pack(device, tx)
    }

    fn is_done(&self) -> bool {
        self.inner.is_done()
    }
}