Skip to main content

ReflectHash

Derive Macro ReflectHash 

Source
#[derive(ReflectHash)]
{
    // Attributes available to this derive:
    #[reflect_hash]
}
Expand description

Derives the ReflectHash trait for structs and enums.

§Hashing Rules

The hash is computed at compile time using Keccak256 with the following rules:

§Enums

The final hash is keccak256(variant_hash_0 || variant_hash_1 || ... || variant_hash_N) where variants are processed in declaration order.

  • Unit variant Transferredkeccak256(b"Transferred")
  • Tuple variant Approved(ActorId, u128)keccak256(b"Approved" || ActorId::HASH || u128::HASH)
  • Named variant Paused { by: ActorId }keccak256(b"Paused" || ActorId::HASH)

§Structs

  • Unit struct struct Empty;keccak256(b"Empty")
  • Tuple struct struct Point(u32, u32);keccak256(b"Point" || u32::HASH || u32::HASH)
  • Named struct struct User { id: u64, name: String }keccak256(b"User" || u64::HASH || String::HASH)

§Examples

use sails_reflect_hash::ReflectHash;

#[derive(ReflectHash)]
struct Transfer {
    from: ActorId,
    to: ActorId,
    amount: u128,
}

#[derive(ReflectHash)]
enum Event {
    Transferred { from: ActorId, to: ActorId },
    Approved(ActorId, u128),
    Paused,
}

§Custom crate path

If sails-reflect-hash is re-exported under a different name, you can specify the crate path using the #[reflect_hash(crate = path)] attribute:

// Local re-export
use sails_reflect_hash as my_hash;

#[derive(ReflectHash)]
#[reflect_hash(crate = my_hash)]
struct MyType {
    field: u32,
}

// Absolute path
#[derive(ReflectHash)]
#[reflect_hash(crate = ::some::other::path::to::hash)]
struct OtherType {
    field: u64,
}