Merge pull request #229 from phil-opp/update-bitfield

Update bit_field to 0.5.0 and use new trait based API
This commit is contained in:
Philipp Oppermann
2016-09-27 13:36:29 +02:00
committed by GitHub
3 changed files with 15 additions and 13 deletions

View File

@@ -4,7 +4,7 @@ name = "blog_os"
version = "0.1.0" version = "0.1.0"
[dependencies] [dependencies]
bit_field = "0.1.0" bit_field = "0.5.0"
bitflags = "0.7.0" bitflags = "0.7.0"
once = "0.3.2" once = "0.3.2"
rlibc = "0.1.4" rlibc = "0.1.4"
@@ -13,6 +13,10 @@ spin = "0.3.4"
[dependencies.hole_list_allocator] [dependencies.hole_list_allocator]
path = "libs/hole_list_allocator" path = "libs/hole_list_allocator"
[dependencies.lazy_static]
features = ["spin_no_std"]
version = "0.2.1"
[dependencies.multiboot2] [dependencies.multiboot2]
git = "https://github.com/phil-opp/multiboot2-elf64" git = "https://github.com/phil-opp/multiboot2-elf64"
@@ -20,17 +24,15 @@ git = "https://github.com/phil-opp/multiboot2-elf64"
default-features = false default-features = false
version = "0.7.1" version = "0.7.1"
[dependencies.lazy_static]
version = "0.2.1"
features = ["spin_no_std"]
[lib] [lib]
crate-type = ["staticlib"] crate-type = ["staticlib"]
[workspace] [profile]
[profile.dev] [profile.dev]
panic = "abort" panic = "abort"
[profile.release] [profile.release]
panic = "abort" panic = "abort"
[workspace]

View File

@@ -146,7 +146,7 @@ Or:
self.0 = ((self.0 >> 3) << 3) | stack_index; self.0 = ((self.0 >> 3) << 3) | stack_index;
``` ```
Well, none of these variants is really _readable_ and it's very easy to make mistakes somewhere. Therefore I created a `BitField` type with the following [Range]-based API: Well, none of these variants is really _readable_ and it's very easy to make mistakes somewhere. Therefore I created a `BitField` trait that provides the following [Range]-based API:
[Range]: https://doc.rust-lang.org/nightly/core/ops/struct.Range.html [Range]: https://doc.rust-lang.org/nightly/core/ops/struct.Range.html
@@ -154,11 +154,11 @@ Well, none of these variants is really _readable_ and it's very easy to make mis
self.0.set_range(0..3, stack_index); self.0.set_range(0..3, stack_index);
``` ```
I think it is much more readable, since we abstracted away all bit-masking details. The `BitField` type is contained in the [bit_field] crate. (It's pretty new, so it might still contain bugs.) To add it as dependency, we run `cargo add bit_field` and add `extern crate bit_field;` to our `src/lib.rs`. I think it is much more readable, since we abstracted away all bit-masking details. The `BitField` trait is contained in the [bit_field] crate. (It's pretty new, so it might still contain bugs.) To add it as dependency, we run `cargo add bit_field` and add `extern crate bit_field;` to our `src/lib.rs`.
[bit_field]: https://crates.io/crates/bit_field [bit_field]: https://crates.io/crates/bit_field
Now we can use the crate to implement the methods of `EntryOptions`: Now we can use the trait to implement the methods of `EntryOptions`:
```rust ```rust
// in src/interrupts/idt.rs // in src/interrupts/idt.rs
@@ -166,11 +166,11 @@ Now we can use the crate to implement the methods of `EntryOptions`:
use bit_field::BitField; use bit_field::BitField;
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct EntryOptions(BitField<u16>); pub struct EntryOptions(u16);
impl EntryOptions { impl EntryOptions {
fn minimal() -> Self { fn minimal() -> Self {
let mut options = BitField::new(0); let mut options = 0;
options.set_range(9..12, 0b111); // 'must-be-one' bits options.set_range(9..12, 0b111); // 'must-be-one' bits
EntryOptions(options) EntryOptions(options)
} }

View File

@@ -66,11 +66,11 @@ impl Entry {
use bit_field::BitField; use bit_field::BitField;
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct EntryOptions(BitField<u16>); pub struct EntryOptions(u16);
impl EntryOptions { impl EntryOptions {
fn minimal() -> Self { fn minimal() -> Self {
let mut options = BitField::new(0); let mut options = 0;
options.set_range(9..12, 0b111); // 'must-be-one' bits options.set_range(9..12, 0b111); // 'must-be-one' bits
EntryOptions(options) EntryOptions(options)
} }