GithubHelp home page GithubHelp logo

Comments (5)

mlange-42 avatar mlange-42 commented on May 27, 2024

@adamreichold Any idea how to solve that? I was hoping to get the result with the lifetime of owner. Thanks!

from kohonen.

adamreichold avatar adamreichold commented on May 27, 2024

Any idea how to solve that? I was hoping to get the result with the lifetime of owner. Thanks!

I think the first attempt should be to drop the lifetime all together and pass owner by value. It already is a pointer to an object whose life time is managed by Godot. (Which is the reason why all actual accesses are unsafe.)

The second observation would be that you probably have to pass the full Instance<Kohonen> around as the Godot object part of it controls the life time even though you are only interested in the native class part.

(More specifically, the signature fn Instance::map<F, U>(&self, op: F) with F: for<'a>: FnOnce(&'a T, <T as NativeClass>::Base) -> U informs you that you loose any control over the lifetime of the &'a T pass to you and hence its scope is effectively limited to your calling scope.)

Long story short: Rust lifetimes will probably not help much when interacting with objects managed by Godots runtime are they are currently exposed which explains the huge amount of unsafe functions in its API.

from kohonen.

adamreichold avatar adamreichold commented on May 27, 2024

Meaning that your signature should probably just be fn get_kohonen(owner: gdnative::Node2D) -> Option<Instance<Kohonen>>. (I don't think you actually need &self here?)

from kohonen.

mlange-42 avatar mlange-42 commented on May 27, 2024

@adamreichold Thanks!
So far, I solved it by a function taking a closure, so I don't need to care for lifetimes:

trait KohonenUser2D {
    fn with_kohonen<F>(mut owner: gdnative::Control, path: &str, mut fun: F)
    where
        F: FnMut(&mut gdnative::Control, &Kohonen),
    {
        let node = unsafe { owner.get_node(NodePath::from_str(path)) };
        node.and_then(|node| {
            Instance::<Kohonen>::try_from_base(node)
                .map(|inst| inst.map(|koh, _| fun(&mut owner, koh)))
        });
    }
}

from kohonen.

adamreichold avatar adamreichold commented on May 27, 2024

@adamreichold Thanks!
So far, I solved it by a function taking a closure, so I don't need to care for lifetimes:

Closure-based are often the last resort when fixing lifetime issue due to the implicit HKTB on the closure types FnOnce, Fn and FnMut. Keep in mind that fetch the node via get_node is not particularly cheap and even NodePath::from_str will allocate.

from kohonen.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.