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

Implement From to convert abi type into Vec<FieldElement> #21

Open
glihm opened this issue Mar 8, 2024 · 3 comments
Open

Implement From to convert abi type into Vec<FieldElement> #21

glihm opened this issue Mar 8, 2024 · 3 comments
Assignees
Labels
good first issue Good for newcomers

Comments

@glihm
Copy link
Collaborator

glihm commented Mar 8, 2024

Currently, this is the way to serialize data into felts:

let m = MyData {
    name: felt!("0x1234").into(),
    class_hash: felt!("0x5555").into(),
};

let felts = MyData::cairo_serialize(&m);                                                                                                                                         

To improve the Rustiness of this, we may want to do:

let m = MyData {
    name: felt!("0x1234").into(),
    class_hash: felt!("0x5555").into(),
};

let felts = m.into();                                                                                                                                         

This issue aims at tracking the implementation (which should be possible) of such pattern.

@glihm glihm added the good first issue Good for newcomers label Mar 8, 2024
@edisontim
Copy link

Hey would love to work on this :)

@edisontim
Copy link

I'm just going on a naive approach here, but would something like this fit this pattern?

pub trait IntoFelt {
    fn into_felts(self) -> Vec<Felt>;
}

impl<T: CairoSerde<RustType = T>> IntoFelt for T {
    fn into_felts(self) -> Vec<Felt> {
        T::cairo_serialize(&self)
    }
}

The problem with implementing the From trait directly is that we cannot implement From for Vec because of the orphan rule.

Another idea I had was making cairo_serialize
fn cairo_serialize(&self) -> Vec<Felt>;

@glihm
Copy link
Collaborator Author

glihm commented Nov 11, 2024

Hum, the idea here would be to add this in the generated code for each type.
Doing so, you can implement From without any issue, without the orphan rule being an issue.

So for each struct/enum generated, being able to convert it from a Vec<Felt>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants