RCArray is unsafe

via Digitalmars-d digitalmars-d at puremagic.com
Tue Mar 3 05:45:35 PST 2015


On Tuesday, 3 March 2015 at 09:05:46 UTC, Walter Bright wrote:
> On 3/2/2015 9:58 PM, weaselcat wrote:
>> Borrowing 'a' from a struct would make the parent struct 
>> immutable during the
>> borrow scope of 'a', I believe.
>
> Right, now consider that struct is a leaf in a complex graph of 
> data structures.

Then you still cannot have more than one mutable reference to the 
entire graph. Because that is impractical, Rust uses unsafe (i.e. 
@trusted in D speak) accessors that "cast away" the ownership, 
but do so in a way that doesn't violate the guarantees.

For example, the type system doesn't allow you to get mutable 
references to the left and right children of a binary tree node. 
But there can be an accessor method that internally does some 
unsafe magic to return a tuple with mutable references to them, 
annotated with the information that they are mutably borrowed 
from the node. Both child refs are mutable, and the parent node 
is inaccessible as long as they exist.


More information about the Digitalmars-d mailing list