Trait CustomType

pub trait CustomType: Variant + Clone {
    // Required method
    fn build(builder: TypeBuilder<'_, Self>);
}
Expand description

Trait to build the API of a custom type for use with an [Engine] (i.e. register the type and its getters, setters, methods, etc.).

§Example

use rhai::{CustomType, TypeBuilder, Engine};

#[derive(Debug, Clone, Eq, PartialEq)]
struct TestStruct {
    field: i64
}

impl TestStruct {
    fn new() -> Self {
        Self { field: 1 }
    }
    fn update(&mut self, offset: i64) {
        self.field += offset;
    }
    fn get_value(&mut self) -> i64 {
        self.field
    }
    fn set_value(&mut self, value: i64) {
       self.field = value;
    }
}

impl CustomType for TestStruct {
    fn build(mut builder: TypeBuilder<Self>) {
        builder
            // Register pretty-print name of the type
            .with_name("TestStruct")
            // Register display functions
            .on_print(|v| format!("TestStruct({})", v.field))
            .on_debug(|v| format!("{v:?}"))
            // Register a constructor function
            .with_fn("new_ts", Self::new)
            // Register the 'update' method
            .with_fn("update", Self::update)
            // Register the 'value' property
            .with_get_set("value", Self::get_value, Self::set_value);
    }
}

let mut engine = Engine::new();

// Register API for the custom type.
engine.build_type::<TestStruct>();

assert_eq!(
    engine.eval::<TestStruct>("let x = new_ts(); x.update(41); print(x); x")?,
    TestStruct { field: 42 }
);

Required Methods§

fn build(builder: TypeBuilder<'_, Self>)

Builds the custom type for use with the [Engine].

Methods, property getters/setters, indexers etc. should be registered in this function.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

§

impl CustomType for AbilityIdsVec

§

fn build(builder: TypeBuilder<'_, AbilityIdsVec>)

§

impl CustomType for AbilityShardsVec

§

fn build(builder: TypeBuilder<'_, AbilityShardsVec>)

§

impl CustomType for ConditionalProgress

§

fn build(builder: TypeBuilder<'_, ConditionalProgress>)

§

impl CustomType for CurrenciesVec

§

fn build(builder: TypeBuilder<'_, CurrenciesVec>)

§

impl CustomType for ESAbilityShard

§

fn build(builder: TypeBuilder<'_, ESAbilityShard>)

§

impl CustomType for ESCurrencyUnit

§

fn build(builder: TypeBuilder<'_, ESCurrencyUnit>)

§

impl CustomType for ESEntityAttribute

§

fn build(builder: TypeBuilder<'_, ESEntityAttribute>)

§

impl CustomType for EntityAttributesVec

§

fn build(builder: TypeBuilder<'_, EntityAttributesVec>)

§

impl CustomType for ItemIdsVec

§

fn build(builder: TypeBuilder<'_, ItemIdsVec>)

§

impl CustomType for ScriptRandom

§

fn build(builder: TypeBuilder<'_, ScriptRandom>)

§

impl<E> CustomType for EventVec<E>
where E: Event + EventRhaiEnum,

§

fn build(builder: TypeBuilder<'_, EventVec<E>>)

Implementors§

Source§

impl CustomType for FightClass

Source§

impl CustomType for EntityTeam

Source§

impl CustomType for ItemType

Source§

impl CustomType for QuestGroupType

Source§

impl CustomType for RatingType

Source§

impl CustomType for VassalTaskStatus

Source§

impl CustomType for Ability

Source§

impl CustomType for AbilityDrop

Source§

impl CustomType for AbilityRarity

Source§

impl CustomType for AbilityTemplate

Source§

impl CustomType for ActiveAbility

Source§

impl CustomType for EquippedAbilities

Source§

impl CustomType for UpgradedAbilitiesMap

Source§

impl CustomType for Arena

Source§

impl CustomType for ArenaStars

Source§

impl CustomType for ArenaTickets

Source§

impl CustomType for League

Source§

impl CustomType for Match

Source§

impl CustomType for RankingItem

Source§

impl CustomType for Bot

Source§

impl CustomType for BundleStep

Source§

impl CustomType for CharacterSettings

Source§

impl CustomType for CharacterState

Source§

impl CustomType for Character

Source§

impl CustomType for Class

Source§

impl CustomType for ClassTransition

Source§

impl CustomType for Currency

Source§

impl CustomType for CurrencyUnit

Source§

impl CustomType for Effect

Source§

impl CustomType for Coordinates

Source§

impl CustomType for Entity

Source§

impl CustomType for EntityAttributes

Source§

impl CustomType for ActiveDungeon

Source§

impl CustomType for ActiveFight

Source§

impl CustomType for ActivePetAbility

Source§

impl CustomType for FightTemplate

Source§

impl CustomType for Chapter

Source§

impl CustomType for EnemyReward

Source§

impl CustomType for EntityAttribute

Source§

impl CustomType for EntityTemplate

Source§

impl CustomType for Gift

Source§

impl CustomType for InitParams

Source§

impl CustomType for Attribute

Source§

impl CustomType for Item

Source§

impl CustomType for ItemAttribute

Source§

impl CustomType for ItemRarity

Source§

impl CustomType for Mail

Source§

impl CustomType for OpponentPreview

Source§

impl CustomType for RatingCalculationData

Source§

impl CustomType for PartyMemberPreview

Source§

impl CustomType for EquippedPets

Source§

impl CustomType for Pet

Source§

impl CustomType for PetComputedSecondaryStat

Source§

impl CustomType for PetDrop

Source§

impl CustomType for PetRarity

Source§

impl CustomType for PetSecondaryStat

Source§

impl CustomType for PetTemplate

Source§

impl CustomType for UpgradedPetsMap

Source§

impl CustomType for MatchmakingData

Source§

impl CustomType for MatchmakingOpponent

Source§

impl CustomType for PVPState

Source§

impl CustomType for RatingChange

Source§

impl CustomType for QuestInstance

Source§

impl CustomType for QuestTemplate

Source§

impl CustomType for QuestsGroups

Source§

impl CustomType for QuestsProgressTrack

Source§

impl CustomType for QuestsProgressionGroup

Source§

impl CustomType for QuestsTrackReward

Source§

impl CustomType for RatingRankingItem

Source§

impl CustomType for Patron

Source§

impl CustomType for ShareLinkParam

Source§

impl CustomType for CharacterSkins

Source§

impl CustomType for ConfigSkin

Source§

impl CustomType for SkinsSettings

Source§

impl CustomType for StatueSlotsMap

Source§

impl CustomType for TalentLevelsMap

Source§

impl CustomType for CustomValuesMap

Source§

impl CustomType for User

Source§

impl CustomType for Suzerain

Source§

impl CustomType for Vassal

Source§

impl CustomType for VassalTask