-
-
Notifications
You must be signed in to change notification settings - Fork 198
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
It is tricky to figure out how to properly use WithBaseField
generically
#715
Comments
Add WithBaseField example to traits.rs (#715)
Is it actually possible to make It looks like when using fn some_fn<T, C>(gd: &T)
where
T: WithBaseField<Base = C>,
C: Inherits<Node>
{
let base = gd.base();
}
// or
impl<T> some_fn(gd: &T)
where
T: WithBaseField,
<T as GodotClass>::Base: Inherits<Node>,
{
let base = gd.base();
} EDIT: Actually after tinkering a bit more it looks like I got the second pattern to work. The trick was to use an explicit upcast first before using it |
In which context did this come up? Do you not have a |
My use case was: I noticed that I'm copy/pasting the same two functions into many class pub trait RootViewportConvenience {
fn get_root_viewport(&self) -> Gd<Window>;
fn get_root_viewport_size(&self) -> Vector2i;
}
impl<T> RootViewportConvenience for T
where
T: WithBaseField,
<T as GodotClass>::Base: Inherits<Node>,
{
fn get_root_viewport(&self) -> Gd<Window> {
let base = self.base().clone().upcast::<Node>();
base.get_tree()
.expect("Couldn't get tree of node")
.get_root()
.expect("Couldn't get root of tree")
}
fn get_root_viewport_size(&self) -> Vector2i {
self.get_root_viewport().get_size()
}
} Of course this only really makes sense if it works for all classes inheriting from |
I see, so you want to write Btw, you should be able to use |
See this discord thread.
But basically there isn't any explicit documentation stating that in order to call
self.base/base_mut()
, the compiler must know the type ofSelf::Base
. It could be useful to add an example somewhere of how to properly declare the bounds in the generic case.The way to declare the bound in case you want the base to be
Node3D
is:The text was updated successfully, but these errors were encountered: