Skip to content

Commit

Permalink
fix set_bit, edit borsh features
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacholt100 committed Sep 18, 2024
1 parent 8e207de commit 7c8fc00
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bnum"
version = "0.11.0"
version = "0.12.0"
authors = ["isaac-holt <isaac_holt@icloud.com>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand All @@ -20,7 +20,6 @@ default = []
nightly = []
serde = ["dep:serde", "serde-big-array"]
numtraits = ["num-integer", "num-traits"]
borsh = ["dep:borsh"]

[dependencies]
num-integer = { version = "0.1", optional = true, default-features = false }
Expand All @@ -34,7 +33,7 @@ quickcheck = { version = "1.0", optional = true, default-features = false }
# proptest = { version = "1.2", optional = true, default-features = false }
valuable = { version = "0.1", optional = true, features = ["derive"], default-features = false }
# lit-parser = { path = "./lit-parser/", optional = true }
borsh = { version = "^1.5", optional = true, default-features = false, features = ["rc", "std", "unstable__schema"]}
borsh = { version = "^1.5", optional = true, default-features = false, features = ["unstable__schema"] }

[dev-dependencies]
quickcheck = "1.0"
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ This crate uses Rust's const generics to allow creation of integers of arbitrary
To install and use `bnum`, simply add the following line to your `Cargo.toml` file in the `[dependencies]` section:

```toml
bnum = "0.11.0"
bnum = "0.12.0"
```

Or, to enable various `bnum` features as well, add for example this line instead:

```toml
bnum = { version = "0.11.0", features = ["rand"] } # enables the "rand" feature
bnum = { version = "0.12.0", features = ["rand"] } # enables the "rand" feature
```

## Example Usage
Expand Down Expand Up @@ -113,6 +113,8 @@ The `rand` feature allows creation of random `bnum` integers via the [`rand`](ht

The `serde` feature enables serialization and deserialization of `bnum` integers via the [`serde`](https://docs.rs/serde/latest/serde/) and [`serde_big_array`](https://docs.rs/serde-big-array/latest/serde_big_array/) crates.

The `borsh`

### `num_traits` and `num_integer` trait implementations

The `numtraits` feature includes implementations of traits from the [`num_traits`](https://docs.rs/num-traits/latest/num_traits/) and [`num_integer`](https://docs.rs/num-integer/latest/num_integer/) crates, e.g. [`AsPrimitive`](https://docs.rs/num-traits/latest/num_traits/cast/trait.AsPrimitive.html), [`Signed`](https://docs.rs/num-traits/latest/num_traits/sign/trait.Signed.html), [`Integer`](https://docs.rs/num-integer/latest/num_integer/trait.Integer.html) and [`Roots`](https://docs.rs/num-integer/latest/num_integer/trait.Roots.html).
Expand All @@ -131,7 +133,7 @@ The `valuable` feature enables the [`Valuable`](https://docs.rs/valuable/latest/

### Nightly features

Activating the `nightly` feature will enable the `from_be_bytes`, `from_le_bytes`, `from_ne_bytes`, `to_be_bytes`, `to_le_bytes` and `to_ne_bytes` methods on `bnum`'s unsigned and signed integers and will make the `unchecked_...` methods `const`. This comes at the cost of only being able to compile on nightly. The nightly features that this uses are [`generic_const_exprs`](https://github.com/rust-lang/rust/issues/76560), [`const_trait_impl`](https://github.com/rust-lang/rust/issues/67792), [`effects`](https://github.com/rust-lang/rust/issues/102090) and [`const_option_ext`](https://github.com/rust-lang/rust/issues/91930).
Activating the `nightly` feature will enable the `from_be_bytes`, `from_le_bytes`, `from_ne_bytes`, `to_be_bytes`, `to_le_bytes` and `to_ne_bytes` methods on `bnum`'s unsigned and signed integers and will make the `unchecked_...` methods `const`. This comes at the cost of only being able to compile on nightly. The nightly features that this uses are [`generic_const_exprs`](https://github.com/rust-lang/rust/issues/76560), [`const_trait_impl`](https://github.com/rust-lang/rust/issues/67792), [`effects`](https://github.com/rust-lang/rust/issues/102090) and [`const_option`](https://github.com/rust-lang/rust/issues/67441).

## Testing

Expand Down
7 changes: 5 additions & 2 deletions src/buint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,12 +463,15 @@ macro_rules! mod_impl {
}

#[doc = doc::set_bit!(U 256)]
#[must_use]
#[inline]
pub fn set_bit(&mut self, index: ExpType, value: bool) {
let digit = &mut self.digits[index as usize >> digit::$Digit::BIT_SHIFT];
let shift = index & digit::$Digit::BITS_MINUS_1;
*digit = *digit & (1 << shift) | ($Digit::from(value) << shift)
if value {
*digit |= (1 << shift);
} else {
*digit &= !(1 << shift);
}
}

/// Returns an integer whose value is `2^power`. This is faster than using a shift left on `Self::ONE`.
Expand Down
7 changes: 4 additions & 3 deletions src/doc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ macro_rules! bit {
($sign: ident $bits: literal) => {
doc::doc_comment! {
$sign $bits,
"Returns a boolean representing the bit in the given position (`true` if the bit is set). The least significant bit is at index `0`, the most significant bit is at index `Self::BITS - 1`",
"Returns a boolean representing the bit in the given position (`true` if the bit is set). The least significant bit is at index `0`, the most significant bit is at index `Self::BITS - 1`.",

"let n = " doc::type_str!($sign $bits) "::from(0b001010100101010101u32);\n"
"assert!(n.bit(0));\n"
Expand All @@ -349,12 +349,13 @@ macro_rules! set_bit {
($sign: ident $bits: literal) => {
doc::doc_comment! {
$sign $bits,
"Sets/unsets the bit in the given position (`1` if value is true). The least significant bit is at index `0`, the most significant bit is at index `Self::BITS - 1`",
"Sets/unsets the bit in the given position (`1` if value is true). The least significant bit is at index `0`, the most significant bit is at index `Self::BITS - 1`.",

"let mut n = " doc::type_str!($sign $bits) "::from(0b001010100101010101u32);\n"
"assert!(n.bit(2));\n"
"n.set_bit(2,false);\n"
"n.set_bit(2, false);\n"
"assert!(!n.bit(2));\n"
"assert_eq!(n, " doc::type_str!($sign $bits) "::from(0b001010100101010001u32));"
}
};
}
Expand Down
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
feature(
generic_const_exprs,
const_trait_impl,
const_option_ext,
const_option,
// effects,
)
)]
Expand Down Expand Up @@ -42,6 +42,13 @@ pub mod random;

pub mod types;

#[test]
fn test_set_bit() {
let mut a = types::U128::MAX;
a.set_bit(6, false);
panic!("{:b}", a);
}

// #[cfg(feature = "nightly")]
// mod float;

Expand Down

0 comments on commit 7c8fc00

Please sign in to comment.