Skip to content

Commit

Permalink
feat: add TryFrom
Browse files Browse the repository at this point in the history
  • Loading branch information
olehmisar committed Mar 5, 2025
1 parent ebf72f8 commit f35c5e6
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Nodash is a utility library for [Noir](https://github.com/noir-lang/noir) langua
Put this into your Nargo.toml.

```toml
nodash = { git = "https://github.com/olehmisar/nodash/", tag = "v0.40.0" }
nodash = { git = "https://github.com/olehmisar/nodash/", tag = "v0.40.1" }
```

## Docs
Expand Down Expand Up @@ -129,6 +129,19 @@ use nodash::str_to_u64;
assert(str_to_u64("02345678912345678912") == 02345678912345678912);
```

### `try_from`

Fail-able conversion.

````rs
use nodash::TryFrom;

assert(u8::try_from(123) == 123);
u8::try_from(256); // runtime error

assert(u128::try_from(123) == 123);
```

### `ord`

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

assert(ord("a") == 97);
```
````

### `ArrayExtensions`

Expand Down
82 changes: 82 additions & 0 deletions src/convert.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
pub trait TryFrom<T> {
/// Convert or fail.
fn try_from(input: T) -> Self;
}

impl TryFrom<Field> for u8 {
fn try_from(input: Field) -> Self {
input.assert_max_bit_size::<8>();
input as u8
}
}

impl TryFrom<Field> for u16 {
fn try_from(input: Field) -> Self {
input.assert_max_bit_size::<16>();
input as u16
}
}

impl TryFrom<Field> for u32 {
fn try_from(input: Field) -> Self {
input.assert_max_bit_size::<32>();
input as u32
}
}

impl TryFrom<Field> for u64 {
fn try_from(input: Field) -> Self {
input.assert_max_bit_size::<64>();
input as u64
}
}

impl TryFrom<Field> for u128 {
fn try_from(input: Field) -> Self {
input.assert_max_bit_size::<128>();
input as u128
}
}

impl TryFrom<Field> for i8 {
fn try_from(input: Field) -> Self {
input.assert_max_bit_size::<8>();
input as i8
}
}

impl TryFrom<Field> for i16 {
fn try_from(input: Field) -> Self {
input.assert_max_bit_size::<16>();
input as i16
}
}

impl TryFrom<Field> for i32 {
fn try_from(input: Field) -> Self {
input.assert_max_bit_size::<32>();
input as i32
}
}

impl TryFrom<Field> for i64 {
fn try_from(input: Field) -> Self {
input.assert_max_bit_size::<64>();
input as i64
}
}

mod tests {
use super::TryFrom;

#[test]
fn test_try_from() {
assert(u8::try_from(123) == 123);
assert(u128::try_from(123) == 123);
}

#[test(should_fail_with = "call to assert_max_bit_size")]
fn test_try_from_fail() {
let _ = u8::try_from(256);
}
}
2 changes: 2 additions & 0 deletions src/lib.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod convert;
mod hash;
mod math;
mod solidity;
Expand All @@ -7,6 +8,7 @@ mod array;
mod validate_inputs;

pub use array::pack_bytes;
pub use convert::TryFrom;
pub use hash::{keccak256, pedersen, poseidon2, sha256};
pub use math::{clamp, div_ceil, sqrt::sqrt};
pub use string::{field_to_hex, ord, str_to_u64, to_hex_string_bytes};
Expand Down

0 comments on commit f35c5e6

Please sign in to comment.