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

Trait bound struct implementations #3954

Closed
sirasistant opened this issue Jan 4, 2024 · 1 comment
Closed

Trait bound struct implementations #3954

sirasistant opened this issue Jan 4, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@sirasistant
Copy link
Contributor

Problem

While trying to make Aztec's Map use a trait for keys, we want to bind that the key type implements the trait ToField.
In Rust, you could do something like this:

use std::marker::PhantomData;

struct Map<K, V> {
    items: Vec<V>,
    phantom: PhantomData<K>,
}

impl<K, V> Map<K, V>
where
    K: Into<usize>,
{
    fn new() -> Self {
        Self {
            items: Vec::new(),
            phantom: PhantomData,
        }
    }

    fn at(&self, index: K) -> &V {
        let index = index.into();
        &self.items[index]
    }
}

fn main() {
    let _map: Map<u128, u128> = Map::new(); <= complains because u128 is not Into<usize>
}

Where you'd trait bind your impl so a map could never be created with a key type that cannot be converted to usize.

In noir, trait binding impls is not working:

trait ToField {
    fn to_field() -> Field;
}

struct Map<K,V> {
    key: K, // Just an example
    value: V, // Just an example
}

impl<K,V> Map<K,V> where K: ToField { <== fails
    fn new(key: K, value: V) -> Self {
        Self { key, value }
    }
}

Happy Case

Trait binding impls is possible

Alternatives Considered

You can always make the specific function that needs the IntoField generic but then you'd be able to use a map with different key types, which sounds like a footgun for users.

Additional Context

No response

Would you like to submit a PR for this Issue?

No

Support Needs

No response

@sirasistant sirasistant added the enhancement New feature or request label Jan 4, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Jan 4, 2024
Thunkar added a commit to AztecProtocol/aztec-packages that referenced this issue Jan 12, 2024
Closes #2787

Unfortunately key type with a `ToField` trait binding is only defined in
the `.at` method, due to this blocker:

noir-lang/noir#3954
AztecBot pushed a commit that referenced this issue Jan 12, 2024
Closes AztecProtocol/aztec-packages#2787

Unfortunately key type with a `ToField` trait binding is only defined in
the `.at` method, due to this blocker:

#3954
@kevaundray kevaundray added this to the 1.0 milestone Jan 15, 2024
AztecBot pushed a commit to AztecProtocol/aztec-nr that referenced this issue Jan 18, 2024
Closes AztecProtocol/aztec-packages#2787

Unfortunately key type with a `ToField` trait binding is only defined in
the `.at` method, due to this blocker:

noir-lang/noir#3954
michaelelliot pushed a commit to Swoir/noir_rs that referenced this issue Feb 28, 2024
Closes AztecProtocol#2787

Unfortunately key type with a `ToField` trait binding is only defined in
the `.at` method, due to this blocker:

noir-lang/noir#3954
superstar0402 added a commit to superstar0402/aztec-nr that referenced this issue Aug 16, 2024
Closes AztecProtocol/aztec-packages#2787

Unfortunately key type with a `ToField` trait binding is only defined in
the `.at` method, due to this blocker:

noir-lang/noir#3954
@asterite
Copy link
Collaborator

asterite commented Nov 4, 2024

It seems the second code snippet complies fine. Maybe this started working once we started checking trait where clauses. But please reopen if this is not the case!

@asterite asterite closed this as completed Nov 4, 2024
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Archived in project
Development

No branches or pull requests

4 participants