Converting subclass to superclass within Rust #2987
Unanswered
campbellcole
asked this question in
Questions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have three classes implemented:
I am trying to write a static function on the
BaseCard
class which returns a full deck of cards:I know I can't directly return a BaseCard, but I have tried lots of combinations of
Py
,PyRef
,PyAny
, and I just can't seem to do it.Perhaps inevitably, while trying some other things after writing this I managed to solve the issue. I would still like to post this as a discussion because it took me a few hours to figure this out and I only figured it out due to pure luck. Perhaps I'm not reading the documentation very well, but this was not at all intuitive for me. I generalized the approach I am using into a function which does it all in one:
This function does exactly what I was trying to do. The problem I was having with this approach earlier is that I didn't know about the
into_py
function, and was trying to use the return value ofinto_super
, which ended up with me trying to hold a reference past its owners lifetime. I'm still not very clear on how this avoids that, but I am still reading so maybe I will understand better soon.Beta Was this translation helpful? Give feedback.
All reactions