@@ -11,15 +11,15 @@ impl NumericLiteralBoundedInt<
11
11
impl BoundedIntIntoFelt252 <
12
12
const MIN : felt252 , const MAX : felt252 ,
13
13
> of Into <BoundedInt <MIN , MAX >, felt252 > {
14
- fn into (self : BoundedInt <MIN , MAX >) -> felt252 {
14
+ const fn into (self : BoundedInt <MIN , MAX >) -> felt252 {
15
15
upcast (self )
16
16
}
17
17
}
18
18
19
19
impl Felt252TryIntoBoundedInt <
20
20
const MIN : felt252 , const MAX : felt252 ,
21
21
> of TryInto <felt252 , BoundedInt <MIN , MAX >> {
22
- fn try_into (self : felt252 ) -> Option <BoundedInt <MIN , MAX >> {
22
+ const fn try_into (self : felt252 ) -> Option <BoundedInt <MIN , MAX >> {
23
23
// Using `downcast` is allowed, since `BoundedInt` itself is not `pub`, and only has a few
24
24
// specific `pub` instances, such as `u96`, `ConstZero` and `ConstOne`.
25
25
downcast (self )
@@ -113,56 +113,49 @@ extern fn bounded_int_constrain<T, const BOUNDARY: felt252, impl H: ConstrainHel
113
113
value : T ,
114
114
) -> Result <H :: LowT , H :: HighT > implicits (RangeCheck ) nopanic ;
115
115
116
- /// A helper trait for trimming a `BoundedInt` instance.
117
- pub trait TrimHelper <T , const TRIMMED_VALUE : felt252 > {
116
+ /// A helper trait for trimming a `BoundedInt` instance min value.
117
+ pub trait TrimMinHelper <T > {
118
+ type Target ;
119
+ }
120
+ /// A helper trait for trimming a `BoundedInt` instance max value.
121
+ pub trait TrimMaxHelper <T > {
118
122
type Target ;
119
123
}
120
124
mod trim_impl {
121
- pub impl Impl <
122
- T , const TRIMMED_VALUE : felt252 , const MIN : felt252 , const MAX : felt252 ,
123
- > of super :: TrimHelper <T , TRIMMED_VALUE > {
125
+ pub impl Min <T , const MIN : felt252 , const MAX : felt252 > of super :: TrimMinHelper <T > {
126
+ type Target = super :: BoundedInt <MIN , MAX >;
127
+ }
128
+ pub impl Max <T , const MIN : felt252 , const MAX : felt252 > of super :: TrimMaxHelper <T > {
124
129
type Target = super :: BoundedInt <MIN , MAX >;
125
130
}
126
131
}
127
- impl U8TrimBelow = trim_impl :: Impl <u8 , 0 , 1 , 0xff >;
128
- impl U8TrimAbove = trim_impl :: Impl <u8 , 0xff , 0 , 0xfe >;
129
- impl I8TrimBelow = trim_impl :: Impl <i8 , - 0x80 , - 0x7f , 0x7f >;
130
- impl I8TrimAbove = trim_impl :: Impl <i8 , 0x7f , - 0x80 , 0x7e >;
131
- impl U16TrimBelow = trim_impl :: Impl <u16 , 0 , 1 , 0xffff >;
132
- impl U16TrimAbove = trim_impl :: Impl <u16 , 0xffff , 0 , 0xfffe >;
133
- impl I16TrimBelow = trim_impl :: Impl <i16 , - 0x8000 , - 0x7fff , 0x7fff >;
134
- impl I16TrimAbove = trim_impl :: Impl <i16 , 0x7fff , - 0x8000 , 0x7ffe >;
135
- impl U32TrimBelow = trim_impl :: Impl <u32 , 0 , 1 , 0xffffffff >;
136
- impl U32TrimAbove = trim_impl :: Impl <u32 , 0xffffffff , 0 , 0xfffffffe >;
137
- impl I32TrimBelow = trim_impl :: Impl <i32 , - 0x80000000 , - 0x7fffffff , 0x7fffffff >;
138
- impl I32TrimAbove = trim_impl :: Impl <i32 , 0x7fffffff , - 0x80000000 , 0x7ffffffe >;
139
- impl U64TrimBelow = trim_impl :: Impl <u64 , 0 , 1 , 0xffffffffffffffff >;
140
- impl U64TrimAbove = trim_impl :: Impl <u64 , 0xffffffffffffffff , 0 , 0xfffffffffffffffe >;
141
- impl I64TrimBelow =
142
- trim_impl :: Impl <i64 , - 0x8000000000000000 , - 0x7fffffffffffffff , 0x7fffffffffffffff >;
143
- impl I64TrimAbove =
144
- trim_impl :: Impl <i64 , 0x7fffffffffffffff , - 0x8000000000000000 , 0x7ffffffffffffffe >;
145
- impl U128TrimBelow = trim_impl :: Impl <u128 , 0 , 1 , 0xffffffffffffffffffffffffffffffff >;
146
- impl U128TrimAbove =
147
- trim_impl :: Impl <
148
- u128 , 0xffffffffffffffffffffffffffffffff , 0 , 0xfffffffffffffffffffffffffffffffe ,
149
- >;
132
+ impl U8TrimBelow = trim_impl :: Min <u8 , 1 , 0xff >;
133
+ impl U8TrimAbove = trim_impl :: Max <u8 , 0 , 0xfe >;
134
+ impl I8TrimBelow = trim_impl :: Min <i8 , - 0x7f , 0x7f >;
135
+ impl I8TrimAbove = trim_impl :: Max <i8 , - 0x80 , 0x7e >;
136
+ impl U16TrimBelow = trim_impl :: Min <u16 , 1 , 0xffff >;
137
+ impl U16TrimAbove = trim_impl :: Max <u16 , 0 , 0xfffe >;
138
+ impl I16TrimBelow = trim_impl :: Min <i16 , - 0x7fff , 0x7fff >;
139
+ impl I16TrimAbove = trim_impl :: Max <i16 , - 0x8000 , 0x7ffe >;
140
+ impl U32TrimBelow = trim_impl :: Min <u32 , 1 , 0xffffffff >;
141
+ impl U32TrimAbove = trim_impl :: Max <u32 , 0 , 0xfffffffe >;
142
+ impl I32TrimBelow = trim_impl :: Min <i32 , - 0x7fffffff , 0x7fffffff >;
143
+ impl I32TrimAbove = trim_impl :: Max <i32 , - 0x80000000 , 0x7ffffffe >;
144
+ impl U64TrimBelow = trim_impl :: Min <u64 , 1 , 0xffffffffffffffff >;
145
+ impl U64TrimAbove = trim_impl :: Max <u64 , 0 , 0xfffffffffffffffe >;
146
+ impl I64TrimBelow = trim_impl :: Min <i64 , - 0x7fffffffffffffff , 0x7fffffffffffffff >;
147
+ impl I64TrimAbove = trim_impl :: Max <i64 , - 0x8000000000000000 , 0x7ffffffffffffffe >;
148
+ impl U128TrimBelow = trim_impl :: Min <u128 , 1 , 0xffffffffffffffffffffffffffffffff >;
149
+ impl U128TrimAbove = trim_impl :: Max <u128 , 0 , 0xfffffffffffffffffffffffffffffffe >;
150
150
impl I128TrimBelow =
151
- trim_impl :: Impl <
152
- i128 ,
153
- - 0x80000000000000000000000000000000 ,
154
- - 0x7fffffffffffffffffffffffffffffff ,
155
- 0x7fffffffffffffffffffffffffffffff ,
156
- >;
151
+ trim_impl :: Min <i128 , - 0x7fffffffffffffffffffffffffffffff , 0x7fffffffffffffffffffffffffffffff >;
157
152
impl I128TrimAbove =
158
- trim_impl :: Impl <
159
- i128 ,
160
- 0x7fffffffffffffffffffffffffffffff ,
161
- - 0x80000000000000000000000000000000 ,
162
- 0x7ffffffffffffffffffffffffffffffe ,
163
- >;
153
+ trim_impl :: Max <i128 , - 0x80000000000000000000000000000000 , 0x7ffffffffffffffffffffffffffffffe >;
164
154
165
- extern fn bounded_int_trim <T , const TRIMMED_VALUE : felt252 , impl H : TrimHelper <T , TRIMMED_VALUE >>(
155
+ extern fn bounded_int_trim_min <T , impl H : TrimMinHelper <T >>(
156
+ value : T ,
157
+ ) -> core :: internal :: OptionRev <H :: Target > nopanic ;
158
+ extern fn bounded_int_trim_max <T , impl H : TrimMaxHelper <T >>(
166
159
value : T ,
167
160
) -> core :: internal :: OptionRev <H :: Target > nopanic ;
168
161
@@ -272,5 +265,5 @@ impl MulMinusOneNegateHelper<T, impl H: MulHelper<T, MinusOne>> of NegateHelper<
272
265
pub use {
273
266
bounded_int_add as add, bounded_int_constrain as constrain, bounded_int_div_rem as div_rem,
274
267
bounded_int_is_zero as is_zero, bounded_int_mul as mul, bounded_int_sub as sub,
275
- bounded_int_trim as trim ,
268
+ bounded_int_trim_max as trim_max, bounded_int_trim_min as trim_min ,
276
269
};
0 commit comments