|
| 1 | +//! Module for [`TagType`]. |
| 2 | +
|
1 | 3 | use core::fmt::{Debug, Formatter};
|
2 |
| -use core::hash::{Hash, Hasher}; |
| 4 | +use core::hash::Hash; |
3 | 5 | use core::marker::PhantomData;
|
4 | 6 |
|
5 |
| -/// Magic number that a multiboot2-compliant boot loader will store in `eax` register |
6 |
| -/// right before handoff to the payload (the kernel). This value can be used to check, |
7 |
| -/// that the kernel was indeed booted via multiboot2. |
8 |
| -/// |
9 |
| -/// Caution: You might need some assembly code (e.g. GAS or NASM) first, which |
10 |
| -/// moves `eax` to another register, like `edi`. Otherwise it probably happens, |
11 |
| -/// that the Rust compiler output changes `eax` before you can access it. |
12 |
| -pub const MULTIBOOT2_BOOTLOADER_MAGIC: u32 = 0x36d76289; |
13 |
| - |
14 | 7 | /// Possible types of a Tag in the Multiboot2 Information Structure (MBI), therefore the value
|
15 | 8 | /// of the the `typ` property. The names and values are taken from the example C code
|
16 | 9 | /// at the bottom of the Multiboot2 specification.
|
17 | 10 | #[repr(u32)]
|
18 |
| -#[derive(Copy, Clone, Debug, Eq)] |
| 11 | +#[derive(Copy, Clone, Debug, Eq, Ord, PartialOrd, PartialEq, Hash)] |
19 | 12 | pub enum TagType {
|
20 | 13 | /// Marks the end of the tags.
|
21 | 14 | End = 0,
|
@@ -110,19 +103,6 @@ impl PartialEq<TagType> for u32 {
|
110 | 103 | }
|
111 | 104 | }
|
112 | 105 |
|
113 |
| -impl PartialEq<TagType> for TagType { |
114 |
| - fn eq(&self, other: &TagType) -> bool { |
115 |
| - *self as u32 == *other as u32 |
116 |
| - } |
117 |
| -} |
118 |
| - |
119 |
| -// impl required because this type is used in a hashmap in `multiboot2-header` |
120 |
| -impl Hash for TagType { |
121 |
| - fn hash<H: Hasher>(&self, state: &mut H) { |
122 |
| - state.write_u32(*self as u32); |
123 |
| - } |
124 |
| -} |
125 |
| - |
126 | 106 | /// All tags that could passed via the Multiboot2 information structure to a payload/program/kernel.
|
127 | 107 | /// Better not confuse this with the Multiboot2 header tags. They are something different.
|
128 | 108 | #[derive(Clone, Copy)]
|
@@ -185,14 +165,36 @@ mod tests {
|
185 | 165 | use super::*;
|
186 | 166 |
|
187 | 167 | #[test]
|
188 |
| - fn test_hash() { |
189 |
| - let mut hashset = std::collections::HashSet::new(); |
190 |
| - hashset.insert(TagType::Cmdline); |
191 |
| - hashset.insert(TagType::ElfSections); |
192 |
| - hashset.insert(TagType::BootLoaderName); |
193 |
| - hashset.insert(TagType::LoadBaseAddr); |
194 |
| - hashset.insert(TagType::LoadBaseAddr); |
195 |
| - assert_eq!(hashset.len(), 4); |
196 |
| - println!("{:#?}", hashset); |
| 168 | + fn test_hashset() { |
| 169 | + let mut set = std::collections::HashSet::new(); |
| 170 | + set.insert(TagType::Cmdline); |
| 171 | + set.insert(TagType::ElfSections); |
| 172 | + set.insert(TagType::BootLoaderName); |
| 173 | + set.insert(TagType::LoadBaseAddr); |
| 174 | + set.insert(TagType::LoadBaseAddr); |
| 175 | + assert_eq!(set.len(), 4); |
| 176 | + println!("{:#?}", set); |
| 177 | + } |
| 178 | + |
| 179 | + #[test] |
| 180 | + fn test_btreeset() { |
| 181 | + let mut set = std::collections::BTreeSet::new(); |
| 182 | + set.insert(TagType::Cmdline); |
| 183 | + set.insert(TagType::ElfSections); |
| 184 | + set.insert(TagType::BootLoaderName); |
| 185 | + set.insert(TagType::LoadBaseAddr); |
| 186 | + set.insert(TagType::LoadBaseAddr); |
| 187 | + assert_eq!(set.len(), 4); |
| 188 | + for (current, next) in set.iter().zip(set.iter().skip(1)) { |
| 189 | + assert!(current < next); |
| 190 | + } |
| 191 | + println!("{:#?}", set); |
| 192 | + } |
| 193 | + |
| 194 | + /// Tests for equality when one type is u32 and the other the enum representation. |
| 195 | + #[test] |
| 196 | + fn test_partial_eq_u32() { |
| 197 | + assert_eq!(21, TagType::LoadBaseAddr); |
| 198 | + assert_eq!(TagType::LoadBaseAddr, 21); |
197 | 199 | }
|
198 | 200 | }
|
0 commit comments