Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

71%
+3 −0
Q&A Recursive traversal of composite tree of mutable "trait objects"?

A root.components field need not exist, so I'm not sure what you're hoping for. Also, I don't know if it's an issue with your description or your code, but the code will only return mutable referen...

posted 5mo ago by Derek Elkins‭  ·  edited 5mo ago by Derek Elkins‭

Answer
#2: Post edited by user avatar Derek Elkins‭ · 2024-07-03T08:17:20Z (5 months ago)
Add Rc comments
  • A `root.components` field need not exist, so I'm not sure what you're hoping for. Also, I don't know if it's an issue with your description or your code, but the code will only return mutable references to "leaf" `Component`s while your description talks about "all descendants". It's not clear if "all descendants" is intended to include `Component`s that have children as well.
  • As far as I can tell, changing to:
  • ```rust
  • match root.components().is_empty() {
  • true => v.push(root),
  • false =>
  • root.components_mut()
  • .into_iter()
  • .for_each(|c| v.append(&mut subtree_mut(c))),
  • }
  • ```
  • would compile and work fine other than the unnecessary allocation of the result of `root.components()` which could be fixed by adding a method to `Component` to test for this case without needing to return a `Vec`. This could probably be finessed some other way.
  • As for
  • > Using `root.components_mut()` would create a temporary value owned by the function and trying to return that is an error.
  • This doesn't matter. This temporary's lifetime isn't really connected to anything. Just the contents of the `Vec` are attached to the relevant lifetimes. In the above code, this is addressed by consuming that intermediate via `into_iter()` and then just passing `c` directly since it is the mutable ref we want.
  • And for
  • > Even if it weren't an error, the recursive logic would be incorrect and several objects would be added more than once.
  • I don't quite understand what you're getting at here. In the code I list above, mutable references to all "leaf" `Component`s will be returned (assuming no cycles...). If a `Component` is a child of multiple `Component`s, then its "leaves" will be included multiple times. If this is a problem, then it wouldn't be resolved by directly accessing some `component` field. You need to detect visited `Component`s and avoid reprocessing them. This could be done by having a `visited` field that is set or by having a set of visited `Component`s that gets passed along.
  • A `root.components` field need not exist, so I'm not sure what you're hoping for. Also, I don't know if it's an issue with your description or your code, but the code will only return mutable references to "leaf" `Component`s while your description talks about "all descendants". It's not clear if "all descendants" is intended to include `Component`s that have children as well.
  • As far as I can tell, changing to:
  • ```rust
  • match root.components().is_empty() {
  • true => v.push(root),
  • false =>
  • root.components_mut()
  • .into_iter()
  • .for_each(|c| v.append(&mut subtree_mut(c))),
  • }
  • ```
  • would compile and work fine other than the unnecessary allocation of the result of `root.components()` which could be fixed by adding a method to `Component` to test for this case without needing to return a `Vec`. This could probably be finessed some other way.
  • As for
  • > Using `root.components_mut()` would create a temporary value owned by the function and trying to return that is an error.
  • This doesn't matter. This temporary's lifetime isn't really connected to anything. Just the contents of the `Vec` are attached to the relevant lifetimes. In the above code, this is addressed by consuming that intermediate via `into_iter()` and then just passing `c` directly since it is the mutable ref we want.
  • And for
  • > Even if it weren't an error, the recursive logic would be incorrect and several objects would be added more than once.
  • I don't quite understand what you're getting at here. In the code I list above, mutable references to all "leaf" `Component`s will be returned (assuming no cycles...). ~~If a `Component` is a child of multiple `Component`s, then its "leaves" will be included multiple times. If this is a problem, then it wouldn't be resolved by directly accessing some `component` field. You need to detect visited `Component`s and avoid reprocessing them. This could be done by having a `visited` field that is set or by having a set of visited `Component`s that gets passed along.~~ This technically can't happen, since it would require a violation of the borrowing rules to get into this situation. That said, if you did want a non-tree-like graph to be possible, you'd need to use something like `Rc`. Even without that though, you'd likely need something like `Rc` to be able to return all mutable references to all descendants, since mutably borrowing a `KeypadModel` would preclude mutably borrowing any element of its `component` field.
#1: Initial revision by user avatar Derek Elkins‭ · 2024-07-03T08:02:37Z (5 months ago)
A `root.components` field need not exist, so I'm not sure what you're hoping for. Also, I don't know if it's an issue with your description or your code, but the code will only return mutable references to "leaf" `Component`s while your description talks about "all descendants". It's not clear if "all descendants" is intended to include `Component`s that have children as well.

As far as I can tell, changing to:

```rust
match root.components().is_empty() {
    true => v.push(root),
    false =>
        root.components_mut()
            .into_iter()
            .for_each(|c| v.append(&mut subtree_mut(c))),
}
```
would compile and work fine other than the unnecessary allocation of the result of `root.components()` which could be fixed by adding a method to `Component` to test for this case without needing to return a `Vec`. This could probably be finessed some other way.

As for

 > Using `root.components_mut()` would create a temporary value owned by the function and trying to return that is an error.

This doesn't matter. This temporary's lifetime isn't really connected to anything. Just the contents of the `Vec` are attached to the relevant lifetimes. In the above code, this is addressed by consuming that intermediate via `into_iter()` and then just passing `c` directly since it is the mutable ref we want.

And for

 > Even if it weren't an error, the recursive logic would be incorrect and several objects would be added more than once.

I don't quite understand what you're getting at here. In the code I list above, mutable references to all "leaf" `Component`s will be returned (assuming no cycles...). If a `Component` is a child of multiple `Component`s, then its "leaves" will be included multiple times. If this is a problem, then it wouldn't be resolved by directly accessing some `component` field. You need to detect visited `Component`s and avoid reprocessing them. This could be done by having a `visited` field that is set or by having a set of visited `Component`s that gets passed along.