RCArray is unsafe

deadalnix via Digitalmars-d digitalmars-d at puremagic.com
Mon Mar 2 13:12:19 PST 2015


On Monday, 2 March 2015 at 20:54:20 UTC, Walter Bright wrote:
> I looked at how Rust does it. I was hoping it was something 
> clever, but it isn't. A copy is made of the object to which a 
> borrowed reference is taken - in other words, the reference 
> count is incremented.
>
> For D structs, that means, if there's a postblit, a copy must 
> be made. For D ref counted classes, a ref count increment must 
> be done.
>
> I was hoping to avoid that, but apparently there's no way.
>
> There are cases where this can be avoided, like calling pure 
> functions. Another win for pure functions!

I do think you are confusing how Rust does it. In Rust, borrowing 
makes the source and borrowed reference immutable by default. So 
by default the problem do not occurs.

You can also borrow a mutable reference, in which case the source 
is disabled for the duration of the borrowing. That makes it 
impossible to borrow a mutable reference twice.

The above mentioned scenario cannot happen at all in Rust, by 
design. As a result, there is solution for the problem, as the 
problem cannot occur in the first place.


More information about the Digitalmars-d mailing list