Skip to content

Commit

Permalink
Add more const constructors and various convenience functions to Numb…
Browse files Browse the repository at this point in the history
…er 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]>
  • Loading branch information
danlehmann and Daniel Lehmann authored Dec 13, 2024
1 parent 1953249 commit c90fcf3
Show file tree
Hide file tree
Showing 4 changed files with 353 additions and 22 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
# Changelog

## arbitrary-int 1.2.8
## arbitrary-int 1.3.0

### Added

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

## arbitrary-int 1.2.7

Expand Down
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "arbitrary-int"
version = "1.2.7"
version = "1.3.0"
edition = "2021"
authors = ["Daniel Lehmann <[email protected]>"]
description = "Modern and lightweight implementation of u2, u3, u4, ..., u127."
Expand All @@ -15,7 +15,9 @@ std = []

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

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

0 comments on commit c90fcf3

Please sign in to comment.