Parcourir la source

Merge pull request #94 from phip1611/hash-impl-tag-type-tests-std

multiboot2 v0.12.2 (std in tests; hash for TagType)
Philipp Schuster il y a 3 ans
Parent
commit
3e20693144
4 fichiers modifiés avec 39 ajouts et 4 suppressions
  1. 1 1
      multiboot2/Cargo.toml
  2. 5 1
      multiboot2/Changelog.md
  3. 26 1
      multiboot2/src/header.rs
  4. 7 1
      multiboot2/src/lib.rs

+ 1 - 1
multiboot2/Cargo.toml

@@ -6,7 +6,7 @@ Multiboot2-compliant bootloaders, like GRUB. It supports all tags from the speci
 including full support for the sections of ELF-64. This library is `no_std` and can be
 used in a Multiboot2-kernel.
 """
-version = "0.12.1"
+version = "0.12.2"
 authors = [
     "Philipp Oppermann <dev@phil-opp.com>",
     "Calvin Lee <cyrus296@gmail.com>",

+ 5 - 1
multiboot2/Changelog.md

@@ -1,7 +1,11 @@
 # CHANGELOG for crate `multiboot2`
 
-## TODO 0.12.2 / 0.13
+## 0.12.2
+- `TagType` now implements `Eq` and `Hash`
 - internal improvements
+  - `std` can be used in tests; the crate is still `no_std`
+    - this implies that `cargo test` doesn't work on "non-standard" targets
+    - CI (Ubuntu) still works.
   - code formatting/style
   - sensible style checks as optional CI job
   - `.editorconfig` file

+ 26 - 1
multiboot2/src/header.rs

@@ -1,4 +1,5 @@
 use core::fmt::{Debug, Formatter};
+use core::hash::{Hash, Hasher};
 use core::marker::PhantomData;
 
 /// Magic number that a multiboot2-compliant boot loader will store in `eax` register
@@ -14,7 +15,7 @@ pub const MULTIBOOT2_BOOTLOADER_MAGIC: u32 = 0x36d76289;
 /// of the the `typ` property. The names and values are taken from the example C code
 /// at the bottom of the Multiboot2 specification.
 #[repr(u32)]
-#[derive(Copy, Clone, Debug)]
+#[derive(Copy, Clone, Debug, Eq)]
 pub enum TagType {
     /// Marks the end of the tags.
     End = 0,
@@ -115,6 +116,13 @@ impl PartialEq<TagType> for TagType {
     }
 }
 
+// impl required because this type is used in a hashmap in `multiboot2-header`
+impl Hash for TagType {
+    fn hash<H: Hasher>(&self, state: &mut H) {
+        state.write_u32(*self as u32);
+    }
+}
+
 /// All tags that could passed via the Multiboot2 information structure to a payload/program/kernel.
 /// Better not confuse this with the Multiboot2 header tags. They are something different.
 #[derive(Clone, Copy)]
@@ -171,3 +179,20 @@ impl<'a> Iterator for TagIter<'a> {
         }
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_hash() {
+        let mut hashset = std::collections::HashSet::new();
+        hashset.insert(TagType::Cmdline);
+        hashset.insert(TagType::ElfSections);
+        hashset.insert(TagType::BootLoaderName);
+        hashset.insert(TagType::LoadBaseAddr);
+        hashset.insert(TagType::LoadBaseAddr);
+        assert_eq!(hashset.len(), 4);
+        println!("{:#?}", hashset);
+    }
+}

+ 7 - 1
multiboot2/src/lib.rs

@@ -1,4 +1,5 @@
-#![no_std]
+// this crate can use `std` in tests only
+#![cfg_attr(not(test), no_std)]
 #![deny(missing_debug_implementations)]
 // --- BEGIN STYLE CHECKS ---
 // These checks are optional in CI for PRs, as discussed in
@@ -30,6 +31,11 @@
 //! }
 //! ```
 
+// this crate can use std in tests only
+#[cfg_attr(test, macro_use)]
+#[cfg(test)]
+extern crate std;
+
 use core::fmt;
 
 pub use boot_loader_name::BootLoaderNameTag;