Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Bounded, ToPrimitive, NumCast, FromPrimitive for NonZero* #334

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions src/bounds.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use core::num::Wrapping;
use core::num::{
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
};
use core::{f32, f64};
use core::{i128, i16, i32, i64, i8, isize};
use core::{u128, u16, u32, u64, u8, usize};
Expand Down Expand Up @@ -68,6 +72,49 @@ bounded_impl!(i32, i32::MIN, i32::MAX);
bounded_impl!(i64, i64::MIN, i64::MAX);
bounded_impl!(i128, i128::MIN, i128::MAX);

macro_rules! bounded_impl_nonzero_const {
($t:ty, $v:expr, $i:ident) => {
const $i: $t = {
let arr = [$v];
let idx = if $v != 0 { 0 } else { 1 };

unsafe { <$t>::new_unchecked(arr[idx]) }
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be done safely -- NonZero::new is const since 1.47, and const panic! since 1.57. (Our MSRV is 1.60.)

Suggested change
const $i: $t = {
let arr = [$v];
let idx = if $v != 0 { 0 } else { 1 };
unsafe { <$t>::new_unchecked(arr[idx]) }
};
const $i: $t = match <$t>::new($v) {
Some(nz) => nz,
None => panic!("bad nonzero bound!"),
};

And once our MSRV is 1.70 we should use the inherent consts, so please leave a comment for that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, done.

};
}

macro_rules! bounded_impl_nonzero {
($t:ty, $min:expr, $max:expr) => {
impl Bounded for $t {
#[inline]
fn min_value() -> $t {
bounded_impl_nonzero_const!($t, $min, MIN);
MIN
}

#[inline]
fn max_value() -> $t {
bounded_impl_nonzero_const!($t, $max, MAX);
MAX
}
}
};
}

bounded_impl_nonzero!(NonZeroUsize, 1, usize::MAX);
bounded_impl_nonzero!(NonZeroU8, 1, u8::MAX);
bounded_impl_nonzero!(NonZeroU16, 1, u16::MAX);
bounded_impl_nonzero!(NonZeroU32, 1, u32::MAX);
bounded_impl_nonzero!(NonZeroU64, 1, u64::MAX);
bounded_impl_nonzero!(NonZeroU128, 1, u128::MAX);

bounded_impl_nonzero!(NonZeroIsize, isize::MIN, isize::MAX);
bounded_impl_nonzero!(NonZeroI8, i8::MIN, i8::MAX);
bounded_impl_nonzero!(NonZeroI16, i16::MIN, i16::MAX);
bounded_impl_nonzero!(NonZeroI32, i32::MIN, i32::MAX);
bounded_impl_nonzero!(NonZeroI64, i64::MIN, i64::MAX);
bounded_impl_nonzero!(NonZeroI128, i128::MIN, i128::MAX);

impl<T: Bounded> Bounded for Wrapping<T> {
fn min_value() -> Self {
Wrapping(T::min_value())
Expand Down Expand Up @@ -146,3 +193,37 @@ fn wrapping_is_bounded() {
require_bounded(&Wrapping(42_u32));
require_bounded(&Wrapping(-42));
}

#[test]
fn bounded_unsigned_nonzero() {
macro_rules! test_bounded_impl_unsigned_nonzero {
($t:ty, $base_ty:ty) => {
assert_eq!(<$t as Bounded>::min_value().get(), 1);
assert_eq!(<$t as Bounded>::max_value().get(), <$base_ty>::MAX);
};
}

test_bounded_impl_unsigned_nonzero!(NonZeroUsize, usize);
test_bounded_impl_unsigned_nonzero!(NonZeroU8, u8);
test_bounded_impl_unsigned_nonzero!(NonZeroU16, u16);
test_bounded_impl_unsigned_nonzero!(NonZeroU32, u32);
test_bounded_impl_unsigned_nonzero!(NonZeroU64, u64);
test_bounded_impl_unsigned_nonzero!(NonZeroU128, u128);
}

#[test]
fn bounded_signed_nonzero() {
macro_rules! test_bounded_impl_signed_nonzero {
($t:ty, $base_ty:ty) => {
assert_eq!(<$t as Bounded>::min_value().get(), <$base_ty>::MIN);
assert_eq!(<$t as Bounded>::max_value().get(), <$base_ty>::MAX);
};
}

test_bounded_impl_signed_nonzero!(NonZeroIsize, isize);
test_bounded_impl_signed_nonzero!(NonZeroI8, i8);
test_bounded_impl_signed_nonzero!(NonZeroI16, i16);
test_bounded_impl_signed_nonzero!(NonZeroI32, i32);
test_bounded_impl_signed_nonzero!(NonZeroI64, i64);
test_bounded_impl_signed_nonzero!(NonZeroI128, i128);
}
166 changes: 166 additions & 0 deletions src/cast.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use core::mem::size_of;
use core::num::Wrapping;
use core::num::{
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
};
use core::{f32, f64};
use core::{i128, i16, i32, i64, i8, isize};
use core::{u128, u16, u32, u64, u8, usize};
Expand Down Expand Up @@ -266,6 +270,61 @@ impl_to_primitive_uint!(u32);
impl_to_primitive_uint!(u64);
impl_to_primitive_uint!(u128);

macro_rules! impl_to_primitive_nonzero_to_method {
($SrcT:ident : $( $(#[$cfg:meta])* fn $method:ident -> $DstT:ident ; )*) => {$(
#[inline]
$(#[$cfg])*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you copied the cfg from the other macros, but it's vestigial, from the days when i128 was conditional. We definitely shouldn't propagate that, but could you clean up the others too?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure what it was so I just kept it. Removed here and from other macros

fn $method(&self) -> Option<$DstT> {
self.get().$method()
}
)*}
}

macro_rules! impl_to_primitive_nonzero {
($T:ident) => {
impl ToPrimitive for $T {
impl_to_primitive_nonzero_to_method! { $T:
fn to_isize -> isize;
fn to_i8 -> i8;
fn to_i16 -> i16;
fn to_i32 -> i32;
fn to_i64 -> i64;
fn to_i128 -> i128;

fn to_usize -> usize;
fn to_u8 -> u8;
fn to_u16 -> u16;
fn to_u32 -> u32;
fn to_u64 -> u64;
fn to_u128 -> u128;
}

#[inline]
fn to_f32(&self) -> Option<f32> {
Some(self.get() as f32)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this forwarding to the inner to_f32 as well?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, I think I copied from the other macro and didn't notice here I could put everything together.

}
#[inline]
fn to_f64(&self) -> Option<f64> {
Some(self.get() as f64)
}
}
};
}

impl_to_primitive_nonzero!(NonZeroUsize);
impl_to_primitive_nonzero!(NonZeroU8);
impl_to_primitive_nonzero!(NonZeroU16);
impl_to_primitive_nonzero!(NonZeroU32);
impl_to_primitive_nonzero!(NonZeroU64);
impl_to_primitive_nonzero!(NonZeroU128);

impl_to_primitive_nonzero!(NonZeroIsize);
impl_to_primitive_nonzero!(NonZeroI8);
impl_to_primitive_nonzero!(NonZeroI16);
impl_to_primitive_nonzero!(NonZeroI32);
impl_to_primitive_nonzero!(NonZeroI64);
impl_to_primitive_nonzero!(NonZeroI128);

macro_rules! impl_to_primitive_float_to_float {
($SrcT:ident : $( fn $method:ident -> $DstT:ident ; )*) => {$(
#[inline]
Expand Down Expand Up @@ -572,6 +631,85 @@ impl_from_primitive!(u128, to_u128);
impl_from_primitive!(f32, to_f32);
impl_from_primitive!(f64, to_f64);

macro_rules! impl_from_primitive_nonzero {
($T:ty, $to_ty:ident) => {
#[allow(deprecated)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is also vestigial, way back from commit 03db5c9 when it was copied from its deprecated state in the standard library.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

impl FromPrimitive for $T {
#[inline]
fn from_isize(n: isize) -> Option<$T> {
n.$to_ty().and_then(Self::new)
}
#[inline]
fn from_i8(n: i8) -> Option<$T> {
n.$to_ty().and_then(Self::new)
}
#[inline]
fn from_i16(n: i16) -> Option<$T> {
n.$to_ty().and_then(Self::new)
}
#[inline]
fn from_i32(n: i32) -> Option<$T> {
n.$to_ty().and_then(Self::new)
}
#[inline]
fn from_i64(n: i64) -> Option<$T> {
n.$to_ty().and_then(Self::new)
}
#[inline]
fn from_i128(n: i128) -> Option<$T> {
n.$to_ty().and_then(Self::new)
}

#[inline]
fn from_usize(n: usize) -> Option<$T> {
n.$to_ty().and_then(Self::new)
}
#[inline]
fn from_u8(n: u8) -> Option<$T> {
n.$to_ty().and_then(Self::new)
}
#[inline]
fn from_u16(n: u16) -> Option<$T> {
n.$to_ty().and_then(Self::new)
}
#[inline]
fn from_u32(n: u32) -> Option<$T> {
n.$to_ty().and_then(Self::new)
}
#[inline]
fn from_u64(n: u64) -> Option<$T> {
n.$to_ty().and_then(Self::new)
}
#[inline]
fn from_u128(n: u128) -> Option<$T> {
n.$to_ty().and_then(Self::new)
}

#[inline]
fn from_f32(n: f32) -> Option<$T> {
n.$to_ty().and_then(Self::new)
}
#[inline]
fn from_f64(n: f64) -> Option<$T> {
n.$to_ty().and_then(Self::new)
}
}
};
}

impl_from_primitive_nonzero!(NonZeroIsize, to_isize);
impl_from_primitive_nonzero!(NonZeroI8, to_i8);
impl_from_primitive_nonzero!(NonZeroI16, to_i16);
impl_from_primitive_nonzero!(NonZeroI32, to_i32);
impl_from_primitive_nonzero!(NonZeroI64, to_i64);
impl_from_primitive_nonzero!(NonZeroI128, to_i128);
impl_from_primitive_nonzero!(NonZeroUsize, to_usize);
impl_from_primitive_nonzero!(NonZeroU8, to_u8);
impl_from_primitive_nonzero!(NonZeroU16, to_u16);
impl_from_primitive_nonzero!(NonZeroU32, to_u32);
impl_from_primitive_nonzero!(NonZeroU64, to_u64);
impl_from_primitive_nonzero!(NonZeroU128, to_u128);

macro_rules! impl_to_primitive_wrapping {
($( $(#[$cfg:meta])* fn $method:ident -> $i:ident ; )*) => {$(
#[inline]
Expand Down Expand Up @@ -695,6 +833,34 @@ impl_num_cast!(isize, to_isize);
impl_num_cast!(f32, to_f32);
impl_num_cast!(f64, to_f64);

macro_rules! impl_num_cast_nonzero {
($T:ty, $conv:ident) => {
impl NumCast for $T {
#[inline]
#[allow(deprecated)]
fn from<N: ToPrimitive>(n: N) -> Option<$T> {
// `$conv` could be generated using `concat_idents!`, but that
// macro seems to be broken at the moment
n.$conv().and_then(Self::new)
}
}
};
}

impl_num_cast_nonzero!(NonZeroUsize, to_usize);
impl_num_cast_nonzero!(NonZeroU8, to_u8);
impl_num_cast_nonzero!(NonZeroU16, to_u16);
impl_num_cast_nonzero!(NonZeroU32, to_u32);
impl_num_cast_nonzero!(NonZeroU64, to_u64);
impl_num_cast_nonzero!(NonZeroU128, to_u128);

impl_num_cast_nonzero!(NonZeroIsize, to_isize);
impl_num_cast_nonzero!(NonZeroI8, to_i8);
impl_num_cast_nonzero!(NonZeroI16, to_i16);
impl_num_cast_nonzero!(NonZeroI32, to_i32);
impl_num_cast_nonzero!(NonZeroI64, to_i64);
impl_num_cast_nonzero!(NonZeroI128, to_i128);

impl<T: NumCast> NumCast for Wrapping<T> {
fn from<U: ToPrimitive>(n: U) -> Option<Self> {
T::from(n).map(Wrapping)
Expand Down
Loading