ByteArray

Struct ByteArray 

Source
#[repr(transparent)]
pub struct ByteArray<const N: usize>(pub [u8; N]);
Expand description

Newtype for [u8; N] in order to be able to implement traits.

Tuple Fields§

§0: [u8; N]

Implementations§

Source§

impl ByteArray<6>

Source

pub const BROADCAST: MacAddr

Broadcast address (all ones)

Source

pub const ANY: MacAddr

Any address (all zeroes)

Source

pub fn new(v: [u8; 6]) -> Self

New from bytes

Examples found in repository?
examples/dhcp.rs (line 7)
2fn main() -> () {
3    use catnip::*;
4
5    let dhcp_inform = DhcpFixedPayload::new_inform(
6        IpV4Addr::new([1, 2, 3, 4]),
7        MacAddr::new([5, 6, 7, 8, 9, 10]),
8        12345
9    );
10
11    // Serialize
12    let bytes = dhcp_inform.to_be_bytes();
13    // Deserialize
14    let msg_parsed = DhcpFixedPayload::read_bytes(&bytes);
15
16    assert_eq!(msg_parsed, dhcp_inform);
17}
More examples
Hide additional examples
examples/arp.rs (line 7)
3fn main() -> () {
4    use catnip::*;
5
6    let msg = ArpPayload::new(
7        MacAddr::new([1, 2, 3, 4, 5, 6]),
8        IpV4Addr::new([7, 8, 9, 10]),
9        MacAddr::new([11, 12, 13, 14, 15, 16]),
10        IpV4Addr::new([17, 18, 19, 20]),
11        ArpOperation::Request,
12    );
13
14    // Serialize
15    let bytes: [u8; ArpPayload::BYTE_LEN] = msg.to_be_bytes();
16
17    // Deserialize
18    let msg_parsed = ArpPayload::read_bytes(&bytes);
19
20    assert_eq!(msg, msg_parsed);
21}
examples/udp.rs (line 18)
5fn main() -> () {
6    use catnip::*;
7    
8    // Some made-up data with two 32-bit words' worth of bytes
9    let data: ByteArray<8> = ByteArray([0, 1, 2, 3, 4, 5, 6, 7]);
10
11    // Arbitrary addresses
12    let src_ipaddr: IpV4Addr = IpV4Addr::new([10, 0, 0, 120]);
13    let dst_ipaddr: IpV4Addr = IpV4Addr::new([10, 0, 0, 121]);
14
15    let frame = EthernetFrame::<IpV4Frame<UdpFrame<ByteArray<8>>>> {
16        header: EthernetHeader {
17            dst_macaddr: MacAddr::BROADCAST,
18            src_macaddr: MacAddr::new([0x02, 0xAF, 0xFF, 0x1A, 0xE5, 0x3C]),
19            ethertype: EtherType::IpV4,
20        },
21        data: IpV4Frame::<UdpFrame<ByteArray<8>>> {
22            header: IpV4Header {
23                version_and_header_length: VersionAndHeaderLength::new().with_version(4).with_header_length((IpV4Header::BYTE_LEN / 4) as u8),
24                dscp: DSCP::Standard,
25                total_length: IpV4Frame::<UdpFrame<ByteArray<8>>>::BYTE_LEN as u16,
26                identification: 0,
27                fragmentation: Fragmentation::default(),
28                time_to_live: 10,
29                protocol: Protocol::Udp,
30                checksum: 0,
31                src_ipaddr: src_ipaddr,
32                dst_ipaddr: dst_ipaddr,
33            },
34            data: UdpFrame::<ByteArray<8>> {
35                header: UdpHeader {
36                    src_port: 8123,
37                    dst_port: 8125,
38                    length: UdpFrame::<ByteArray<8>>::BYTE_LEN as u16,
39                    checksum: 0,
40                },
41                data: data,
42            },
43        },
44        checksum: 0_u32,
45    };
46
47    let bytes = frame.to_be_bytes();
48    let frame_parsed = EthernetFrame::<IpV4Frame<UdpFrame<ByteArray<8>>>>::read_bytes(&bytes);
49
50    assert_eq!(frame_parsed, frame);
51}
Source§

impl ByteArray<4>

Source

pub const BROADCAST: IpV4Addr

Broadcast address (all ones)

Source

pub const BROADCAST_LOCAL: IpV4Addr

LAN broadcast address (all ones)

Source

pub const ANY: IpV4Addr

Any address (all zeroes)

Source

pub fn new(v: [u8; 4]) -> Self

New from bytes

Examples found in repository?
examples/dhcp.rs (line 6)
2fn main() -> () {
3    use catnip::*;
4
5    let dhcp_inform = DhcpFixedPayload::new_inform(
6        IpV4Addr::new([1, 2, 3, 4]),
7        MacAddr::new([5, 6, 7, 8, 9, 10]),
8        12345
9    );
10
11    // Serialize
12    let bytes = dhcp_inform.to_be_bytes();
13    // Deserialize
14    let msg_parsed = DhcpFixedPayload::read_bytes(&bytes);
15
16    assert_eq!(msg_parsed, dhcp_inform);
17}
More examples
Hide additional examples
examples/arp.rs (line 8)
3fn main() -> () {
4    use catnip::*;
5
6    let msg = ArpPayload::new(
7        MacAddr::new([1, 2, 3, 4, 5, 6]),
8        IpV4Addr::new([7, 8, 9, 10]),
9        MacAddr::new([11, 12, 13, 14, 15, 16]),
10        IpV4Addr::new([17, 18, 19, 20]),
11        ArpOperation::Request,
12    );
13
14    // Serialize
15    let bytes: [u8; ArpPayload::BYTE_LEN] = msg.to_be_bytes();
16
17    // Deserialize
18    let msg_parsed = ArpPayload::read_bytes(&bytes);
19
20    assert_eq!(msg, msg_parsed);
21}
examples/udp.rs (line 12)
5fn main() -> () {
6    use catnip::*;
7    
8    // Some made-up data with two 32-bit words' worth of bytes
9    let data: ByteArray<8> = ByteArray([0, 1, 2, 3, 4, 5, 6, 7]);
10
11    // Arbitrary addresses
12    let src_ipaddr: IpV4Addr = IpV4Addr::new([10, 0, 0, 120]);
13    let dst_ipaddr: IpV4Addr = IpV4Addr::new([10, 0, 0, 121]);
14
15    let frame = EthernetFrame::<IpV4Frame<UdpFrame<ByteArray<8>>>> {
16        header: EthernetHeader {
17            dst_macaddr: MacAddr::BROADCAST,
18            src_macaddr: MacAddr::new([0x02, 0xAF, 0xFF, 0x1A, 0xE5, 0x3C]),
19            ethertype: EtherType::IpV4,
20        },
21        data: IpV4Frame::<UdpFrame<ByteArray<8>>> {
22            header: IpV4Header {
23                version_and_header_length: VersionAndHeaderLength::new().with_version(4).with_header_length((IpV4Header::BYTE_LEN / 4) as u8),
24                dscp: DSCP::Standard,
25                total_length: IpV4Frame::<UdpFrame<ByteArray<8>>>::BYTE_LEN as u16,
26                identification: 0,
27                fragmentation: Fragmentation::default(),
28                time_to_live: 10,
29                protocol: Protocol::Udp,
30                checksum: 0,
31                src_ipaddr: src_ipaddr,
32                dst_ipaddr: dst_ipaddr,
33            },
34            data: UdpFrame::<ByteArray<8>> {
35                header: UdpHeader {
36                    src_port: 8123,
37                    dst_port: 8125,
38                    length: UdpFrame::<ByteArray<8>>::BYTE_LEN as u16,
39                    checksum: 0,
40                },
41                data: data,
42            },
43        },
44        checksum: 0_u32,
45    };
46
47    let bytes = frame.to_be_bytes();
48    let frame_parsed = EthernetFrame::<IpV4Frame<UdpFrame<ByteArray<8>>>>::read_bytes(&bytes);
49
50    assert_eq!(frame_parsed, frame);
51}
Source§

impl<const N: usize> ByteArray<N>

Source

pub fn to_be_bytes(&self) -> [u8; N]

Convert to big-endian byte array

Trait Implementations§

Source§

impl<const N: usize> ByteStruct for ByteArray<N>

Source§

fn read_bytes(bytes: &[u8]) -> Self

Unpacks raw bytes from a slice into a new struct
Source§

fn write_bytes(&self, bytes: &mut [u8])

Packs the struct into raw bytes and write to a slice
Source§

impl<const N: usize> ByteStructLen for ByteArray<N>

Source§

const BYTE_LEN: usize = N

The length of the packed bytes of this type
Source§

impl<const N: usize> Clone for ByteArray<N>

Source§

fn clone(&self) -> ByteArray<N>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const N: usize> Debug for ByteArray<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const N: usize> Ord for ByteArray<N>

Source§

fn cmp(&self, other: &ByteArray<N>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<const N: usize> PartialEq for ByteArray<N>

Source§

fn eq(&self, other: &ByteArray<N>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const N: usize> PartialOrd for ByteArray<N>

Source§

fn partial_cmp(&self, other: &ByteArray<N>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl uDebug for ByteArray<4>

Source§

fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
where W: uWrite + ?Sized,

Formats the value using the given formatter
Source§

impl uDebug for ByteArray<6>

Source§

fn fmt<W>(&self, f: &mut Formatter<'_, W>) -> Result<(), W::Error>
where W: uWrite + ?Sized,

Formats the value using the given formatter
Source§

impl<const N: usize> Copy for ByteArray<N>

Source§

impl<const N: usize> Eq for ByteArray<N>

Source§

impl<const N: usize> StructuralPartialEq for ByteArray<N>

Auto Trait Implementations§

§

impl<const N: usize> Freeze for ByteArray<N>

§

impl<const N: usize> RefUnwindSafe for ByteArray<N>

§

impl<const N: usize> Send for ByteArray<N>

§

impl<const N: usize> Sync for ByteArray<N>

§

impl<const N: usize> Unpin for ByteArray<N>

§

impl<const N: usize> UnwindSafe for ByteArray<N>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ByteStructUnspecifiedByteOrder for T
where T: ByteStruct,

Source§

fn write_bytes_default_le(&self, bytes: &mut [u8])

Source§

fn read_bytes_default_le(bytes: &[u8]) -> T

Source§

fn write_bytes_default_be(&self, bytes: &mut [u8])

Source§

fn read_bytes_default_be(bytes: &[u8]) -> T

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.