-
-
Notifications
You must be signed in to change notification settings - Fork 200
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
Mimic onready
vars
#130
Comments
That's a nice idea, and it already came up in godot-rust/gdnative#758 (comment) 🙂 A few things to note:
|
An alternative syntax to consider is generalization into a trait (all identifiers subject to bikeshedding): trait Fetch {
fn fetch(root: Gd<Node>, path: &str) -> Result<Self, FetchError>;
}
impl<T: GodotClass + Inherits<Node>> Fetch for Gd<T> {
// snip
}
#[derive(GodotClass)]
#[class(base=Node)]
struct MyStruct {
#[on_ready(fetch("MyChild"))]
my_child: Option<Gd<Node>>,
#[base]
base: Base<Node>
} Which could later open up possibilities of using separate types for sub-trees, for code organization and/or reuse: #[derive(Fetch)]
struct MyChildren {
#[fetch("MyChild")]
my_child: Gd<Node>,
#[fetch("MyChild2")]
my_child_2: Gd<Node>,
#[fetch("MyChild3")]
my_child_3: Gd<Node>,
}
#[derive(GodotClass)]
#[class(base=Node)]
struct MyStruct {
#[on_ready(fetch("."))]
my_child: Option<MyChildren>,
#[base]
base: Base<Node>
} There is some existing discussion about this syntax here, that apart from the safety aspects should also apply to |
I have some experiments ongoing on branch It's indeed a bit tricky, my current approach is a specific Anyway, I'll open a PR once there's something ready (no pun intended). |
Currently the pattern for structs with
Gd
references (which have to be manually initialized inready
) looks like:Then,
map
,unwrap
, or user-defined accessors are needed to bypass theOption
.How about:
where
OnReady
is aMaybeUninit<Gd<T>>
with aDeref
/DerefMut
instance to avoid unwrapping.It certainly clutters the struct definition a bit more, but you avoid:
Option
inready
(which can easily be forgotten)map
or accessors to get the obviously initialized fieldOption<Gd<T>>
's 16 bytes compared toGd<T>
The text was updated successfully, but these errors were encountered: