Skip to content

Commit 028e4c1

Browse files
committed
Made int into and try_into trait based.
commit-id:c5d44d91
1 parent 5ac1a2e commit 028e4c1

File tree

3 files changed

+38
-116
lines changed

3 files changed

+38
-116
lines changed

corelib/src/integer.cairo

+36-113
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,36 @@ extern fn upcast<FromType, ToType>(x: FromType) -> ToType nopanic;
15421542
// will not lead to Sierra errors.
15431543
extern fn downcast<FromType, ToType>(x: FromType) -> Option<ToType> implicits(RangeCheck) nopanic;
15441544

1545+
// Marks `FromType` as upcastable to `ToType`.
1546+
// Do not add user code implementing this trait.
1547+
trait Upcastable<FromType, ToType>;
1548+
impl UpcastableU8U16 of Upcastable<u8, u16> {}
1549+
impl UpcastableU8U32 of Upcastable<u8, u32> {}
1550+
impl UpcastableU8U64 of Upcastable<u8, u64> {}
1551+
impl UpcastableU8U128 of Upcastable<u8, u128> {}
1552+
impl UpcastableU16U32 of Upcastable<u16, u32> {}
1553+
impl UpcastableU16U64 of Upcastable<u16, u64> {}
1554+
impl UpcastableU16U128 of Upcastable<u16, u128> {}
1555+
impl UpcastableU32U64 of Upcastable<u32, u64> {}
1556+
impl UpcastableU32U128 of Upcastable<u32, u128> {}
1557+
impl UpcastableU64U128 of Upcastable<u64, u128> {}
1558+
// Marks `FromType` as downcastable to `ToType`.
1559+
// Do not add user code implementing this trait.
1560+
trait Downcastable<FromType, ToType>;
1561+
impl DowncastableU128U64 of Downcastable<u128, u64> {}
1562+
impl DowncastableU128U32 of Downcastable<u128, u32> {}
1563+
impl DowncastableU128U16 of Downcastable<u128, u16> {}
1564+
impl DowncastableU128U8 of Downcastable<u128, u8> {}
1565+
1566+
impl DowncastableU64U32 of Downcastable<u64, u32> {}
1567+
impl DowncastableU64U16 of Downcastable<u64, u16> {}
1568+
impl DowncastableU64U8 of Downcastable<u64, u8> {}
1569+
1570+
impl DowncastableU32U16 of Downcastable<u32, u16> {}
1571+
impl DowncastableU32U8 of Downcastable<u32, u8> {}
1572+
1573+
impl DowncastableU16U8 of Downcastable<u16, u8> {}
1574+
15451575
/// Default values
15461576
impl U8Default of Default<u8> {
15471577
#[inline(always)]
@@ -1622,50 +1652,16 @@ impl U128Felt252DictValue of Felt252DictValue<u128> {
16221652
}
16231653
}
16241654

1625-
impl U8IntoU16 of Into<u8, u16> {
1626-
fn into(self: u8) -> u16 {
1627-
upcast(self)
1628-
}
1629-
}
1630-
1631-
impl U16TryIntoU8 of TryInto<u16, u8> {
1632-
fn try_into(self: u16) -> Option<u8> {
1633-
downcast(self)
1634-
}
1635-
}
1636-
1637-
impl U8IntoU32 of Into<u8, u32> {
1638-
fn into(self: u8) -> u32 {
1655+
impl UpcastableInto<From, To, impl FromToUpcastable: Upcastable<From, To>> of Into<From, To> {
1656+
fn into(self: From) -> To {
16391657
upcast(self)
16401658
}
16411659
}
16421660

1643-
impl U32TryIntoU8 of TryInto<u32, u8> {
1644-
fn try_into(self: u32) -> Option<u8> {
1645-
downcast(self)
1646-
}
1647-
}
1648-
1649-
impl U8IntoU64 of Into<u8, u64> {
1650-
fn into(self: u8) -> u64 {
1651-
upcast(self)
1652-
}
1653-
}
1654-
1655-
impl U64TryIntoU8 of TryInto<u64, u8> {
1656-
fn try_into(self: u64) -> Option<u8> {
1657-
downcast(self)
1658-
}
1659-
}
1660-
1661-
impl U8IntoU128 of Into<u8, u128> {
1662-
fn into(self: u8) -> u128 {
1663-
upcast(self)
1664-
}
1665-
}
1666-
1667-
impl U128TryIntoU8 of TryInto<u128, u8> {
1668-
fn try_into(self: u128) -> Option<u8> {
1661+
impl DowncastableTryInto<
1662+
From, To, impl FromToDowncastable: Downcastable<From, To>
1663+
> of TryInto<From, To> {
1664+
fn try_into(self: From) -> Option<To> {
16691665
downcast(self)
16701666
}
16711667
}
@@ -1688,42 +1684,6 @@ impl U256TryIntoU8 of TryInto<u256, u8> {
16881684
}
16891685
}
16901686

1691-
impl U16IntoU32 of Into<u16, u32> {
1692-
fn into(self: u16) -> u32 {
1693-
upcast(self)
1694-
}
1695-
}
1696-
1697-
impl U32TryIntoU16 of TryInto<u32, u16> {
1698-
fn try_into(self: u32) -> Option<u16> {
1699-
downcast(self)
1700-
}
1701-
}
1702-
1703-
impl U16IntoU64 of Into<u16, u64> {
1704-
fn into(self: u16) -> u64 {
1705-
upcast(self)
1706-
}
1707-
}
1708-
1709-
impl U64TryIntoU16 of TryInto<u64, u16> {
1710-
fn try_into(self: u64) -> Option<u16> {
1711-
downcast(self)
1712-
}
1713-
}
1714-
1715-
impl U16IntoU128 of Into<u16, u128> {
1716-
fn into(self: u16) -> u128 {
1717-
upcast(self)
1718-
}
1719-
}
1720-
1721-
impl U128TryIntoU16 of TryInto<u128, u16> {
1722-
fn try_into(self: u128) -> Option<u16> {
1723-
downcast(self)
1724-
}
1725-
}
1726-
17271687
impl U16IntoU256 of Into<u16, u256> {
17281688
fn into(self: u16) -> u256 {
17291689
u256 { low: upcast(self), high: 0_u128 }
@@ -1742,30 +1702,6 @@ impl U256TryIntoU16 of TryInto<u256, u16> {
17421702
}
17431703
}
17441704

1745-
impl U32IntoU64 of Into<u32, u64> {
1746-
fn into(self: u32) -> u64 {
1747-
upcast(self)
1748-
}
1749-
}
1750-
1751-
impl U64TryIntoU32 of TryInto<u64, u32> {
1752-
fn try_into(self: u64) -> Option<u32> {
1753-
downcast(self)
1754-
}
1755-
}
1756-
1757-
impl U32IntoU128 of Into<u32, u128> {
1758-
fn into(self: u32) -> u128 {
1759-
upcast(self)
1760-
}
1761-
}
1762-
1763-
impl U128TryIntoU32 of TryInto<u128, u32> {
1764-
fn try_into(self: u128) -> Option<u32> {
1765-
downcast(self)
1766-
}
1767-
}
1768-
17691705
impl U32IntoU256 of Into<u32, u256> {
17701706
fn into(self: u32) -> u256 {
17711707
u256 { low: upcast(self), high: 0_u128 }
@@ -1784,18 +1720,6 @@ impl U256TryIntoU32 of TryInto<u256, u32> {
17841720
}
17851721
}
17861722

1787-
impl U64IntoU128 of Into<u64, u128> {
1788-
fn into(self: u64) -> u128 {
1789-
upcast(self)
1790-
}
1791-
}
1792-
1793-
impl U128TryIntoU64 of TryInto<u128, u64> {
1794-
fn try_into(self: u128) -> Option<u64> {
1795-
downcast(self)
1796-
}
1797-
}
1798-
17991723
impl U64IntoU256 of Into<u64, u256> {
18001724
fn into(self: u64) -> u256 {
18011725
u256 { low: upcast(self), high: 0_u128 }
@@ -1832,7 +1756,6 @@ impl U256TryIntoU128 of TryInto<u256, u128> {
18321756
}
18331757
}
18341758

1835-
18361759
// === Zeroable ===
18371760

18381761
impl U8Zeroable of Zeroable<u8> {

corelib/src/lib.cairo

+1-2
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,7 @@ use integer::{
254254
u128_const, u128_sqrt, u128_is_zero, u8, u8_const, u16, u16_const, u32, u32_const, u64,
255255
u64_const, u256, u256_sqrt, Felt252TryIntoU8, U8IntoFelt252, Felt252TryIntoU16, U16IntoFelt252,
256256
Felt252TryIntoU32, U32IntoFelt252, Felt252TryIntoU64, U64IntoFelt252, Felt252TryIntoU128,
257-
U128IntoFelt252, U16TryIntoU8, U32TryIntoU16, U64TryIntoU32, U128TryIntoU64, Felt252IntoU256,
258-
Bitwise
257+
U128IntoFelt252, Felt252IntoU256, Bitwise
259258
};
260259

261260
// Math.

crates/cairo-lang-lowering/src/test_data/implicits

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ End:
5656

5757
blk2:
5858
Statements:
59-
(v52: core::RangeCheck, v13: core::option::Option::<core::integer::u64>) <- core::integer::U128TryIntoU64::try_into(v41, v3)
59+
(v52: core::RangeCheck, v13: core::option::Option::<core::integer::u64>) <- core::integer::DowncastableTryInto::<core::integer::u128, core::integer::u64, core::integer::DowncastableU128U64>::try_into(v41, v3)
6060
End:
6161
Match(match_enum(v13) {
6262
Option::Some(v26) => blk3,

0 commit comments

Comments
 (0)