Comments (11)
(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.
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.
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.
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.
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.
@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.
@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.
@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.
That said, using mem::replace is still unergonomic compared just calling a method. So I just added one. Enjoy :-)
from dbus-rs.
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.
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)
- "vendored" feature fails to compile HOT 7
- Possible deadlock in dbus-tokio HOT 4
- Crossroad example not working HOT 1
- `vendored` feature's build script writes outside of `$OUT_DIR` HOT 5
- async monitor? HOT 4
- monitor system bus without root privileges HOT 4
- [Help] When variant is a struct? HOT 10
- dbus-codegen: When crossroads is enabled, no client-side code is generated HOT 3
- dbus-codegen: Add attribute to specify rust type for method arguments HOT 4
- Annotations on interfaces not shown in introspection
- Any way to set uid in dbus authentication? HOT 6
- How to ensure that libdbus-sys compiles with X11 for dbus-daemon autolaunch on build HOT 4
- register object of type X on dbus? HOT 3
- pkg config failed HOT 1
- Async signal match not working HOT 2
- `dbus-codegen-rust` produces `#[deprecated]` annotations on trait implementations HOT 1
- Yank rbus-rs package on crates.io HOT 1
- Generated code from an XML file that only contains properties does not compile with crossroads HOT 4
- Make new `dbus-codegen` release
- Recompilation of libdbus-sys triggered on every cargo build HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dbus-rs.