Module custom_signature

Source
Expand description

§A module for custom signature support.

§Overview

This module allows you to create arbitrary signatures for types and functions in compile time.

§Type signatures

To create the desired type signature, you need to create your own trait with the SIGNATURE constant. Then in the implementation, for the required type, use the macro make_signature

§Example
use std::str::from_utf8;
use evm_coder::{custom_signature::SignatureUnit, make_signature};

// Create trait for our signature
trait SoliditySignature {
    const SIGNATURE: SignatureUnit;

    fn name() -> &'static str {
        from_utf8(&Self::SIGNATURE.data[..Self::SIGNATURE.len]).expect("bad utf-8")
    }
}

// Make signatures for some types
impl SoliditySignature for u8 {
    const SIGNATURE: SignatureUnit = make_signature!(new fixed("uint8"));
}
impl SoliditySignature for u32 {
    const SIGNATURE: SignatureUnit = make_signature!(new fixed("uint32"));
}
impl<T: SoliditySignature> SoliditySignature for Vec<T> {
    const SIGNATURE: SignatureUnit = make_signature!(new nameof(T::SIGNATURE) fixed("[]"));
}
impl<A: SoliditySignature, B: SoliditySignature> SoliditySignature for (A, B) {
    const SIGNATURE: SignatureUnit = make_signature!(new fixed("(") nameof(A::SIGNATURE) fixed(",") nameof(B::SIGNATURE) fixed(")"));
}
impl<A: SoliditySignature> SoliditySignature for (A,) {
    const SIGNATURE: SignatureUnit = make_signature!(new fixed("(") nameof(A::SIGNATURE) fixed(",") shift_left(1) fixed(")"));
}

assert_eq!(u8::name(), "uint8");
assert_eq!(<Vec<u8>>::name(), "uint8[]");
assert_eq!(<(u32, u8)>::name(), "(uint32,uint8)");
§Example
use core::str::from_utf8;
use evm_coder::{custom_signature::SignatureUnit, make_signature};
// Trait for our signature
trait SoliditySignature {
    const SIGNATURE: SignatureUnit;

    fn name() -> &'static str {
        from_utf8(&Self::SIGNATURE.data[..Self::SIGNATURE.len]).expect("bad utf-8")
    }
}

// Make signatures for some types
impl SoliditySignature for u8 {
    const SIGNATURE: SignatureUnit = make_signature!(new fixed("uint8"));
}
impl<T: SoliditySignature> SoliditySignature for Vec<T> {
    const SIGNATURE: SignatureUnit = make_signature!(new nameof(T::SIGNATURE) fixed("[]"));
}

Structs§

SignatureUnit
Storage for the signature or its elements.

Constants§

SIGNATURE_SIZE_LIMIT
The maximum length of the signature.