Skip to content

Commit 3e20693

Browse files
authored
Merge pull request #94 from phip1611/hash-impl-tag-type-tests-std
multiboot2 v0.12.2 (std in tests; hash for TagType)
2 parents b73db63 + 411b758 commit 3e20693

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

Diff for: multiboot2/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Multiboot2-compliant bootloaders, like GRUB. It supports all tags from the speci
66
including full support for the sections of ELF-64. This library is `no_std` and can be
77
used in a Multiboot2-kernel.
88
"""
9-
version = "0.12.1"
9+
version = "0.12.2"
1010
authors = [
1111
"Philipp Oppermann <[email protected]>",
1212
"Calvin Lee <[email protected]>",

Diff for: multiboot2/Changelog.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# CHANGELOG for crate `multiboot2`
22

3-
## TODO 0.12.2 / 0.13
3+
## 0.12.2
4+
- `TagType` now implements `Eq` and `Hash`
45
- internal improvements
6+
- `std` can be used in tests; the crate is still `no_std`
7+
- this implies that `cargo test` doesn't work on "non-standard" targets
8+
- CI (Ubuntu) still works.
59
- code formatting/style
610
- sensible style checks as optional CI job
711
- `.editorconfig` file

Diff for: multiboot2/src/header.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::fmt::{Debug, Formatter};
2+
use core::hash::{Hash, Hasher};
23
use core::marker::PhantomData;
34

45
/// Magic number that a multiboot2-compliant boot loader will store in `eax` register
@@ -14,7 +15,7 @@ pub const MULTIBOOT2_BOOTLOADER_MAGIC: u32 = 0x36d76289;
1415
/// of the the `typ` property. The names and values are taken from the example C code
1516
/// at the bottom of the Multiboot2 specification.
1617
#[repr(u32)]
17-
#[derive(Copy, Clone, Debug)]
18+
#[derive(Copy, Clone, Debug, Eq)]
1819
pub enum TagType {
1920
/// Marks the end of the tags.
2021
End = 0,
@@ -115,6 +116,13 @@ impl PartialEq<TagType> for TagType {
115116
}
116117
}
117118

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+
118126
/// All tags that could passed via the Multiboot2 information structure to a payload/program/kernel.
119127
/// Better not confuse this with the Multiboot2 header tags. They are something different.
120128
#[derive(Clone, Copy)]
@@ -171,3 +179,20 @@ impl<'a> Iterator for TagIter<'a> {
171179
}
172180
}
173181
}
182+
183+
#[cfg(test)]
184+
mod tests {
185+
use super::*;
186+
187+
#[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);
197+
}
198+
}

Diff for: multiboot2/src/lib.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#![no_std]
1+
// this crate can use `std` in tests only
2+
#![cfg_attr(not(test), no_std)]
23
#![deny(missing_debug_implementations)]
34
// --- BEGIN STYLE CHECKS ---
45
// These checks are optional in CI for PRs, as discussed in
@@ -30,6 +31,11 @@
3031
//! }
3132
//! ```
3233
34+
// this crate can use std in tests only
35+
#[cfg_attr(test, macro_use)]
36+
#[cfg(test)]
37+
extern crate std;
38+
3339
use core::fmt;
3440

3541
pub use boot_loader_name::BootLoaderNameTag;

0 commit comments

Comments
 (0)