Skip to content

Commit

Permalink
Remove serialize example, it requires too much setup with the '..' pl…
Browse files Browse the repository at this point in the history
…aceholders
  • Loading branch information
jfecher committed Sep 6, 2024
1 parent bcc513e commit fd72d65
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
26 changes: 0 additions & 26 deletions compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3423,32 +3423,6 @@ fn errors_on_unused_function() {
assert_eq!(*item_type, "function");
}

#[test]
fn arithmetic_generics_example() {
let src = r#"
// docs:start:arithmetic-generics
trait Serialize<let N: u32> {
fn serialize(self) -> [Field; N];
}
impl Serialize<1> for Field { .. }
impl<T, let N: u32, M: u32> Serialize<N * M> for [T; N]
where T: Serialize<M> { .. }
impl<T, U, let N: u32, M: u32> Serialize<N + M> for (T, U)
where T: Serialize<N>, U: Serialize<M> { .. }
fn main() {
let data = (1, [2, 3, 4]);
assert(data.serialize().len(), 4);
}
// docs:end:arithmetic-generics
"#;

assert_no_errors(src);
}

#[test]
fn constrained_reference_to_unconstrained() {
let src = r#"
Expand Down
23 changes: 22 additions & 1 deletion docs/docs/noir/concepts/generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,28 @@ apply the distributive law and thus sees these as different types.

Even with this limitation though, the compiler can handle common cases decently well:

#include_code arithmetic-generics compiler/noirc_frontend/src/tests.rs rust
```rust
trait Serialize<let N: u32> {
fn serialize(self) -> [Field; N];
}

impl Serialize<1> for Field {
fn serialize(self) -> [Field; 1] {
[self]
}
}

impl<T, let N: u32, M: u32> Serialize<N * M> for [T; N]
where T: Serialize<M> { .. }

impl<T, U, let N: u32, M: u32> Serialize<N + M> for (T, U)
where T: Serialize<N>, U: Serialize<M> { .. }

fn main() {
let data = (1, [2, 3, 4]);
assert(data.serialize().len(), 4);
}
```

Note that if there is any over or underflow the types will fail to unify:

Expand Down

0 comments on commit fd72d65

Please sign in to comment.