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
59
60
61
62
63
64
65
66
67
use crate::derive::{AUTDInternalError, Geometry};

use super::Datagram;

use derive_more::Deref;

#[derive(Clone, Deref)]
pub struct DatagramWithParallelThreshold<D: Datagram> {
    #[deref]
    datagram: D,
    threshold: usize,
}

impl<D: Datagram> Datagram for DatagramWithParallelThreshold<D> {
    type O1 = D::O1;
    type O2 = D::O2;
    type G = D::G;

    fn operation_generator(self, geometry: &Geometry) -> Result<Self::G, AUTDInternalError> {
        self.datagram.operation_generator(geometry)
    }

    fn timeout(&self) -> Option<std::time::Duration> {
        self.datagram.timeout()
    }

    fn parallel_threshold(&self) -> Option<usize> {
        Some(self.threshold)
    }
}

pub trait IntoDatagramWithParallelThreshold<D: Datagram> {
    fn with_paralle_threshold(self, threshold: usize) -> DatagramWithParallelThreshold<D>;
}

impl<D: Datagram> IntoDatagramWithParallelThreshold<D> for D {
    fn with_paralle_threshold(self, threshold: usize) -> DatagramWithParallelThreshold<D> {
        DatagramWithParallelThreshold {
            datagram: self,
            threshold,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::{
        datagram::tests::{NullDatagram, NullOperationGenerator},
        defined::FREQ_40K,
        geometry::tests::create_geometry,
    };

    #[test]
    fn with_parallel_threshold() {
        let geometry = create_geometry(1, 249, FREQ_40K);
        let datagram = NullDatagram {
            timeout: Some(std::time::Duration::from_secs(1)),
            parallel_threshold: None,
        }
        .with_paralle_threshold(100);
        assert_eq!(datagram.timeout(), Some(std::time::Duration::from_secs(1)));
        assert_eq!(datagram.parallel_threshold(), Some(100));
        let _: Result<NullOperationGenerator, _> = datagram.operation_generator(&geometry);
    }
}