Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
49 changes: 47 additions & 2 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,30 @@ macro_rules! from_arbitrary_int_impl {

pub(crate) use from_arbitrary_int_impl;

macro_rules! from_native_impl {
macro_rules! from_native_bigger_impl {
($ty:ident($from:ty), [$($into:ty),+]) => {
$(
impl<const BITS: usize> TryFrom<$from> for $ty<$into, BITS> {
type Error = TryNewError;

#[inline]
fn try_from(from: $from) -> Result<Self, Self::Error> {
Self::try_new(from.try_into().map_err(|_| TryNewError)?)
}
}

impl<const BITS: usize> From<$ty<$from, BITS>> for $into {
#[inline]
fn from(from: $ty<$from, BITS>) -> Self {
from.value as $into
}
}
)+
};
}
pub(crate) use from_native_bigger_impl;

macro_rules! from_native_equal_impl {
($ty:ident($from:ty), [$($into:ty),+]) => {
$(
impl<const BITS: usize> From<$from> for $ty<$into, BITS> {
Expand All @@ -67,8 +90,30 @@ macro_rules! from_native_impl {
)+
};
}
pub(crate) use from_native_equal_impl;

macro_rules! from_native_smaller_impl {
($ty:ident($from:ty), [$($into:ty),+]) => {
$(
impl<const BITS: usize> From<$from> for $ty<$into, BITS> {
#[inline]
fn from(from: $from) -> Self {
Self { value: from.into() }
}
}

impl<const BITS: usize> TryFrom<$ty<$from, BITS>> for $into {
type Error = TryNewError;

pub(crate) use from_native_impl;
#[inline]
fn try_from(from: $ty<$from, BITS>) -> Result<Self, Self::Error> {
from.value.try_into().map_err(|_| TryNewError)
}
}
)+
};
}
pub(crate) use from_native_smaller_impl;

macro_rules! impl_extract {
(
Expand Down
27 changes: 20 additions & 7 deletions src/signed.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{
common::{
bytes_operation_impl, from_arbitrary_int_impl, from_native_impl, impl_bin_proto,
impl_extract, impl_num_traits, impl_schemars, impl_step, impl_sum_product,
bytes_operation_impl, from_arbitrary_int_impl, from_native_bigger_impl,
from_native_equal_impl, from_native_smaller_impl, impl_bin_proto, impl_extract,
impl_num_traits, impl_schemars, impl_step, impl_sum_product,
},
traits::{sealed::Sealed, BuiltinInteger, Integer, SignedInteger},
TryNewError,
Expand Down Expand Up @@ -1964,11 +1965,23 @@ from_arbitrary_int_impl!(Int(i32), [i8, i16, i64, i128]);
from_arbitrary_int_impl!(Int(i64), [i8, i16, i32, i128]);
from_arbitrary_int_impl!(Int(i128), [i8, i32, i64, i16]);

from_native_impl!(Int(i8), [i8, i16, i32, i64, i128]);
from_native_impl!(Int(i16), [i8, i16, i32, i64, i128]);
from_native_impl!(Int(i32), [i8, i16, i32, i64, i128]);
from_native_impl!(Int(i64), [i8, i16, i32, i64, i128]);
from_native_impl!(Int(i128), [i8, i16, i32, i64, i128]);
from_native_smaller_impl!(Int(i8), [i16, i32, i64, i128]);
from_native_equal_impl!(Int(i8), [i8]);

from_native_smaller_impl!(Int(i16), [i32, i64, i128]);
from_native_equal_impl!(Int(i16), [i16]);
from_native_bigger_impl!(Int(i16), [i8]);

from_native_smaller_impl!(Int(i32), [i64, i128]);
from_native_equal_impl!(Int(i32), [i32]);
from_native_bigger_impl!(Int(i32), [i8, i16]);

from_native_smaller_impl!(Int(i64), [i128]);
from_native_equal_impl!(Int(i64), [i64]);
from_native_bigger_impl!(Int(i64), [i8, i16, i32]);

from_native_equal_impl!(Int(i128), [i128]);
from_native_bigger_impl!(Int(i128), [i8, i16, i32, i64]);

use crate::common::{impl_borsh, impl_bytemuck_basic};
pub use aliases::*;
Expand Down
69 changes: 62 additions & 7 deletions src/unsigned.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::common::{
bytes_operation_impl, from_arbitrary_int_impl, from_native_impl, impl_bin_proto, impl_borsh,
impl_bytemuck_full, impl_extract, impl_num_traits, impl_schemars, impl_step, impl_sum_product,
bytes_operation_impl, from_arbitrary_int_impl, from_native_bigger_impl, from_native_equal_impl,
from_native_smaller_impl, impl_bin_proto, impl_borsh, impl_bytemuck_full, impl_extract,
impl_num_traits, impl_schemars, impl_step, impl_sum_product,
};
use crate::traits::{sealed::Sealed, BuiltinInteger, Integer, UnsignedInteger};
use crate::TryNewError;
use core::fmt::{Binary, Debug, Display, Formatter, LowerHex, Octal, UpperHex};
use core::num::TryFromIntError;
use core::ops::{
Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
Mul, MulAssign, Not, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
Expand Down Expand Up @@ -1687,11 +1689,64 @@ from_arbitrary_int_impl!(UInt(u32), [u8, u16, u64, u128]);
from_arbitrary_int_impl!(UInt(u64), [u8, u16, u32, u128]);
from_arbitrary_int_impl!(UInt(u128), [u8, u32, u64, u16]);

from_native_impl!(UInt(u8), [u8, u16, u32, u64, u128]);
from_native_impl!(UInt(u16), [u8, u16, u32, u64, u128]);
from_native_impl!(UInt(u32), [u8, u16, u32, u64, u128]);
from_native_impl!(UInt(u64), [u8, u16, u32, u64, u128]);
from_native_impl!(UInt(u128), [u8, u16, u32, u64, u128]);
from_native_smaller_impl!(UInt(u8), [u16, u32, u64, u128]);
from_native_equal_impl!(UInt(u8), [u8]);

from_native_smaller_impl!(UInt(u16), [u32, u64, u128]);
from_native_equal_impl!(UInt(u16), [u16]);
from_native_bigger_impl!(UInt(u16), [u8]);

from_native_smaller_impl!(UInt(u32), [u64, u128]);
from_native_equal_impl!(UInt(u32), [u32]);
from_native_bigger_impl!(UInt(u32), [u8, u16]);

from_native_smaller_impl!(UInt(u64), [u128]);
from_native_equal_impl!(UInt(u64), [u64]);
from_native_bigger_impl!(UInt(u64), [u8, u16, u32]);

from_native_equal_impl!(UInt(u128), [u128]);
from_native_bigger_impl!(UInt(u128), [u8, u16, u32, u64]);

impl<
T: UnsignedInteger + BuiltinInteger + TryFrom<usize, Error = TryFromIntError>,
const BITS: usize,
> TryFrom<usize> for UInt<T, BITS>
where
Self: Integer,
<Self as Integer>::UnderlyingType: TryFrom<usize, Error = TryFromIntError>,
{
type Error = TryFromIntError;

fn try_from(value: usize) -> Result<Self, Self::Error> {
Ok(Self::new(value.try_into()?))
}
}

impl<
T: UnsignedInteger + BuiltinInteger + TryInto<usize, Error = TryFromIntError>,
const BITS: usize,
> TryFrom<UInt<T, BITS>> for usize
{
type Error = TryFromIntError;

fn try_from(value: UInt<T, BITS>) -> Result<Self, Self::Error> {
value.value.try_into()
}
}
// impl<
// T: UnsignedInteger + BuiltinInteger + TryFrom<usize, Error = TryFromIntError>,
// const BITS: usize,
// > TryFrom<UInt<T, BITS>> for usize
// where
// UInt<T, BITS>: Integer,
// <Self as Integer>::UnderlyingType: TryFrom<usize, Error = TryFromIntError>,
// {
// type Error = TryFromIntError;

// fn try_from(value: usize) -> Result<Self, Self::Error> {
// Ok(Self::new(value.try_into()?))
// }
// }

pub use aliases::*;

Expand Down