Skip to content

Commit

Permalink
Incorporate naming feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
danlehmann committed Dec 4, 2024
1 parent cb6b171 commit f885cba
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub trait Number: Sized + Copy + Clone + PartialOrd + Ord + PartialEq + Eq {
/// Creates a number from the given value, throwing an error if the value is too large.
/// This constructor is useful when the value is convertable to T. Use [`Self::new`] for literals.
#[cfg(not(feature = "const_convert_and_const_trait_impl"))]
fn new_<T: Number>(value: T) -> Self;
fn from_<T: Number>(value: T) -> Self;

/// Creates an instance from the given `value`. Unlike the various `new...` functions, this
/// will never fail as the value is masked to the result size.
Expand Down Expand Up @@ -157,7 +157,7 @@ macro_rules! impl_number_native {
fn value(self) -> Self::UnderlyingType { self }

#[inline]
fn new_<T: Number>(value: T) -> Self {
fn from_<T: Number>(value: T) -> Self {
if T::BITS > Self::BITS as usize {
assert!(value <= T::masked_new(Self::MAX));
}
Expand Down Expand Up @@ -346,7 +346,7 @@ macro_rules! uint_impl_num {
}

#[inline]
fn new_<T: Number>(value: T) -> Self {
fn from_<T: Number>(value: T) -> Self {
if Self::BITS < T::BITS {
assert!(value <= Self::MAX.value.as_());
}
Expand Down Expand Up @@ -415,7 +415,7 @@ macro_rules! uint_impl {

/// Creates an instance. Panics if the given value is outside of the valid range
#[inline]
pub const fn new_u8(value: u8) -> Self {
pub const fn from_u8(value: u8) -> Self {
if Self::BITS < 8 {
assert!(value <= Self::MAX.value as u8);
}
Expand All @@ -424,7 +424,7 @@ macro_rules! uint_impl {

/// Creates an instance. Panics if the given value is outside of the valid range
#[inline]
pub const fn new_u16(value: u16) -> Self {
pub const fn from_u16(value: u16) -> Self {
if Self::BITS < 16 {
assert!(value <= Self::MAX.value as u16);
}
Expand All @@ -433,7 +433,7 @@ macro_rules! uint_impl {

/// Creates an instance. Panics if the given value is outside of the valid range
#[inline]
pub const fn new_u32(value: u32) -> Self {
pub const fn from_u32(value: u32) -> Self {
if Self::BITS < 32 {
assert!(value <= Self::MAX.value as u32);
}
Expand All @@ -442,7 +442,7 @@ macro_rules! uint_impl {

/// Creates an instance. Panics if the given value is outside of the valid range
#[inline]
pub const fn new_u64(value: u64) -> Self {
pub const fn from_u64(value: u64) -> Self {
if Self::BITS < 64 {
assert!(value <= Self::MAX.value as u64);
}
Expand All @@ -451,7 +451,7 @@ macro_rules! uint_impl {

/// Creates an instance. Panics if the given value is outside of the valid range
#[inline]
pub const fn new_u128(value: u128) -> Self {
pub const fn from_u128(value: u128) -> Self {
if Self::BITS < 128 {
assert!(value <= Self::MAX.value as u128);
}
Expand Down
28 changes: 14 additions & 14 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2075,11 +2075,11 @@ fn schemars() {
#[test]
fn new_and_as_specific_types() {
let a = u6::new(42);
let b = u6::new_u8(42);
let c = u6::new_u16(42);
let d = u6::new_u32(42);
let e = u6::new_u64(42);
let f = u6::new_u128(42);
let b = u6::from_u8(42);
let c = u6::from_u16(42);
let d = u6::from_u32(42);
let e = u6::from_u64(42);
let f = u6::from_u128(42);

assert_eq!(a.as_u8(), 42);
assert_eq!(a.as_u16(), 42);
Expand All @@ -2098,7 +2098,7 @@ fn new_and_as_specific_types() {
#[test]
fn new_flexible() {
let a = u10::new(1000);
let b = u11::new_(a);
let b = u11::from_(a);

assert_eq!(a.as_u32(), 1000);
assert_eq!(b.as_u32(), 1000);
Expand All @@ -2109,58 +2109,58 @@ fn new_flexible() {
#[should_panic]
fn new_flexible_catches_out_of_bounds() {
let a = u28::new(0x8000000);
let _b = u9::new_(a);
let _b = u9::from_(a);
}

#[cfg(not(feature = "const_convert_and_const_trait_impl"))]
#[test]
#[should_panic]
fn new_flexible_catches_out_of_bounds_2() {
let a = u28::new(0x0000200);
let _b = u9::new_(a);
let _b = u9::from_(a);
}

#[cfg(not(feature = "const_convert_and_const_trait_impl"))]
#[test]
#[should_panic]
fn new_flexible_catches_out_of_bounds_primitive_type() {
let a = u28::new(0x8000000);
let _b = u8::new_(a);
let _b = u8::from_(a);
}

#[cfg(not(feature = "const_convert_and_const_trait_impl"))]
#[test]
#[should_panic]
fn new_constructors_catch_out_bounds_0() {
u7::new_u8(0x80u8);
u7::from_u8(0x80u8);
}

#[cfg(not(feature = "const_convert_and_const_trait_impl"))]
#[test]
#[should_panic]
fn new_constructors_catch_out_bounds_1() {
u7::new_u32(0x80000060u32);
u7::from_u32(0x80000060u32);
}

#[cfg(not(feature = "const_convert_and_const_trait_impl"))]
#[test]
#[should_panic]
fn new_constructors_catch_out_bounds_2() {
u7::new_u16(0x8060u16);
u7::from_u16(0x8060u16);
}

#[cfg(not(feature = "const_convert_and_const_trait_impl"))]
#[test]
#[should_panic]
fn new_constructors_catch_out_bounds_3() {
u7::new_u64(0x80000000_00000060u64);
u7::from_u64(0x80000000_00000060u64);
}

#[cfg(not(feature = "const_convert_and_const_trait_impl"))]
#[test]
#[should_panic]
fn new_constructors_catch_out_bounds_4() {
u7::new_u128(0x80000000_00000000_00000000_00000060u128);
u7::from_u128(0x80000000_00000000_00000000_00000060u128);
}

#[cfg(not(feature = "const_convert_and_const_trait_impl"))]
Expand Down

0 comments on commit f885cba

Please sign in to comment.