socketcan_alt/frame/
remote.rs

1use super::Id;
2use crate::sys;
3use std::fmt;
4use std::mem::MaybeUninit;
5
6#[derive(Clone, Copy)]
7pub struct RemoteFrame(pub(super) sys::can_frame);
8
9impl RemoteFrame {
10    /// # Panics
11    ///
12    /// Panics if `id` exceeds its limit or `len` is greater than 8.
13    pub fn new(id: Id, len: u8) -> Self {
14        assert!(len <= sys::CAN_MAX_DLEN as _);
15        let mut inner = MaybeUninit::<sys::can_frame>::zeroed();
16        unsafe {
17            (*inner.as_mut_ptr()).can_id = id.into_can_id();
18            (&mut *inner.as_mut_ptr()).set_len(len as _);
19            Self(inner.assume_init())
20        }
21    }
22
23    pub fn id(&self) -> Id {
24        Id::from_can_id(self.0.can_id)
25    }
26
27    pub fn len(&self) -> u8 {
28        self.0.len()
29    }
30}
31
32impl fmt::Debug for RemoteFrame {
33    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
34        fmt.debug_struct("RemoteFrame")
35            .field("id", &self.id())
36            .field("len", &self.len())
37            .finish()
38    }
39}
40
41#[cfg(test)]
42mod tests;