-
Notifications
You must be signed in to change notification settings - Fork 189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tracking issue for CustomType derive macro #820
Comments
@MavethGH This is the tracking issue for the derive macro. In #819 I have further refined it:
|
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
};
}
} |
Actually, I dislike how all that 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
} |
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. |
@MavethGH , the latest drop is now pretty complete. I have decided to call all the attributes
Also, The complete set of attributes are now:
|
In the end, I used a single attribute #[rhai_type(name = "foo", get = Self::get_foo, readonly)] |
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. |
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. |
PR: https://github.com/rhaiscript/rhai/pull/817
TypeBuilder
passed infield0
,field1
, etc perhaps?rhai_custom_type_name
to name fields?The text was updated successfully, but these errors were encountered: