autd3_rs_core/link/
status.rs1use super::DeviceState;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub struct LinkStatus {
5 devices: Vec<DeviceState>,
6 recoveries: u64,
7}
8
9impl LinkStatus {
10 #[must_use]
11 pub fn new(devices: Vec<DeviceState>, recoveries: u64) -> Self {
12 Self {
13 devices,
14 recoveries,
15 }
16 }
17
18 #[must_use]
19 pub fn op(num_devices: usize) -> Self {
20 Self {
21 devices: vec![DeviceState::Op; num_devices],
22 recoveries: 0,
23 }
24 }
25
26 #[must_use]
27 pub fn devices(&self) -> &[DeviceState] {
28 &self.devices
29 }
30
31 #[must_use]
32 pub fn into_devices(self) -> Vec<DeviceState> {
33 self.devices
34 }
35
36 #[must_use]
37 pub fn recoveries(&self) -> u64 {
38 self.recoveries
39 }
40
41 pub fn set_recoveries(&mut self, recoveries: u64) {
42 self.recoveries = recoveries;
43 }
44
45 pub fn set_devices(&mut self, devices: impl IntoIterator<Item = DeviceState>) {
46 self.devices.clear();
47 self.devices.extend(devices);
48 }
49
50 #[must_use]
51 pub fn all_op(&self) -> bool {
52 self.devices.iter().all(|s| *s == DeviceState::Op)
53 }
54
55 #[must_use]
56 pub fn any_lost(&self) -> bool {
57 self.devices.contains(&DeviceState::Lost)
58 }
59}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn link_status_predicates() {
67 let status = LinkStatus::op(2);
68 assert!(status.all_op());
69 assert!(!status.any_lost());
70
71 let status = LinkStatus::new(vec![DeviceState::Op, DeviceState::Lost], 0);
72 assert!(!status.all_op());
73 assert!(status.any_lost());
74 }
75
76 #[test]
77 fn set_devices_reuses_the_buffer() {
78 let mut status = LinkStatus::op(2);
79 status.set_devices([DeviceState::Lost]);
80 status.set_recoveries(3);
81 assert_eq!(status.devices(), [DeviceState::Lost]);
82 assert_eq!(status.recoveries(), 3);
83 }
84}