From 03627f896dbdc8e10f857a43a06918f826647b97 Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Fri, 18 Jun 2021 17:32:39 +0300 Subject: [PATCH 1/5] Update readme --- README.md | 25 +++++++++++++++++++++++++ src/lib.rs | 11 +++++++++++ src/num/integer.rs | 28 +++++++++++++++++++--------- 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index e1df369..b8a834c 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,31 @@ functionality which possible can get to the `std` some day. ## Highlights +- `Integer` super-trait that is implemented for all the built-in integers + and reflects the common part of their interfaces. + + ```rust + use stdext::prelude::*; + + fn accepts_any_integer(a: I, b: I) { + println!("{}", (a + b).count_ones()); + } + ``` + +- Safe conversions from floating numbers to integers. + + ```rust + use stdext::prelude::FloatConvert; + + let valid: Option = 10.5f32.checked_floor(); + let too_big: Option = 256f32.checked_floor(); + let nan: Option = f32::NAN.checked_floor(); + + assert_eq!(valid, Some(10u8)); + assert_eq!(too_big, None); + assert_eq!(nan, None); + ``` + - Convenient builder methods for **`Duration`**: ```rust diff --git a/src/lib.rs b/src/lib.rs index e01d533..53a8735 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,6 +23,7 @@ //! | [`Duration`] | [`DurationExt`] //! | [`RwLock`] | [`RwLockExt`] //! | [`Mutex`] | [`MutexExt`] +//! | [`f32`] and [`f64`] | [`FloatConvert`] //! //! [`Vec`]: https://doc.rust-lang.org/std/vec/struct.Vec.html //! [`&str`]: https://doc.rust-lang.org/std/primitive.str.html @@ -40,6 +41,16 @@ //! [`DurationExt`]: duration/trait.DurationExt.html //! [`RwLockExt`]: sync/rw_lock/trait.RwLockExt.html //! [`MutexExt`]: sync/mutex/trait.MutexExt.html +//! [`FloatConvert`]: num/float/trait.FloatConvert.html +//! +//! ## Integer super-trait +//! +//! While all built-in integer types have mostly the same interface, it's not backed by any trait, +//! which makes it impossible to write a function that will accept any built-in integer. +//! +//! [`Integer`] trait solves that problem by reflecting the common interface of all the built-in integers. +//! +//! [`Integer`]: num/integer/trait.Integer.html //! //! ## Macros //! diff --git a/src/num/integer.rs b/src/num/integer.rs index a8ec5ee..b5de630 100644 --- a/src/num/integer.rs +++ b/src/num/integer.rs @@ -25,23 +25,23 @@ use std::ops::{ /// as they exist for the unsigned numbers only. pub trait Integer: Sized - + Add + + Add + AddAssign - + Sub + + Sub + SubAssign - + Shr + + Shr + ShrAssign - + Shl + + Shl + ShlAssign - + BitAnd + + BitAnd + BitAndAssign - + BitOr + + BitOr + BitOrAssign - + BitXor + + BitXor + BitXorAssign - + Div + + Div + DivAssign - + Mul + + Mul + MulAssign + Copy { @@ -453,4 +453,14 @@ mod tests { 10u32.trailing_ones() ); } + + fn accepts_any_integer(a: I, b: I) -> u32 { + (a + b).count_ones() + } + + #[test] + fn composite() { + assert_eq!(accepts_any_integer(0u8, 0u8), 0); + assert_eq!(accepts_any_integer(1i128, 0i128), 1); + } } From 09f2f8e5d1908bfb103d8af58983563763c133ea Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Fri, 18 Jun 2021 17:32:56 +0300 Subject: [PATCH 2/5] Bump version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 3019b21..082fb62 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "stdext" -version = "0.2.1" +version = "0.3.0" authors = ["Igor Aleksanov "] edition = "2018" repository = "https://github.com/popzxc/stdext-rs" From 2ff4cf81a12906e5e378261940f83ae6254cb524 Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Fri, 18 Jun 2021 17:34:59 +0300 Subject: [PATCH 3/5] Add links to PRs into the changelog --- CHANGELOG.md | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e184439..b25750e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,15 +2,24 @@ ## [[Unreleased]] -- `try_match` and `unwrap_match` macros to get a certain variant from an enum. -- `return_ok` and `return_some` macros for early return of successful calculation. -- `Integer` trait that unifies all the built-in integer types under a single interface. -- `FloatConvert` trait that adds an interface for converting floating point numbers into integers. +## 0.3.0 (18.06.2021) + +- `try_match` and `unwrap_match` macros to get a certain variant from an enum [#11]. +- `return_ok` and `return_some` macros for early return of successful calculation [#11]. +- `Integer` trait that unifies all the built-in integer types under a single interface [#12]. +- `FloatConvert` trait that adds an interface for converting floating point numbers into integers [#12]. + +[#11]: https://github.com/popzxc/stdext-rs/pull/11 +[#12]: https://github.com/popzxc/stdext-rs/pull/12 ## 0.2.1 (09.07.2020) -- `VecExt::remove_item` method was added (#9). +- `VecExt::remove_item` method was added [#9]. + +[#9]: https://github.com/popzxc/stdext-rs/pull/9 ## 0.2.0 (02.07.2020) -- `compile_warning` and `function_name` macros were added (#4). +- `compile_warning` and `function_name` macros were added [#4]. + +[#4]: https://github.com/popzxc/stdext-rs/pull/4 From 7445f5d4e706af7c247069f40f167293d0a504d6 Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Fri, 18 Jun 2021 17:40:01 +0300 Subject: [PATCH 4/5] Fix dead link --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 53a8735..eee47ed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,7 +41,7 @@ //! [`DurationExt`]: duration/trait.DurationExt.html //! [`RwLockExt`]: sync/rw_lock/trait.RwLockExt.html //! [`MutexExt`]: sync/mutex/trait.MutexExt.html -//! [`FloatConvert`]: num/float/trait.FloatConvert.html +//! [`FloatConvert`]: num/float_convert/trait.FloatConvert.html //! //! ## Integer super-trait //! From 2179f94475f925a2eacdc2f2408d7ab352d0052c Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Fri, 18 Jun 2021 17:41:36 +0300 Subject: [PATCH 5/5] Add 'warn(missing_docs)' --- src/lib.rs | 2 ++ src/macros.rs | 2 ++ src/num/integer.rs | 3 +++ 3 files changed, 7 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index eee47ed..12f8619 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -124,6 +124,8 @@ //! } //! ``` +#![warn(missing_docs, unreachable_pub)] + pub mod duration; #[macro_use] pub mod macros; diff --git a/src/macros.rs b/src/macros.rs index 51521f4..1202a0a 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,3 +1,5 @@ +//! Various helper macros. + /// `compile_warning` macro is a brother of [`std::compile_error`], /// which emits a compile-time warning with a provided message. /// diff --git a/src/num/integer.rs b/src/num/integer.rs index b5de630..8798c65 100644 --- a/src/num/integer.rs +++ b/src/num/integer.rs @@ -45,8 +45,11 @@ pub trait Integer: + MulAssign + Copy { + /// The smallest value that can be represented by this integer type. const MIN: Self; + /// The largest value that can be represented by this integer type. const MAX: Self; + /// The size of this integer type in bits. const BITS: u32; /// See [`u128::from_str_radix`].