What should happen here?

Johan j at j.nl
Thu Sep 23 10:54:06 UTC 2021


On Wednesday, 22 September 2021 at 21:06:11 UTC, Steven 
Schveighoffer wrote:
> On 9/21/21 8:02 AM, Steven Schveighoffer wrote:
>
>> I just thought of a possible easy and effective way to ensure 
>> the thing isn't collected early:
>> 
>> ```d
>> struct Pin(T)
>> {
>>     T t;
>>     @nogc nothrow pure @safe ~this() {}
>>     alias t this;
>> }
>> 
>> ...
>> // usage
>> auto c = Pin!C(new C); // now it needs to be held until the 
>> scope ends
>> ```
>
> I made a package for something like this: 
> https://code.dlang.org/packages/keepalive
>
> Maybe it might find some use.

For the simple Pin version above, LDC generates the same machine 
code with/without Pin (as expected):

https://d.godbolt.org/z/MW7d9Mefe

-Johan



More information about the Digitalmars-d mailing list