Giter Site home page Giter Site logo

Comments (8)

schungx avatar schungx commented on June 2, 2024

@MavethGH This is the tracking issue for the derive macro.

In #819 I have further refined it:

  • All the attributes now start with rhai_custom_type_ to avoid collision with other attributes.

  • rhai_custom_type_name is added to remap the field to another name.

  • rhai_custom_type_skip is added to skip a field.

from rhai.

schungx avatar schungx commented on June 2, 2024

This is the example that I intend to put into the Book:

use rhai::{CustomType, TypeBuilder};            // <- necessary imports

#[derive(Debug, Clone)]                         // <- necessary for any custom type
#[derive(CustomType)]                           // <- auto-implement 'CustomType'
struct Foo {
    #[rhai_custom_type_skip]                    // <- not included
    dummy: i64,

    #[rhai_custom_type_readonly]                // <- only auto getter, no setter for 'bar'
    bar: i64,

    #[rhai_custom_type_name("emphasize")]
    baz: bool,                                  // <- auto getter/setter for 'baz'

    #[rhai_custom_type_set(Self::set_hello)]    // <- call custom setter for 'hello'
    hello: String                               // <- auto getter for 'hello'
}

impl Foo {
    pub fn set_hello(&mut self, value: String) {
        self.hello = if self.baz {
            let mut s = self.hello.clone();
            s.push_str(&value);
            for _ in 0..self.bar { s.push('!'); }
            s
        } else {
            value
        };
    }
}

from rhai.

schungx avatar schungx commented on June 2, 2024

Actually, I dislike how all that rhai_custom_type_ prefix is so long and cumbersome...

It might be better (or not) if we can do this:

#[derive(CustomType)]
struct Foo {
    #[rhai_type(skip)]
    dummy: i64,

    #[rhai_type(readonly)]
    bar: i64,

    #[rhai_type(name="emphasize")]
    baz: bool,

    #[rhai_type(set=Self::set_hello)]
    hello: String
}

from rhai.

schungx avatar schungx commented on June 2, 2024

In the latest drop I added support for tuple structs -- essentially the same code just tweaked.

Also, the type name is used for pretty-print. That should be slightly better than the default which is the type's full path name.

Docs are being added to the Book.

from rhai.

schungx avatar schungx commented on June 2, 2024

@MavethGH , the latest drop is now pretty complete.

I have decided to call all the attributes rhai_type_* which should not be too long, but long enough to avoid collisions with other traits.

rhai_type_name can be put on the type or any field to rename them.

rhai_type_extra can specify functions to call (with the TypeBuilder) where you can add new functions.

Also, rhai_type_get now expects a regular getter method with &self, while rhai_type_get_mut takes the standard Rhai getter method with &mut self.

The complete set of attributes are now:

Attribute Applies to Parameter Description
rhai_type_name type, field string expression use this name instead of the type/field name.
rhai_type_skip field none skip this field.
rhai_type_readonly field none only auto-generate getter, no setter.
rhai_type_get field function path use this getter function (with &self) instead of the auto-generated getter.
rhai_type_get_mut field function path use this getter function (with &mut self) instead of the auto-generated getter.
rhai_type_set field function path use this setter function instead of the auto-generated setter.
rhai_type_extra type function path call this function after building the type to add additional API's

from rhai.

schungx avatar schungx commented on June 2, 2024

In the end, I used a single attribute rhai_type with option fields to make it consistent with plugin module macros.

#[rhai_type(name = "foo", get = Self::get_foo, readonly)]

from rhai.

schungx avatar schungx commented on June 2, 2024

I'm leaving the last task, auto-derive enums, unimplemented for now.

I'm not sure a derived API is the best way to expose enums in a dynamic scripting language... since every value in Rhai is an unlimited enum.

from rhai.

schungx avatar schungx commented on June 2, 2024

I'll close off this issue now as I feel the feature is reasonably complete. Thanks for starting this @MavethGH !

In fact, the derive macro will be featured in the Book as the preferred method of registering the API of a type.

from rhai.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.