How use SafeRefCounted in @safe code safely?

kdevel kdevel at vogtner.de
Sat Mar 23 23:10:38 UTC 2024


```d
@safe:

void foo ()
{
    import std.typecons : SafeRefCounted;
    SafeRefCounted!int s;
}

unittest {
    import std.exception : assertNotThrown;
    assertNotThrown (foo);
}
```

```
$ dmd -unittest -main -run  sr.d
sr.d(6): Error: `@safe` function `sr.foo` cannot call `@system` 
destructor `std.typecons.SafeRefCounted!(int, 
RefCountedAutoInitialize.yes).SafeRefCounted.~this`
[...]linux/bin64/../../src/phobos/std/typecons.d(7399):        
`std.typecons.SafeRefCounted!(int, 
RefCountedAutoInitialize.yes).SafeRefCounted.~this` is declared 
here

```

Inspired by [1] I tried to compile with `-dip1000` 
(`-preview=dip1000`):

```
dmd -dip1000 -unittest -main -run sr.d
1 modules passed unittests
```

So in order to use a phobos template in the `@safe`-dialect of D 
I must enable the DIP1000-mode of the compiler? But what is the 
status of DIP1000? According to [2]'s boilerplate its status is 
"Superseded". Unfortunately it does not mention by what DIP1000 
is superseded.

My question is this: I need RAII for non-memory resources and I 
would like to implement this using SafeRefCounted. Assume all 
this is used in a function deployed into cars for autonomous 
driving. Support for this software must be available for the next 
20 years. Is @safe + SafeRefCounted + -dip1000 future-proof?

[1] Issue 13983 - RefCounted needs to be @safe/Comment 9
     https://issues.dlang.org/show_bug.cgi?id=13983#c9

[2] Scoped Pointers
     
https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1000.md



More information about the Digitalmars-d-learn mailing list