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

Add equals method to protocol objects in WASM bindings #64

Merged
merged 2 commits into from
Jun 22, 2023

Conversation

piotr-roslaniec
Copy link
Contributor

@piotr-roslaniec piotr-roslaniec commented Jun 21, 2023

Type of PR:

  • Feature

Required reviews:

  • 2

What this does:

  • Adds equals method to WASM bindings
  • Attempts to remove boilerplate code with macros

Issues fixed/closed:

Why it's needed:

  • Enables comparison operations in JavaScript

Notes for reviewers:

  • Uses changes from an unreleased ferveo version
  • Are macros introduced in this PR a good idea? Should I continue working on them (in this PR?). They removed some lines of code but on the other hand, they are not compatible with all protocol objects. For example, generate_to_bytes may only replace some to_bytes methods and so will require more work / another macro to be introduced.
  • No downstream effect on nucypher

Comment on lines +108 to +118
macro_rules! generate_from_bytes {
($struct_name:ident) => {
#[wasm_bindgen]
impl $struct_name {
#[wasm_bindgen(js_name = "fromBytes")]
pub fn from_bytes(bytes: &[u8]) -> Result<$struct_name, Error> {
from_bytes(bytes).map(Self)
}
}
};
}
Copy link
Member

Choose a reason for hiding this comment

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

Looking good! It's not too abstract and is nice encapsulation of boilerplate for bindings.

Comment on lines +290 to +292
generate_equals!(MessageKit);
generate_from_bytes!(MessageKit);
generate_to_bytes!(MessageKit);
Copy link
Member

Choose a reason for hiding this comment

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

This is great - can is be abstracted farther or does that cross some kind of line? 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could put these into generate_common_methods! but I didn't find it as useful.


#[wasm_bindgen(js_name = toBytes)]
pub fn to_bytes(&self) -> Box<[u8]> {
to_bytes(self)
Copy link
Member

Choose a reason for hiding this comment

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

the original to_bytes() here uses self. The macro uses &self. Why the discrepancy?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a typo. I think normally clippy would have caught this, but &self was inside the macro. This isn't an issue since the resulting &&self would be coerced to just &self, but it's worth fixing.

};
}

macro_rules! generate_to_string {
Copy link
Member

@derekpierre derekpierre Jun 21, 2023

Choose a reason for hiding this comment

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

🎸 - nice split of the macros.

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.

4 participants