can_socket/
interface.rs

1/// A CAN interface identified by its index.
2///
3/// This type is used as a socket address to bind CAN sockets to a specific interface.
4#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
5#[repr(transparent)]
6pub struct CanInterface {
7	/// The inner `CanInterface`, directly usable as a socket address for a CAN socket.
8	pub(crate) inner: crate::sys::CanInterface,
9}
10
11impl CanInterface {
12	/// Create a new `CanInterface` from a raw index.
13	///
14	/// Index `0` represents the "bind all" interface.
15	pub fn from_index(index: u32) -> Self {
16		Self {
17			inner: crate::sys::CanInterface::from_index(index),
18		}
19	}
20
21	/// Resolve a CAN interface name to a [`CanInterface`].
22	pub fn from_name(name: &str) -> std::io::Result<Self> {
23		Ok(Self {
24			inner: crate::sys::CanInterface::from_name(name)?,
25		})
26	}
27
28	/// Get the index of the interface.
29	pub fn index(&self) -> u32 {
30		self.inner.index()
31	}
32
33	/// Look up the name of the interface from the index.
34	pub fn get_name(&self) -> std::io::Result<String> {
35		self.inner.get_name()
36	}
37}
38
39impl std::fmt::Debug for CanInterface {
40	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41		let name = self.get_name();
42		let mut debug = f.debug_struct("CanInterface");
43		debug.field("index", &self.index());
44		if let Ok(name) = &name {
45			debug.field("name", name);
46		}
47		debug.finish()
48	}
49}