1 // Copyright 2022 Unikie
2 // SPDX-License-Identifier: BSD-3-Clause OR Apache-2.0
3 
4 mod generated;
5 pub use generated::*;
6 
7 use std::fmt::{Debug, Formatter, Result};
8 use std::mem::{size_of, transmute};
9 
10 impl Debug for virtio_net_hdr_v1 {
fmt(&self, f: &mut Formatter) -> Result11     fn fmt(&self, f: &mut Formatter) -> Result {
12         // SAFETY: As of Linux v6.0, all union fields have compatible types.
13         // This means that it is safe to convert any variant into any other,
14         // as they all have the same size, alignment, and permitted values.
15         // https://doc.rust-lang.org/reference/items/unions.html#reading-and-writing-union-fields
16         let csum = unsafe { self.__bindgen_anon_1.csum };
17 
18         // We forgo determining the correct name of the fields in the
19         // union due to the complexity that would involve.
20         f.debug_struct("virtio_net_hdr_v1")
21             .field("flags", &self.flags)
22             .field("gso_type", &self.gso_type)
23             .field("hdr_len", &self.hdr_len)
24             .field("gso_size", &self.gso_size)
25             .field("csum_start", &csum.start)
26             .field("csum_offset", &csum.offset)
27             .field("num_buffers", &self.num_buffers)
28             .finish()
29     }
30 }
31 
32 impl PartialEq<Self> for virtio_net_hdr_v1 {
eq<'a>(&'a self, other: &'a Self) -> bool33     fn eq<'a>(&'a self, other: &'a Self) -> bool {
34         // SAFETY: The values will be valid byte arrays, and the lifetimes match the
35         // original types.
36         unsafe {
37             let ptr1 = transmute::<&'a Self, &'a [u8; size_of::<Self>()]>(&self);
38             let ptr2 = transmute::<&'a Self, &'a [u8; size_of::<Self>()]>(&other);
39             ptr1 == ptr2
40         }
41     }
42 }
43 
44 #[test]
virtio_net_hdr_v1_default_debug()45 fn virtio_net_hdr_v1_default_debug() {
46     assert_eq!(format!("{:?}", virtio_net_hdr_v1::default()), "virtio_net_hdr_v1 { flags: 0, gso_type: 0, hdr_len: 0, gso_size: 0, csum_start: 0, csum_offset: 0, num_buffers: 0 }");
47 }
48 
49 #[test]
virtio_net_hdr_v1_hex_debug()50 fn virtio_net_hdr_v1_hex_debug() {
51     let expected = "virtio_net_hdr_v1 {
52     flags: 0x0,
53     gso_type: 0x0,
54     hdr_len: 0x0,
55     gso_size: 0x0,
56     csum_start: 0x0,
57     csum_offset: 0x0,
58     num_buffers: 0x0,
59 }";
60     assert_eq!(format!("{:#x?}", virtio_net_hdr_v1::default()), expected);
61 }
62 
63 #[test]
virtio_net_hdr_v1_partial_eq()64 fn virtio_net_hdr_v1_partial_eq() {
65     let hdr1 = virtio_net_hdr_v1::default();
66     let hdr2 = virtio_net_hdr_v1 {
67         flags: 1,
68         ..Default::default()
69     };
70     assert_eq!(hdr1, hdr1);
71     assert_ne!(hdr1, hdr2);
72 }
73