Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stdlib): Vec type #1905

Merged
merged 18 commits into from
Jul 10, 2023
Merged

feat(stdlib): Vec type #1905

merged 18 commits into from
Jul 10, 2023

Conversation

vezenovm
Copy link
Contributor

Description

This PR utilizes the recent additions of mutable references and slices to create a Vec type.

Problem*

This is work towards resolving #1556

Summary*

I made a new type struct Vec<T> { slice: [T] } with a few builtin funcs that uses our slice builtins to implement a mutable vector type.

If a user wants to update a slice they no longer have to use this syntax:

    let mut slice: [Field] = [];
    for i in 0..5 {
        slice = slice.push_back(i);
    }
    assert(new_slice.len() == 5);

and can instead do this:

    let mut vector: Vec<Field> = Vec::new();
    for i in 0..5 {
        vector.push(i);
    }
    assert(vector.len() == 5);

as the builtin Vec functions use mutable references. The Vec type is essentially a mutable wrapper over slices.

Documentation

  • This PR requires documentation updates when merged.

    • I will submit a noir-lang/docs PR.
    • I will request for and support Dev Rel's help in documenting this PR.

Additional Context

PR Checklist*

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

@jfecher
Copy link
Contributor

jfecher commented Jul 10, 2023

and can instead do this:

let mut vector: Vec<Field> = Vec::new();
for i in 0..5 {
    vector.push(i);
}
assert(vector.len() == 5);

The type annotation is not necessary here, right?

@vezenovm
Copy link
Contributor Author

The type annotation is not necessary here, right?

Yeah it is unneeded I removed

Copy link
Contributor

@jfecher jfecher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Once we fix slices in if statements we can add functions like map as well.

crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs Outdated Show resolved Hide resolved
noir_stdlib/src/collections/vec.nr Outdated Show resolved Hide resolved
noir_stdlib/src/collections/vec.nr Show resolved Hide resolved
@vezenovm vezenovm requested a review from jfecher July 10, 2023 17:38
Copy link
Contributor

@jfecher jfecher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@jfecher jfecher added this pull request to the merge queue Jul 10, 2023
Merged via the queue into master with commit 3734e25 Jul 10, 2023
@jfecher jfecher deleted the mv/vec-type branch July 10, 2023 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants