Skip to content

Commit

Permalink
Merge pull request #30 from kas-gui/work
Browse files Browse the repository at this point in the history
0.5.3: MSRV=1.60, Edition=2021, explicit features, doc_auto_cfg, Clippy
  • Loading branch information
dhardy authored Dec 12, 2024
2 parents 023d745 + fab5453 commit d3dbe1c
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 43 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ jobs:
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: "1.53.0"
toolchain: "1.60.0"
override: true
- name: Use Cargo.lock.msrv
run: cp Cargo.lock.msrv Cargo.lock
- name: Test
run: |
cargo test
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

## [0.5.3] — 2024-12-12

- Bump MSRV to 1.60.0 and use Edition 2021 (#30)

## [0.5.2] — 2022-12-22

- Support cast to self, useful in generics (#28)
Expand Down
16 changes: 16 additions & 0 deletions Cargo.lock.msrv

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
[package]
name = "easy-cast"
version = "0.5.2"
version = "0.5.3"
authors = ["Diggory Hardy <git@dhardy.name>"]
edition = "2018"
edition = "2021"
license = "Apache-2.0"
description = "Type conversions which are expected to succeed"
readme = "README.md"
documentation = "https://docs.rs/easy-cast/"
keywords = ["cast", "into", "from", "conversion"]
repository = "https://github.com/kas-gui/easy-cast"
rust-version = "1.60.0"

[package.metadata.docs.rs]
features = []
# To build locally:
# RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --open

[features]
default = ["std"]

# Without std, float conversions are disabled (unless libm is used)
std = []

# libm may be used instead of std to provide float conversions
libm = ["dep:libm"]

# Note: assertions are always used in debug builds; these only affect release builds:

# Always use all assertions
Expand All @@ -31,6 +40,5 @@ assert_int = []
assert_digits = []

[dependencies.libm]
# libm may be used instead of std to provide float conversions
version = "0.2.1"
optional = true
4 changes: 1 addition & 3 deletions src/impl_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ impl Conv<()> for () {
Ok(())
}
#[inline]
fn conv(_: ()) -> Self {
()
}
fn conv(_: ()) -> Self {}
}
impl<S0, T0: Conv<S0>> Conv<(S0,)> for (T0,) {
#[inline]
Expand Down
24 changes: 12 additions & 12 deletions src/impl_float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use super::*;

#[allow(clippy::manual_range_contains)]
impl ConvApprox<f64> for f32 {
fn try_conv_approx(x: f64) -> Result<f32> {
use core::num::FpCategory;
Expand Down Expand Up @@ -80,7 +81,6 @@ impl FloatRound for f64 {
}

#[cfg(any(feature = "std", feature = "libm"))]
#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "libm"))))]
macro_rules! impl_float {
($x:ty: $y:tt) => {
impl ConvFloat<$x> for $y {
Expand Down Expand Up @@ -140,8 +140,8 @@ macro_rules! impl_float {
#[inline]
fn try_conv_trunc(x: $x) -> Result<Self> {
// Tested: these limits work for $x=f32 and all $y except u128
const LBOUND: $x = core::$y::MIN as $x - 1.0;
const UBOUND: $x = core::$y::MAX as $x + 1.0;
const LBOUND: $x = $y::MIN as $x - 1.0;
const UBOUND: $x = $y::MAX as $x + 1.0;
if x > LBOUND && x < UBOUND {
Ok(x as $y)
} else {
Expand All @@ -151,10 +151,10 @@ macro_rules! impl_float {
#[inline]
fn try_conv_nearest(x: $x) -> Result<Self> {
// Tested: these limits work for $x=f32 and all $y except u128
const LBOUND: $x = core::$y::MIN as $x;
const UBOUND: $x = core::$y::MAX as $x + 1.0;
const LBOUND: $x = $y::MIN as $x;
const UBOUND: $x = $y::MAX as $x + 1.0;
let x = x.round();
if x >= LBOUND && x < UBOUND {
if (LBOUND..UBOUND).contains(&x) {
Ok(x as $y)
} else {
Err(Error::Range)
Expand All @@ -163,10 +163,10 @@ macro_rules! impl_float {
#[inline]
fn try_conv_floor(x: $x) -> Result<Self> {
// Tested: these limits work for $x=f32 and all $y except u128
const LBOUND: $x = core::$y::MIN as $x;
const UBOUND: $x = core::$y::MAX as $x + 1.0;
const LBOUND: $x = $y::MIN as $x;
const UBOUND: $x = $y::MAX as $x + 1.0;
let x = x.floor();
if x >= LBOUND && x < UBOUND {
if (LBOUND..UBOUND).contains(&x) {
Ok(x as $y)
} else {
Err(Error::Range)
Expand All @@ -175,10 +175,10 @@ macro_rules! impl_float {
#[inline]
fn try_conv_ceil(x: $x) -> Result<Self> {
// Tested: these limits work for $x=f32 and all $y except u128
const LBOUND: $x = core::$y::MIN as $x;
const UBOUND: $x = core::$y::MAX as $x + 1.0;
const LBOUND: $x = $y::MIN as $x;
const UBOUND: $x = $y::MAX as $x + 1.0;
let x = x.ceil();
if x >= LBOUND && x < UBOUND {
if (LBOUND..UBOUND).contains(&x) {
Ok(x as $y)
} else {
Err(Error::Range)
Expand Down
42 changes: 21 additions & 21 deletions src/impl_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ macro_rules! impl_via_as_max_check {
fn conv(x: $x) -> $y {
#[cfg(any(debug_assertions, feature = "assert_int"))]
assert!(
x <= core::$y::MAX as $x,
x <= $y::MAX as $x,
"cast x: {} to {}: expected x <= {}, found x = {}",
stringify!($x), stringify!($y), core::$y::MAX, x
stringify!($x), stringify!($y), $y::MAX, x
);
x as $y
}
#[inline]
fn try_conv(x: $x) -> Result<Self> {
if x <= core::$y::MAX as $x {
if x <= $y::MAX as $x {
Ok(x as $y)
} else {
Err(Error::Range)
Expand Down Expand Up @@ -90,15 +90,15 @@ macro_rules! impl_via_as_range_check {
fn conv(x: $x) -> $y {
#[cfg(any(debug_assertions, feature = "assert_int"))]
assert!(
core::$y::MIN as $x <= x && x <= core::$y::MAX as $x,
$y::MIN as $x <= x && x <= $y::MAX as $x,
"cast x: {} to {}: expected {} <= x <= {}, found x = {}",
stringify!($x), stringify!($y), core::$y::MIN, core::$y::MAX, x
stringify!($x), stringify!($y), $y::MIN, $y::MAX, x
);
x as $y
}
#[inline]
fn try_conv(x: $x) -> Result<Self> {
if core::$y::MIN as $x <= x && x <= core::$y::MAX as $x {
if $y::MIN as $x <= x && x <= $y::MAX as $x {
Ok(x as $y)
} else {
Err(Error::Range)
Expand All @@ -123,8 +123,8 @@ macro_rules! impl_int_generic {
#[allow(unused_comparisons)]
#[inline]
fn conv(x: $x) -> $y {
let src_is_signed = core::$x::MIN != 0;
let dst_is_signed = core::$y::MIN != 0;
let src_is_signed = $x::MIN != 0;
let dst_is_signed = $y::MIN != 0;
if size_of::<$x>() < size_of::<$y>() {
if !dst_is_signed {
#[cfg(any(debug_assertions, feature = "assert_int"))]
Expand All @@ -138,9 +138,9 @@ macro_rules! impl_int_generic {
if dst_is_signed {
#[cfg(any(debug_assertions, feature = "assert_int"))]
assert!(
x <= core::$y::MAX as $x,
x <= $y::MAX as $x,
"cast x: {} to {}: expected x <= {}, found x = {}",
stringify!($x), stringify!($y), core::$y::MAX, x
stringify!($x), stringify!($y), $y::MAX, x
);
} else if src_is_signed {
#[cfg(any(debug_assertions, feature = "assert_int"))]
Expand All @@ -155,16 +155,16 @@ macro_rules! impl_int_generic {
if src_is_signed {
#[cfg(any(debug_assertions, feature = "assert_int"))]
assert!(
core::$y::MIN as $x <= x && x <= core::$y::MAX as $x,
$y::MIN as $x <= x && x <= $y::MAX as $x,
"cast x: {} to {}: expected {} <= x <= {}, found x = {}",
stringify!($x), stringify!($y), core::$y::MIN, core::$y::MAX, x
stringify!($x), stringify!($y), $y::MIN, $y::MAX, x
);
} else {
#[cfg(any(debug_assertions, feature = "assert_int"))]
assert!(
x <= core::$y::MAX as $x,
x <= $y::MAX as $x,
"cast x: {} to {}: expected x <= {}, found x = {}",
stringify!($x), stringify!($y), core::$y::MAX, x
stringify!($x), stringify!($y), $y::MAX, x
);
}
}
Expand All @@ -173,15 +173,15 @@ macro_rules! impl_int_generic {
#[allow(unused_comparisons)]
#[inline]
fn try_conv(x: $x) -> Result<Self> {
let src_is_signed = core::$x::MIN != 0;
let dst_is_signed = core::$y::MIN != 0;
let src_is_signed = $x::MIN != 0;
let dst_is_signed = $y::MIN != 0;
if size_of::<$x>() < size_of::<$y>() {
if dst_is_signed || x >= 0 {
return Ok(x as $y);
}
} else if size_of::<$x>() == size_of::<$y>() {
if dst_is_signed {
if x <= core::$y::MAX as $x {
if x <= $y::MAX as $x {
return Ok(x as $y);
}
} else if src_is_signed {
Expand All @@ -195,11 +195,11 @@ macro_rules! impl_int_generic {
} else {
// src size > dst size
if src_is_signed {
if core::$y::MIN as $x <= x && x <= core::$y::MAX as $x {
if $y::MIN as $x <= x && x <= $y::MAX as $x {
return Ok(x as $y);
}
} else {
if x <= core::$y::MAX as $x {
if x <= $y::MAX as $x {
return Ok(x as $y);
}
}
Expand Down Expand Up @@ -249,7 +249,7 @@ macro_rules! impl_via_digits_check {
fn try_conv(x: $x) -> Result<Self> {
let src_ty_bits = (size_of::<$x>() * 8) as u32;
let src_digits = src_ty_bits.saturating_sub(x.leading_zeros() + x.trailing_zeros());
let dst_digits = core::$y::MANTISSA_DIGITS;
let dst_digits = $y::MANTISSA_DIGITS;
if src_digits <= dst_digits {
Ok(x as $y)
} else {
Expand Down Expand Up @@ -286,7 +286,7 @@ macro_rules! impl_via_digits_check_signed {
let src_digits = x.checked_abs()
.map(|y| src_ty_bits.saturating_sub(y.leading_zeros() + y.trailing_zeros()))
.unwrap_or(1 /*MIN has one binary digit in float repr*/);
let dst_digits = core::$y::MANTISSA_DIGITS;
let dst_digits = $y::MANTISSA_DIGITS;
if src_digits <= dst_digits {
Ok(x as $y)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
#![deny(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(doc_cfg, feature(doc_cfg))]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]

mod impl_basic;
mod impl_float;
Expand Down
2 changes: 0 additions & 2 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ impl<S, T: ConvApprox<S>> CastApprox<T> for S {
///
/// The sister-trait [`CastFloat`] supports "into" style usage.
#[cfg(any(feature = "std", feature = "libm"))]
#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "libm"))))]
pub trait ConvFloat<T>: Sized {
/// Try converting to integer with truncation
///
Expand Down Expand Up @@ -280,7 +279,6 @@ pub trait ConvFloat<T>: Sized {
/// This trait is automatically implemented for every implementation of
/// [`ConvFloat`].
#[cfg(any(feature = "std", feature = "libm"))]
#[cfg_attr(doc_cfg, doc(cfg(any(feature = "std", feature = "libm"))))]
pub trait CastFloat<T> {
/// Cast to integer, truncating
///
Expand Down

0 comments on commit d3dbe1c

Please sign in to comment.