Skip to content

Commit 9cdab5f

Browse files
author
Andrew Gazelka
authored
Merge pull request #9 from JarredAllen/remove-transmute-copy-failures
Remove transmute copy failures
2 parents a06a507 + 0f05995 commit 9cdab5f

File tree

8 files changed

+1229
-1093
lines changed

8 files changed

+1229
-1093
lines changed

Cargo.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
[package]
22
name = "bit-struct"
3-
version = "0.1.32"
3+
version = "0.2.0"
44
edition = "2021"
55
description = "Define structs which have fields which are assigned to individual bits, not bytes"
6-
repository = "https://github.com/andrewgazelka/bit-struct"
6+
repository = "https://github.com/parallel-systems/bit-struct"
7+
documentation = "https://docs.rs/crate/bit-struct/latest"
78
readme = "README.md"
89
license = "MIT OR Apache-2.0"
910
keywords = ["bit", "struct", "macros"]
11+
categories = ["no-std"]
12+
rust-version = "1.62.1"
1013

1114
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1215

1316
[dependencies]
1417
num-traits = { version = "0.2", default-features = false }
15-
serde = { version = "1.0", default-features = false }
18+
serde = { version = "1.0", default-features = false, features = ["derive"] }
1619

1720
[dev-dependencies]
1821
trybuild = "1.0"
19-
matches = "0.1.9"
22+
matches = "0.1.9"

README.md

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# bit-struct #![no_std]
1+
# bit-struct
22

33
[![crates.io](https://img.shields.io/crates/v/bit-struct.svg)](https://crates.io/crates/bit-struct)
44
[![codecov](https://codecov.io/gh/andrewgazelka/bit-struct/branch/main/graph/badge.svg?token=60R82VBBVF)](https://codecov.io/gh/andrewgazelka/bit-struct)
5-
![Minimum rustc version](https://img.shields.io/badge/rustc-1.57.0+-yellow.svg)
5+
![Minimum rustc version](https://img.shields.io/badge/rustc-1.62.1+-yellow.svg)
66

77
Bit struct is a crate which allows for ergonomic use of C-like bit fields without mediocre IDE support resulting from proc macros.
88
In addition, everything is statically typed checked!
@@ -14,12 +14,12 @@ use bit_struct::*;
1414

1515
enums! {
1616
// 2 bits, i.e., 0b00, 0b01, 0b10
17-
HouseKind { Urban, Suburban, Rural}
17+
pub HouseKind { Urban, Suburban, Rural}
1818
}
1919

2020
bit_struct! {
2121
// u8 is the base storage type. This can be any multiple of 8
22-
struct HouseConfig(u8) {
22+
pub struct HouseConfig(u8) {
2323
// 2 bits
2424
kind: HouseKind,
2525

@@ -30,47 +30,58 @@ bit_struct! {
3030
highest_floor: u2,
3131
}
3232
}
33-
```
3433

35-
We can create a new `HouseConfig` like such:
36-
```rust
34+
// We can create a new `HouseConfig` like such:
35+
// where all numbers are statically checked to be in bounds.
3736
let config = HouseConfig::new(HouseKind::Suburban, i3!(-2), u2!(1));
38-
```
39-
where all numbers are statically checked to be in bounds. We can get the
40-
raw `u8` which represents `config`:
41-
```rust
37+
38+
// We can get the raw `u8` which represents `config`:
4239
let raw: u8 = config.raw();
43-
```
44-
or we can get a `HouseConfig` from a `u8` like
45-
```rust
46-
let config: HouseConfig = HouseConfig::try_from(123_u8).unwrap();
47-
```
48-
We need to unwrap because `HouseConfig` is not valid for all numbers. For instance, if the
49-
most significant bits are `0b11`, it encodes an invalid `HouseKind`. However,
50-
if all elements of a struct are always valid (suppose we removed the `kind` field), the struct will
51-
auto implement a trait which allows calling
52-
```rust
53-
let config: HouseConfig = HouseConfig::exact_from(123_u8);
54-
```
55-
which will never panic. We can access values of `config` like so:
56-
```rust
57-
// get a value
40+
assert_eq!(114_u8, raw);
41+
42+
// or we can get a `HouseConfig` from a `u8` like:
43+
let mut config: HouseConfig = HouseConfig::try_from(114_u8).unwrap();
44+
assert_eq!(config, HouseConfig::new(HouseKind::Suburban, i3!(-2), u2!(1)));
45+
// We need to unwrap because `HouseConfig` is not valid for all numbers. For instance, if the
46+
// most significant bits are `0b11`, it encodes an invalid `HouseKind`. However,
47+
// if all elements of a struct are always valid (suppose we removed the `kind` field), the struct will
48+
// auto implement a trait which allows calling the non-panicking:
49+
// let config: HouseConfig = HouseConfig::exact_from(123_u8);
50+
51+
// We can access values of `config` like so:
5852
let kind: HouseKind = config.kind().get();
5953

60-
// set a value
54+
// And we can set values like so:
6155
config.lowest_floor().set(i3!(0));
62-
```
63-
We can also access bit-level numbers as std-lib values like this
64-
```rust
56+
57+
// We can also convert the new numeric types for alternate bit-widths into the
58+
// numeric types provided by the standard library:
6559
let lowest_floor: i3 = config.lowest_floor().get();
6660
let lowest_floor_std: i8 = lowest_floor.value();
61+
assert_eq!(lowest_floor_std, 0_i8);
6762
```
6863

6964
## Benefits
7065
- No proc macros
7166
- Autocompletion fully works (tested in IntelliJ Rust)
7267
- Fast compile times
73-
- Statically checked bounds (structs cannot be over-filled)
68+
- Statically checks that structs are not overfilled. For example, these overfilled structs will not compile:
69+
```compile_fail
70+
bit_struct::bit_struct! {
71+
struct TooManyFieldsToFit(u16) {
72+
a: u8,
73+
b: u8,
74+
c: bit_struct::u1
75+
}
76+
}
77+
```
78+
```compile_fail
79+
bit_struct::bit_struct! {
80+
struct FieldIsTooBig(u16) {
81+
a: u32
82+
}
83+
}
84+
```
7485
- Statically checked types
7586

7687
## Further Documentation

0 commit comments

Comments
 (0)