### Install Rust Toolchain Components Source: https://github.com/hawkw/mycelium/blob/main/CONTRIBUTING.md Manually installs the 'rust-src' and 'llvm-tools-preview' Rust toolchain components required for building Mycelium. ```shell rustup component add rust-src llvm-tools-preview ``` -------------------------------- ### Interacting with Bitfield Types Source: https://github.com/hawkw/mycelium/blob/main/bitfield/README.md Illustrates common operations on bitfield types defined with the bitfield! macro, including construction from raw bits, getting field values, and packing/setting new values. ```rust // Bitfield types can be cheaply constructed from a raw numeric // representation: let bitfield = MyBitfield::from_bits(0b10100_0011_0101); // `get` methods can be used to unpack fields from a bitfield type: assert_eq!(bitfield.get(MyBitfield::HELLO), 0b11_0101); assert_eq!(bitfield.get(MyBitfield::WORLD), 0b0101); // `with` methods can be used to pack bits into a bitfield type by // value: let bitfield2 = MyBitfield::new() .with(MyBitfield::HELLO, 0b11_0101) .with(MyBitfield::WORLD, 0b0101); assert_eq!(bitfield, bitfield2); // `set` methods can be used to mutate a bitfield type in place: let mut bitfield3 = MyBitfield::new(); bitfield3 .set(MyBitfield::HELLO, 0b011_0101) .set(MyBitfield::WORLD, 0b0101); assert_eq!(bitfield, bitfield3); ``` -------------------------------- ### Accessing Typed Bitfield Values with get() Source: https://github.com/hawkw/mycelium/blob/main/bitfield/README.md Demonstrates how to extract values from a bitfield using the `get` method. This method returns the typed value or panics if the bit pattern is invalid. ```rust // Unpacking a typed value with `get` will return that value, or panic if // the bit pattern is invalid: let my_bitfield = TypedBitfield::from_bits(0b0010_0100_0011_0101_1001_1110); assert_eq!(my_bitfield.get(TypedBitfield::ENUM_VALUE), MyEnum::Baz); assert_eq!(my_bitfield.get(TypedBitfield::FLAG_1), true); assert_eq!(my_bitfield.get(TypedBitfield::FLAG_2), false); assert_eq!(my_bitfield.get(TypedBitfield::OTHER_ENUM), MyGeneratedEnum::Wow); ``` -------------------------------- ### Inoculate Build Tool Usage Source: https://github.com/hawkw/mycelium/blob/main/CONTRIBUTING.md Displays the help information for the 'inoculate' build tool, detailing its usage, flags, options, and subcommands. ```text inoculate 0.1.0 the horrible mycelium build tool (because that's a thing we have to have now apparently!) USAGE: inoculate [OPTIONS] [SUBCOMMAND] FLAGS: -h, --help Prints help information -V, --version Prints version information OPTIONS: --bootloader-manifest The path to the `bootloader` crate's Cargo manifest. If this is not provided, it will be located automatically --color Whether to emit colors in output [env: CARGO_TERM_COLORS=] [default: auto] [possible values: auto, always, never] --kernel-manifest The path to the kernel's Cargo manifest. If this is not provided, it will be located automatically -l, --log Configures build logging [env: RUST_LOG=] [default: warn] -o, --out-dir Overrides the directory in which to build the output image -t, --target-dir Overrides the target directory for the kernel build ARGS: The path to the kernel binary SUBCOMMANDS: help Prints this message or the help of the given subcommand(s) run Builds a bootable disk image and runs it in QEMU (implies: `build`) test Builds a bootable disk image with tests enabled, and runs the tests in QEMU ``` -------------------------------- ### Run Mycelium Kernel with Inoculate Source: https://github.com/hawkw/mycelium/blob/main/README.md Use the 'inoculate' build tool to launch the Mycelium kernel in the QEMU emulator. Ensure you have the development environment set up as per CONTRIBUTING.md. ```console cargo run-x64 ``` -------------------------------- ### Defining and Using Packing Specs with Pack64 Source: https://github.com/hawkw/mycelium/blob/main/bitfield/README.md Demonstrates how to define packing specifications for different bit ranges within a u64 value and how to pack and unpack data using these specs. Useful for granular bit manipulation. ```rust use mycelium_bitfield::Pack64; // Defines a packing spec for the least-significant 12 bits of a 64-bit value. const LOW: Pack64 = Pack64::least_significant(12); // Defines a packing spec for the next 8 more-significant bits after `LOW`. const MID: Pack64 = LOW.next(8); // Defines a packing spec for the next 4 more-significant bits after `MID`. const HIGH: Pack64 = MID.next(4); // Wrap an integer value to pack it using method calls. let coffee = Pack64::pack_in(0) // pack the 12 bits of `0xfee` at the range specified by `LOW`. .pack(0xfee, &LOW) // pack the 4 bits `0xc` at the range specified by `HIGH`. .pack(0xc, &HIGH) // pack `0xf` in the 8 bits specified by `MID`. .pack(0xf, &MID) // unwrap the packing value back into a `u64`. .bits(); assert_eq!(coffee, 0xc0ffee); // i want c0ffee ``` -------------------------------- ### Automatic FromBits Implementation with enum_from_bits! Source: https://github.com/hawkw/mycelium/blob/main/bitfield/README.md Shows how to use the enum_from_bits! macro to automatically generate a FromBits implementation for an enum. This simplifies the process of making enums usable in bitfields. ```rust enum_from_bits! { #[derive(Debug, Eq, PartialEq)] pub enum MyGeneratedEnum { /// Isn't this cool? Wow = 0b1001, /// It sure is! :D Whoa = 0b0110, } } ``` -------------------------------- ### Manual FromBits Implementation for Enum Source: https://github.com/hawkw/mycelium/blob/main/bitfield/README.md Demonstrates how to manually implement the FromBits trait for an enum with a repr attribute. This allows the enum to be used as a typed subfield within a bitfield. ```rust use mycelium_bitfield::{bitfield, enum_from_bits, FromBits}; // An enum type can implement the `FromBits` trait if it has a // `#[repr(uN)]` attribute. #[repr(u8)] #[derive(Debug, Eq, PartialEq)] enum MyEnum { Foo = 0b00, Bar = 0b01, Baz = 0b10, } impl FromBits for MyEnum { // Two bits can represent all possible `MyEnum` values. const BITS: u32 = 2; type Error = &'static str; fn try_from_bits(bits: u32) -> Result { match bits as u8 { bits if bits == Self::Foo as u8 => Ok(Self::Foo), bits if bits == Self::Bar as u8 => Ok(Self::Bar), bits if bits == Self::Baz as u8 => Ok(Self::Baz), _ => Err("expected one of 0b00, 0b01, or 0b10"), } } fn into_bits(self) -> u32 { self as u8 as u32 } } ``` -------------------------------- ### Defining a Structured Bitfield with bitfield! Macro Source: https://github.com/hawkw/mycelium/blob/main/bitfield/README.md Shows how to declare a custom bitfield type with named fields, doc comments, and attributes using the bitfield! macro. This simplifies the creation of complex bit structures. ```rust mycelium_bitfield::bitfield! { /// Bitfield types can have doc comments. #[derive(Eq, PartialEq)] // ...and attributes pub struct MyBitfield { /// Generates a packing spec named `HELLO` for the first 6 /// least-significant bits. pub const HELLO = 6; // Fields with names starting with `_` can be used to mark bits as // reserved. const _RESERVED = 4; /// Generates a packing spec named `WORLD` for the next 3 bits. pub const WORLD = 3; /// A boolean value will generate a packing spec for a single bit. pub const FLAG: bool; } } ``` -------------------------------- ### Safely Accessing Bitfield Values with try_get() Source: https://github.com/hawkw/mycelium/blob/main/bitfield/README.md Shows how to use the `try_get` method for safely accessing bitfield values. This method returns a Result, allowing error handling for invalid bit patterns instead of panicking. ```rust // The `try_get` method will return an error rather than panicking if an // invalid bit pattern is encountered: let invalid = TypedBitfield::from_bits(0b0011); // There is no `MyEnum` variant for 0b11. assert!(invalid.try_get(TypedBitfield::ENUM_VALUE).is_err()); ``` -------------------------------- ### Defining a Bitfield with Typed and Untyped Fields Source: https://github.com/hawkw/mycelium/blob/main/bitfield/README.md Illustrates the definition of a structured bitfield using the bitfield! macro. It includes typed fields (enums, bool, u8) and untyped raw bit fields. ```rust bitfield! { pub struct TypedBitfield { /// Use the first two bits to represent a typed `MyEnum` value. const ENUM_VALUE: MyEnum; /// Typed values and untyped raw bit fields can be used in the /// same bitfield type. pub const SOME_BITS = 6; /// The `FromBits` trait is also implemented for `bool`, which /// can be used to implement bitflags. pub const FLAG_1: bool; pub const FLAG_2: bool; /// `FromBits` is also implemented by (signed and unsigned) integer /// types. This will allow the next 8 bits to be treated as a `u8`. pub const A_BYTE: u8; /// We can also use the automatically generated enum: pub const OTHER_ENUM: MyGeneratedEnum; } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.