Skip to content

Commit f35c5e6

Browse files
committed
feat: add TryFrom
1 parent ebf72f8 commit f35c5e6

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Nodash is a utility library for [Noir](https://github.com/noir-lang/noir) langua
77
Put this into your Nargo.toml.
88

99
```toml
10-
nodash = { git = "https://github.com/olehmisar/nodash/", tag = "v0.40.0" }
10+
nodash = { git = "https://github.com/olehmisar/nodash/", tag = "v0.40.1" }
1111
```
1212

1313
## Docs
@@ -129,6 +129,19 @@ use nodash::str_to_u64;
129129
assert(str_to_u64("02345678912345678912") == 02345678912345678912);
130130
```
131131

132+
### `try_from`
133+
134+
Fail-able conversion.
135+
136+
````rs
137+
use nodash::TryFrom;
138+
139+
assert(u8::try_from(123) == 123);
140+
u8::try_from(256); // runtime error
141+
142+
assert(u128::try_from(123) == 123);
143+
```
144+
132145
### `ord`
133146

134147
Returns the ASCII code of a single character.
@@ -137,7 +150,7 @@ Returns the ASCII code of a single character.
137150
use nodash::ord;
138151

139152
assert(ord("a") == 97);
140-
```
153+
````
141154

142155
### `ArrayExtensions`
143156

src/convert.nr

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
pub trait TryFrom<T> {
2+
/// Convert or fail.
3+
fn try_from(input: T) -> Self;
4+
}
5+
6+
impl TryFrom<Field> for u8 {
7+
fn try_from(input: Field) -> Self {
8+
input.assert_max_bit_size::<8>();
9+
input as u8
10+
}
11+
}
12+
13+
impl TryFrom<Field> for u16 {
14+
fn try_from(input: Field) -> Self {
15+
input.assert_max_bit_size::<16>();
16+
input as u16
17+
}
18+
}
19+
20+
impl TryFrom<Field> for u32 {
21+
fn try_from(input: Field) -> Self {
22+
input.assert_max_bit_size::<32>();
23+
input as u32
24+
}
25+
}
26+
27+
impl TryFrom<Field> for u64 {
28+
fn try_from(input: Field) -> Self {
29+
input.assert_max_bit_size::<64>();
30+
input as u64
31+
}
32+
}
33+
34+
impl TryFrom<Field> for u128 {
35+
fn try_from(input: Field) -> Self {
36+
input.assert_max_bit_size::<128>();
37+
input as u128
38+
}
39+
}
40+
41+
impl TryFrom<Field> for i8 {
42+
fn try_from(input: Field) -> Self {
43+
input.assert_max_bit_size::<8>();
44+
input as i8
45+
}
46+
}
47+
48+
impl TryFrom<Field> for i16 {
49+
fn try_from(input: Field) -> Self {
50+
input.assert_max_bit_size::<16>();
51+
input as i16
52+
}
53+
}
54+
55+
impl TryFrom<Field> for i32 {
56+
fn try_from(input: Field) -> Self {
57+
input.assert_max_bit_size::<32>();
58+
input as i32
59+
}
60+
}
61+
62+
impl TryFrom<Field> for i64 {
63+
fn try_from(input: Field) -> Self {
64+
input.assert_max_bit_size::<64>();
65+
input as i64
66+
}
67+
}
68+
69+
mod tests {
70+
use super::TryFrom;
71+
72+
#[test]
73+
fn test_try_from() {
74+
assert(u8::try_from(123) == 123);
75+
assert(u128::try_from(123) == 123);
76+
}
77+
78+
#[test(should_fail_with = "call to assert_max_bit_size")]
79+
fn test_try_from_fail() {
80+
let _ = u8::try_from(256);
81+
}
82+
}

src/lib.nr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod convert;
12
mod hash;
23
mod math;
34
mod solidity;
@@ -7,6 +8,7 @@ mod array;
78
mod validate_inputs;
89

910
pub use array::pack_bytes;
11+
pub use convert::TryFrom;
1012
pub use hash::{keccak256, pedersen, poseidon2, sha256};
1113
pub use math::{clamp, div_ceil, sqrt::sqrt};
1214
pub use string::{field_to_hex, ord, str_to_u64, to_hex_string_bytes};

0 commit comments

Comments
 (0)