Skip to content

Commit c90fcf3

Browse files
danlehmannDaniel Lehmann
andauthored
Add more const constructors and various convenience functions to Number for generic conversions (#47)
* Add more const constructors and various convenience functions to Number for generic conversions In particular: - new_u8, new_u16, ..., new_u128 which allow creating an arbitrary int without type conversion, e.g. `u5::new_u32(123)` - new_() which allows any Number argument to be passed through generics - as_() which easily converts any Number to another - as_u8(), as_u16() for more control (and to implement the others) * Fix compile errors * Fix some range checking bugs * Clarify compile time asserts, give them cleaner error messages First step in making .into() more usable * Improve comments; work in new assert_unchecked in the various getters * Add as_usize() * Add inline_const to const_convert_and_const_trait_impl (which is needed only for the old compiler) * Update changelog, bump version * Incorporate naming feedback * Fix test names --------- Co-authored-by: Daniel Lehmann <[email protected]>
1 parent 1953249 commit c90fcf3

File tree

4 files changed

+353
-22
lines changed

4 files changed

+353
-22
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
# Changelog
22

3-
## arbitrary-int 1.2.8
3+
## arbitrary-int 1.3.0
44

55
### Added
66

77
- New optional feature `hint`, which tells the compiler that the returned `value()` can't exceed a maximum value. This
88
allows the compiler to optimize faster code at the expense of unsafe code within arbitrary-int itself.
9+
- Various new const constructors: `new_u8`, `new_u16`, ..., `new_u128` which allow creating an arbitrary int without
10+
type conversion, e.g. `u5::new_u32(i)` (where i is e.g. u32). This is shorter than writing
11+
`u5::new(i.try_into().unwrap())`,
12+
and combines two possible panic paths into one. Also, unlike `try_into().unwrap()`, the new constructors are usable in
13+
const contexts.
14+
- For non-const contexts, `new_()` allows any Number argument to be passed through generics.
15+
- `as_()` easily converts any Number to another. `as_u8()`, `as_u16()` for more control (and to implement the others).
16+
- New optional feature `borsh` to support binary serialization using the borsh crate.
917

1018
## arbitrary-int 1.2.7
1119

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "arbitrary-int"
3-
version = "1.2.7"
3+
version = "1.3.0"
44
edition = "2021"
55
authors = ["Daniel Lehmann <[email protected]>"]
66
description = "Modern and lightweight implementation of u2, u3, u4, ..., u127."
@@ -15,7 +15,9 @@ std = []
1515

1616
# Supports const trait implementation through const_convert and const_trait_impl. Support for those was removed
1717
# from more recent Rust nightlies, so this feature requires an older Rust compiler
18-
# (2023-04-20 is broken, 2022-11-23 works. The exact day is somewhere inbetween)
18+
# (2023-04-20 is broken, 2022-11-23 works. The exact day is somewhere inbetween).
19+
# As of 12/2/2024, this also uses inline_const. This has been stabilized but is required for current code to work
20+
# on the old compiler.
1921
const_convert_and_const_trait_impl = []
2022

2123
# core::fmt::Step is currently unstable and is available on nightly behind a feature gate

0 commit comments

Comments
 (0)