Debian Codegraph @

src/virtio_net/mod.rs

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