Announcement

Collapse
No announcement yet.

Linus Torvalds' Initial Comment On Rust Code Prospects Within The Linux Kernel

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #31
    Originally posted by oleid View Post

    There are plenty of trees on crates.io. You can always fall back to raw pointers for such stuff and provide a safe wrapper. Nothing wrong with that.

    But I'm curious how you'd implement that in C++. Do you have a sample source of what exactly you have in mind at hand?
    You can't create an acceptable implementation with raw pointers, I tried and failed (but don't recall exactly why because it was like 12-18 months ago), if you can provide a viable/usable (not demo) solution with raw pointers I'd be more than happy!

    There are crates that deal with trees, yes, but none which is dynamically (and) mutable at the same time. So here's a virtual code example of a dynamically mutable tree:
    Code:
    class Tag {
    vector<Tag*> children;
    Tag *parent;
    
    // and methods that would tolerate aliased mutability - which is forbidden in Rust. "Safe" blocks don't cancel the borrow checker.
    int CountSize() {
        if (size_ == -1) { // not counted yet, need to count and cache it
           size_ = count my size;
          // size_ also depends on something from children or parents so need to call also into
         // their mutable methods but this isn't allowed. Full Stop.
         // What's worse you can catch this only at compile time because you're using RefCell and what's even worse it might not get triggered
          // until it detects aliased mutability.
        }
    return size_;
    }
    So
    a) you should be able to call mutable (non-const) methods of children or parents from your node's methods.
    b) therefore it should never trigger a runtime error like RefCell does, which is why you can't use it because it's only a matter of time - as your code becomes larger it will start throwing runtime errors and you can't design your code around this.
    cl333r
    Senior Member
    Last edited by cl333r; 11 July 2020, 04:30 PM.

    Comment


    • #32
      Originally posted by 144Hz View Post
      Same thing on the desktop.
      s/the/a/
      There, I fixed it for you. You're welcome.

      Comment


      • #33
        Originally posted by oleid View Post

        True, RefCell should only be used as last resort. But if it doesn't help in your example case, how do you prevent undefined behaviour in similar C++ code?
        There's no undefined behavior in C++, you just have to not forget to properly clean up resources in your object's destructor which is one of the very first things you learn in C++. It's easy, effective and simple.

        Comment


        • #34
          Originally posted by cl333r View Post
          You can't create an acceptable implementation with raw pointers, I tried and failed (but don't recall exactly why because it was like 12-18 months ago), if you can provide a viable/usable (not demo) solution with raw pointers I'd be more than happy!

          How about indextree? They are using an arena for the nodes (my gut feeling agrees). I never used it, but judging by the tests, you can remove parent while you also hold references to the children.
          oleid
          Senior Member
          Last edited by oleid; 11 July 2020, 04:57 PM.

          Comment


          • #35
            Originally posted by cl333r View Post

            There's no undefined behavior in C++, you just have to not forget to properly clean up resources in your object's destructor which is one of the very first things you learn in C++. It's easy, effective and simple.
            My mistake, I thought you were talking about trouble with RefCell due to concurrent access. Hence, I mentioned undefined behavior.

            Comment


            • #36
              Originally posted by cl333r View Post
              I'd actually like Rust to replace C but not C++ because Rust simply doesn't cut it. Something as simple as a dynamic mutable tree which would take a minute to implement in C++, is next to impossible in Rust - I've spent 2 months and failed, because the naive online examples don't work IRL because of the borrow checker which makes to whole thing a total mess, to this day I haven't seen a viable Rust solution, Mozilla's own Rust code from WebRender uses a JavaScript backend for this.
              Then use unsafe. It's an option for a reason.

              Comment


              • #37
                Originally posted by atmartens View Post

                Then use unsafe. It's an option for a reason.
                Unsafe doesn't solve this issue in any way. Just because it's unsafe doesn't mean you can get rid of the borrow checker, a quote from [1]:
                It’s important to understand that unsafe doesn’t turn off the borrow checker or disable any other of Rust’s safety checks: if you use a reference in unsafe code, it will still be checked.
                [1] https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html
                cl333r
                Senior Member
                Last edited by cl333r; 11 July 2020, 06:03 PM.

                Comment


                • #38
                  Originally posted by Veto
                  Aww, this is just cute. We will all learn to be nuanced as we grow older....
                  Context is key. I meant undefined behavior in the context of this issue.

                  Comment


                  • #39
                    Originally posted by oleid View Post


                    How about indextree? They are using an arena for the nodes (my gut feeling agrees). I never used it, but judging by the tests, you can remove parent while you also hold references to the children.
                    It seems they worked around and implemented the adding/removing of children but not of allowing to call mutable methods of children/parents because you can't lift the borrow checker's ban on aliased mutability.

                    Comment


                    • #40
                      Originally posted by Veto
                      trolling the internet
                      OK boomer.

                      Comment

                      Working...
                      X