Comments (8)
@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.
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.
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.
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.
@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.
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.
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.
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)
- to_float not implemented for float HOT 4
- check_struct_sizes and test_string_index test fails on 32 bit architectures HOT 6
- Add a no_plugin feature HOT 7
- Issue with `CustomType` getters HOT 2
- Customizing "let", syntax
- Customizing "let", "fn" syntax HOT 4
- Object definition syntax HOT 2
- on_var not called for variables in closures HOT 6
- `NativeCallContext` in `export_module` results in a "hidden lifetime parameters in types are deprecated" warning HOT 7
- Pre-compile scripts? HOT 6
- The `sync` feature flag is not compatible with `no_std` HOT 1
- Willingness to offer alternative Map types behind feature flags? HOT 1
- Rhai `no_std` appears to rely on `once_cell/alloc` but does not request it HOT 2
- Could you consider removing the semicolons at the end of the lines HOT 5
- Defining a pipeline operator? HOT 2
- min(f64, f64) function not found HOT 2
- A memory leak was discovered HOT 1
- A stack overflow vulnerability was found HOT 3
- [Question] Is there a way to turn a value with a lifetime into a `Dynamic` HOT 5
- Add `position` to `EvalAltResult::SystemError` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rhai.