Coder Social home page Coder Social logo

Comments (11)

diwic avatar diwic commented on June 28, 2024 2

(although this requires an unsafe conversion of &m.tree to a &mut m.tree, it's a least possible) (This should not cause issues as long as it's always a different ObjectPath we're adding or removing than the objectpath/interface our method was invoked on)

It is never allowed to directly convert a & to a &mut. This is because there are LLVM annotations on & that indicates the pointer is read-only (edit: and on &mut that it's not aliased). This allows LLVM to do some interesting optimisations which, obviously, can cause random behaviour if you in fact break them. More information

from dbus-rs.

diwic avatar diwic commented on June 28, 2024 1

@agrover

here's a slightly longer way of doing the same thing, but probably easier to understand:

// we want to add "my_new_path" to "tree"
let mut temp = Tree::new();
mem::swap(&mut temp, &mut tree);
let mut newtree = temp.add(my_new_path);
mem::swap(&mut newtree, &mut tree);

Nothing is ever added to the empty tree, it's just used to be temporarily stored in tree while we change the tree.

from dbus-rs.

agrover avatar agrover commented on June 28, 2024

Proposed implementation of tree/objectpath.rs Tree add_o_ref:

    pub fn add_o_ref<I: Into<Arc<ObjectPath<M, D>>>>(&mut self, s: I)
                                                     -> Arc<ObjectPath<M, D>> {
        let m = s.into();
        self.paths.insert(m.name.clone(), m.clone());
        m
    }

from dbus-rs.

diwic avatar diwic commented on June 28, 2024

Well, I'm not happy about sending a &mut Tree in, because if you could, you could remove all object paths, which could include the one you're currently calling a method on. That Rust protects you from being able to write that code is more a good than a bad thing IMO.

I believe the way to do this is in two steps: The method would just add an "add" or "remove" instruction in a TODO list. Back outside tree::run (edit: or tree::handle) you have full mutable access to the tree, where you can carry out the instruction.

If you want to wait to send the reply back to the client until after the instruction has been fully carried out, you can also store the method_return message in the TODO list (and return an empty vec back in the method handler).

from dbus-rs.

diwic avatar diwic commented on June 28, 2024

I believe your suggested add_o_ref can be done today as:

*tree = mem::replace(&mut tree, Tree::new()).add(my_new_path);

from dbus-rs.

tasleson avatar tasleson commented on June 28, 2024

@diwic Maybe it's not a good API design choice, but for a different project that I've worked on I exposed a dbus job object for long running operations. The job object has properties and methods , one of which is to remove itself when the client no longer needs it. Thus in this case it was required that the dbus library have the ability to remove a dbus object from one of the methods on the object itself.

from dbus-rs.

diwic avatar diwic commented on June 28, 2024

@tasleson I don't mind that the API exposes a function to remove its own object path (or interface, etc). But when we implement such functionality, we need to do this in a safe manner, avoiding dangling pointers.

IMO, the TODO list approach is the best one. The other option would be to do refcounting, but then every time you call into a method you need to up the reference count to the object path, interface, method etc, and down the refcount afterwards. While that would not be much of a performance hit compared to sending messages over D-Bus in general, it still feels like an unnecessary complication for something that happens quite rarely or not at all for most D-Bus applications.

from dbus-rs.

agrover avatar agrover commented on June 28, 2024

@diwic: I'm afraid I still need more help understanding your use of mem::replace. Assuming we have obtained a mut ref of Tree, we want to add an objectpath to the existing tree -- your snippet appears to replace the tree entirely with a new tree with just the new object path, no?

from dbus-rs.

diwic avatar diwic commented on June 28, 2024

That said, using mem::replace is still unergonomic compared just calling a method. So I just added one. Enjoy :-)

from dbus-rs.

diwic avatar diwic commented on June 28, 2024

So, the add_o_ref method has been added (renamed to "insert"), the &mut Tree suggestion is unlikely to happen and I suggest to use a TODO list instead as a more "rustic" approach to the problem.

With that, is there anything else in this issue that needs fixing / clarification?

from dbus-rs.

agrover avatar agrover commented on June 28, 2024

Much thanks @diwic. I think insert and remove plus the TODO list approach could work well (and avoid undefined behavior), we'll give that a try!

from dbus-rs.

Related Issues (20)

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.