thormotion/traits/thorlabs_device.rs
1/*
2Project: thormotion
3GitHub: https://github.com/MillieFD/thormotion
4
5BSD 3-Clause License, Copyright (c) 2025, Amelia Fraser-Dale
6
7Redistribution and use in source and binary forms, with or without
8modification, are permitted provided that the conditions of the LICENSE are met.
9*/
10
11use std::fmt::{Debug, Display};
12use std::hash::{Hash, Hasher};
13
14use crate::devices::UsbPrimitive;
15
16pub trait ThorlabsDevice<const CH: usize>: Debug + Display + Send + Sync {
17 /// Returns a borrow that dereferences to the inner [`UsbPrimitive`]
18 fn inner(&self) -> &UsbPrimitive<CH>;
19
20 /// Returns the serial number of the device as a `&str`.
21 fn serial_number(&self) -> &str {
22 self.inner().serial_number()
23 }
24
25 /// Safely brings the [`USB Device`][1] to a resting state and releases the claimed
26 /// [`Interface`][2].
27 ///
28 /// If the device [`Status`][3] is [`Closed`][4], a temporary [`Interface`][2] is [`Opened`][5]
29 /// to send the abort command.
30 ///
31 /// Does not remove the device from the global [`DEVICES`][6] [`HashMap`][7]. You can use
32 /// [`Open`][5] to resume communication.
33 ///
34 /// To release the claimed [`Interface`][2] without bringing the device to a resting state,
35 /// use `close`.
36 ///
37 /// [1]: UsbPrimitive
38 /// [2]: nusb::Interface
39 /// [3]: crate::devices::usb_primitive::status::Status
40 /// [4]: crate::devices::usb_primitive::status::Status::Closed
41 /// [5]: UsbPrimitive::open
42 /// [6]: crate::devices::utils::DEVICES
43 /// [7]: ahash::HashMap
44 fn abort(&self);
45}
46
47impl<const CH: usize> Hash for dyn ThorlabsDevice<CH> {
48 fn hash<H: Hasher>(&self, state: &mut H) {
49 self.inner().serial_number().hash(state);
50 }
51}
52
53impl<const CH: usize> PartialEq for dyn ThorlabsDevice<CH> {
54 fn eq(&self, other: &Self) -> bool {
55 self.inner().serial_number() == other.inner().serial_number()
56 }
57}
58
59impl<const CH: usize> Eq for dyn ThorlabsDevice<CH> {}