Skip to content

Bindings source code conventions

Daniel Zduniak edited this page Mar 9, 2023 · 11 revisions

When modifying bindings source code, please stick to the following conventions.

Use simple tuple structs:

This:

pub struct ZcashDiversifier(Diversifier);

NOT this:

pub struct ZcashDiversifier {
    inner: Diversifier,
}

Import and use name directly

When possible (no type names clashing):

use zcash_primitives::legacy::TransparentAddress;

// ...

pub struct ZcashTransparentAddress(TransparentAddress);

Instead of using longer paths like:

pub struct ZcashTransparentAddress(zcash_primitives::legacy::TransparentAddress);

Constructors

Default constructors

Constructor named new in librustzcash - simply use a constructor in .udl.

Named constructors

Constructor with a name different than new use Name={constructor name} attribute in .udl:

interface ZcashOrchardDiversifier {
  [Name=from_bytes, Throws=ZcashError]
  constructor(sequence<u8> bytes);
};

Unless it's the only constructor - then you may consider simply using new in bindings Rust code and constructor without Name attribute in .udl.

Constructors returning optional

Constructors returning Option - use Result in bindings and ZcashError in .udl (chose or create a new error that seems the most appropriate) - uniffi-rs does not support constructors returning optional types.

interface ZcashAccountPrivKey {

// ...

  [Name=from_bytes, Throws=ZcashError]
  constructor(sequence<u8> data);
Clone this wiki locally