RFC: safe ref counting

Jon Degenhardt jond at noreply.com
Sun May 3 01:38:20 UTC 2020


On Saturday, 2 May 2020 at 02:27:10 UTC, Steven Schveighoffer 
wrote:
> In trying to make iopipe @safe, I came to the realization that 
> having auto-managed items such as files and the like (std.io 
> Files and Sockets are non-copyable), you need to rely on some 
> form of @safe reference counting. Unfortunately 
> std.typecons.RefCounted is not and cannot be safe. This is 
> because it allocates in the C heap, and deallocates regardless 
> of whether anyone has ever squirreled away a reference.

I was solving a much more constrained problem, but I wrote a 
couple of one-pass input ranges over a set of files, providing 
open file access to each in turn. Has the nice benefit of closing 
the file immediately when it is popFront'ed off the range. 
There's nothing preventing the caller from holding a copy of the 
underlying File object, but it is only open while it is the front 
element of the range. If the concept might be of interest, the 
code is here:
* 
https://github.com/eBay/tsv-utils/blob/master/common/src/tsv_utils/common/utils.d#L1968
* 
https://github.com/eBay/tsv-utils/blob/master/common/src/tsv_utils/common/utils.d#L2344

--Jon


More information about the Digitalmars-d mailing list