#[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>
impl ByteArray<6>
Sourcepub fn new(v: [u8; 6]) -> Self
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
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>
impl ByteArray<4>
Sourcepub const BROADCAST_LOCAL: IpV4Addr
pub const BROADCAST_LOCAL: IpV4Addr
LAN broadcast address (all ones)
Sourcepub fn new(v: [u8; 4]) -> Self
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
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}Trait Implementations§
Source§impl<const N: usize> ByteStruct for ByteArray<N>
impl<const N: usize> ByteStruct for ByteArray<N>
Source§fn read_bytes(bytes: &[u8]) -> Self
fn read_bytes(bytes: &[u8]) -> Self
Unpacks raw bytes from a slice into a new struct
Source§fn write_bytes(&self, bytes: &mut [u8])
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>
impl<const N: usize> ByteStructLen for ByteArray<N>
Source§impl<const N: usize> Ord for ByteArray<N>
impl<const N: usize> Ord for ByteArray<N>
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl<const N: usize> PartialOrd for ByteArray<N>
impl<const N: usize> PartialOrd for ByteArray<N>
impl<const N: usize> Copy for ByteArray<N>
impl<const N: usize> Eq for ByteArray<N>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> ByteStructUnspecifiedByteOrder for Twhere
T: ByteStruct,
impl<T> ByteStructUnspecifiedByteOrder for Twhere
T: ByteStruct,
Source§fn write_bytes_default_le(&self, bytes: &mut [u8])
fn write_bytes_default_le(&self, bytes: &mut [u8])
A wrapper of ByteStruct::write_bytes
Source§fn read_bytes_default_le(bytes: &[u8]) -> T
fn read_bytes_default_le(bytes: &[u8]) -> T
A wrapper of ByteStruct::read_bytes
Source§fn write_bytes_default_be(&self, bytes: &mut [u8])
fn write_bytes_default_be(&self, bytes: &mut [u8])
A wrapper of ByteStruct::write_bytes
Source§fn read_bytes_default_be(bytes: &[u8]) -> T
fn read_bytes_default_be(bytes: &[u8]) -> T
A wrapper of ByteStruct::read_bytes