@safe RCSlice with DIP1000 + runtime checks

Nick Treleaven via Digitalmars-d digitalmars-d at puremagic.com
Fri Sep 2 04:18:58 PDT 2016


Hi,
I've been working on a proof of concept Reference Counted Slice 
container, based on the one in DIP1000. That one now has opAssign 
marked @system, see:

https://github.com/dlang/DIPs/blob/master/DIPs/DIP1000.md#owning-containers

So I decided to make an RCSlice with a @safe opAssign (and @safe 
destroy, std.algorithm.move). opIndex is no longer a direct 
member, instead indexing RCSlice causes the compiler to create a 
RCRef temporary struct (via alias this), which does have opIndex:

https://github.com/ntrel/stuff/blob/master/typecons/rcref.d

The temporary struct is a private type, returned as an rvalue, so 
can't itself be passed by ref, so it should avoid the paired ref 
argument case mentioned in the above DIP1000 link.

RCRef increments *RCSlice.count on construction and decrements on 
destruction, but first checks that *count is > 1. If this fails, 
an AssertError is thrown. When -noboundschecks is passed, the 
code assumes no safety checks should be made for RCSlice and 
compiles out all the additional counting code.

Of course it would be great to have compile-time checks, so I'm 
looking forward to a DIP for that. But in the meantime this 
appears to show D can have @safe ref-counting now that DIP1000 is 
underway.

Thoughts?


More information about the Digitalmars-d mailing list