Skip to content

Commit

Permalink
WASM: use a macro to simplify the definition of binding types
Browse files Browse the repository at this point in the history
This reduces the amount of repeated code. Also, it adds some conversions
that were previously not implemented.
  • Loading branch information
andiflabs committed Nov 20, 2024
1 parent 7a936ee commit 0dded2d
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 265 deletions.
37 changes: 8 additions & 29 deletions ironfish-rust-wasm/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ use crate::{
errors::IronfishError,
keys::PublicAddress,
primitives::{ExtendedPoint, SubgroupPoint},
wasm_bindgen_wrapper,
};
use ironfish::errors::IronfishErrorKind;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Asset(ironfish::assets::asset::Asset);
wasm_bindgen_wrapper! {
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Asset(ironfish::assets::asset::Asset);
}

#[wasm_bindgen]
impl Asset {
Expand Down Expand Up @@ -99,22 +101,11 @@ impl Asset {
}
}

impl From<ironfish::assets::asset::Asset> for Asset {
fn from(d: ironfish::assets::asset::Asset) -> Self {
Self(d)
}
wasm_bindgen_wrapper! {
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct AssetIdentifier(ironfish::assets::asset_identifier::AssetIdentifier);
}

impl AsRef<ironfish::assets::asset::Asset> for Asset {
fn as_ref(&self) -> &ironfish::assets::asset::Asset {
&self.0
}
}

#[wasm_bindgen]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct AssetIdentifier(ironfish::assets::asset_identifier::AssetIdentifier);

#[wasm_bindgen]
impl AssetIdentifier {
#[wasm_bindgen(constructor)]
Expand All @@ -140,18 +131,6 @@ impl AssetIdentifier {
}
}

impl From<ironfish::assets::asset_identifier::AssetIdentifier> for AssetIdentifier {
fn from(d: ironfish::assets::asset_identifier::AssetIdentifier) -> Self {
Self(d)
}
}

impl AsRef<ironfish::assets::asset_identifier::AssetIdentifier> for AssetIdentifier {
fn as_ref(&self) -> &ironfish::assets::asset_identifier::AssetIdentifier {
&self.0
}
}

#[cfg(test)]
mod tests {
mod asset {
Expand Down
21 changes: 5 additions & 16 deletions ironfish-rust-wasm/src/keys/public_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::errors::IronfishError;
use crate::{errors::IronfishError, wasm_bindgen_wrapper};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct PublicAddress(ironfish::PublicAddress);
wasm_bindgen_wrapper! {
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct PublicAddress(ironfish::PublicAddress);
}

#[wasm_bindgen]
impl PublicAddress {
Expand All @@ -32,18 +33,6 @@ impl PublicAddress {
}
}

impl From<ironfish::PublicAddress> for PublicAddress {
fn from(d: ironfish::PublicAddress) -> Self {
Self(d)
}
}

impl AsRef<ironfish::PublicAddress> for PublicAddress {
fn as_ref(&self) -> &ironfish::PublicAddress {
&self.0
}
}

#[cfg(test)]
mod tests {
use crate::keys::public_address::PublicAddress;
Expand Down
60 changes: 60 additions & 0 deletions ironfish-rust-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,66 @@ pub mod merkle_note;
pub mod primitives;
pub mod transaction;

/// Creates a [`wasm_bindgen`] wrapper for an existing type.
///
/// This macro can be invoked as follows:
///
/// ```
/// wasm_bindgen_wrapper! {
/// #[derive(Clone, Debug)]
/// pub struct FooBinding(Foo);
/// }
/// ```
///
/// and expands to the following:
///
/// ```
/// #[wasm_bindgen]
/// #[derive(Clone, Debug)]
/// pub struct FooBinding(Foo);
///
/// impl From<Foo> for FooBinding { ... }
/// impl From<FooBinding> for Foo { ... }
/// impl AsRef<Foo> for FooBinding { ... }
/// impl Borrow<Foo> for FooBinding { ... }
/// ```
macro_rules! wasm_bindgen_wrapper {
($(
$( #[ $meta:meta ] )*
$vis:vis struct $name:ident ( $inner:ty ) ;
)*) => {$(
$(#[$meta])*
#[::wasm_bindgen::prelude::wasm_bindgen]
$vis struct $name($inner);

impl ::std::convert::From<$inner> for $name {
fn from(x: $inner) -> Self {
Self(x)
}
}

impl ::std::convert::From<$name> for $inner {
fn from(x: $name) -> Self {
x.0
}
}

impl ::std::convert::AsRef<$inner> for $name {
fn as_ref(&self) -> &$inner {
&self.0
}
}

impl ::std::borrow::Borrow<$inner> for $name {
fn borrow(&self) -> &$inner {
&self.0
}
}
)*}
}

use wasm_bindgen_wrapper;

#[cfg(test)]
mod tests {
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
Expand Down
38 changes: 8 additions & 30 deletions ironfish-rust-wasm/src/merkle_note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use crate::{errors::IronfishError, primitives::Scalar};
use crate::{errors::IronfishError, primitives::Scalar, wasm_bindgen_wrapper};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct MerkleNote(ironfish::MerkleNote);
wasm_bindgen_wrapper! {
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct MerkleNote(ironfish::MerkleNote);
}

#[wasm_bindgen]
impl MerkleNote {
Expand All @@ -31,22 +32,11 @@ impl MerkleNote {
}
}

impl From<ironfish::MerkleNote> for MerkleNote {
fn from(d: ironfish::MerkleNote) -> Self {
Self(d)
}
wasm_bindgen_wrapper! {
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct MerkleNoteHash(ironfish::MerkleNoteHash);
}

impl AsRef<ironfish::MerkleNote> for MerkleNote {
fn as_ref(&self) -> &ironfish::MerkleNote {
&self.0
}
}

#[wasm_bindgen]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct MerkleNoteHash(ironfish::MerkleNoteHash);

#[wasm_bindgen]
impl MerkleNoteHash {
#[wasm_bindgen(constructor)]
Expand Down Expand Up @@ -80,18 +70,6 @@ impl MerkleNoteHash {
}
}

impl From<ironfish::MerkleNoteHash> for MerkleNoteHash {
fn from(d: ironfish::MerkleNoteHash) -> Self {
Self(d)
}
}

impl AsRef<ironfish::MerkleNoteHash> for MerkleNoteHash {
fn as_ref(&self) -> &ironfish::MerkleNoteHash {
&self.0
}
}

#[cfg(test)]
mod tests {
use crate::merkle_note::MerkleNoteHash;
Expand Down
Loading

0 comments on commit 0dded2d

Please sign in to comment.