News for hackers. Stuff that matters.
Re:Immutable.rs: immutable data structures for Rust by jcelerier on Friday April 06, 2018 @09:45PM (#16778959)
> That's a really interesting approach.

... isn't this just basic copy-on-write ? that's something you learn to implement in first year of comp-sci. Old C++ types used to work like this - and some still do, for instance Qt's collection types are all COW.

Re:Immutable.rs: immutable data structures for Rust by nightcracker on Friday April 06, 2018 @10:22PM (#16779070)
No, this is a bit different.

This is copy-on-write-unless-we're-alone-then-just-mutate-without-copy. That is, it's possible to write without copying if it has been determined that there are no other readers that would be referring to the old value (thus no copy is needed).

Re:Immutable.rs: immutable data structures for Rust by slavik81 on Friday April 06, 2018 @10:36PM (#16779101)
That's exactly what the Qt collections do:

> Implicit sharing automatically detaches the object from a shared block if the object is about to change and the reference count is greater than one. (This is often called copy-on-write or value semantics.) [source: http://doc.qt.io/qt-5/implicit-sharing.html]

The C++ language is sometimes a little cruel, though. It's easy to accidentally call the mutable version of a function when the const version would have been sufficient. That can be a performance problem if it causes unnecessary copies.

Re:Immutable.rs: immutable data structures for Rust by barrkel on Saturday April 07, 2018 @03:00AM (#16779717)
It's what Delphi has been doing with strings since 1995. It really is just copy on write, which normally has an assumption that the thing is shared before you copy. It's easily implemented with a refcount. It makes heap allocated types behave a lot like value types.
Re:Immutable.rs: immutable data structures for Rust by jcelerier on Sunday April 08, 2018 @07:05AM (#16785588)
> copy-on-write-unless-we're-alone-then-just-mutate-without-copy

well, yes, that's generally called copy-on-write. A COW class which does a copy every time you write to it would be pretty useless, woudln't it ?

e.g. if you did, in old std::strings:

    std::string s = "foo";
    s[1] = "x"; // no copy here
    auto s2 = s; // no copy here;
    s[1] = "y"; // s is copied in a new buffer
    s[1] = "z"; // no copy here
    s2 += "bar"; // no copy here
Re:Immutable.rs: immutable data structures for Rust by pavanky on Saturday April 07, 2018 @12:45AM (#16779410)
That is a natural extension of copy on write if you are keeping reference counts. I implemented something similar for a C++ library a couple of years ago.
Re:Immutable.rs: immutable data structures for Rust by kibwen on Friday April 06, 2018 @10:22PM (#16779069)
Rust actually provides a COW type in the stdlib for augmenting any of your types with such behavior: https://doc.rust-lang.org/std/borrow/enum.Cow.html