Skip to content

Commit

Permalink
Add Unshared to std.traits.
Browse files Browse the repository at this point in the history
This is the shared equivalent of Unconst. It allows code to strip off
shared without stripping off const.
  • Loading branch information
jmdavis authored and dlang-bot committed Oct 31, 2023
1 parent c4cbe0c commit 1febc32
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
13 changes: 13 additions & 0 deletions changelog/unshared.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Add Unshared to std.traits.

`Unshared` is the `shared` equivalent of `Unconst`. It strips off the outer
layer of `shared` from a type. e.g.

```
static assert(Unshared!(shared int) == int);
static assert(Unshared!(shared(int[])) == shared(int)[]);
```

So, `Unconst` strips off the outer layer of `const`, `immutable`, and `inout`;
`Unshared` strips off the outer layer of `shared`; and `Unqual` strips off all
qualifiers from the outer layer of a type.
41 changes: 41 additions & 0 deletions std/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
* $(LREF PointerTarget)
* $(LREF Signed)
* $(LREF Unconst)
* $(LREF Unshared)
* $(LREF Unqual)
* $(LREF Unsigned)
* $(LREF ValueType)
Expand Down Expand Up @@ -7848,6 +7849,46 @@ else
static assert(is(Unconst!ImmIntArr == immutable(int)[]));
}

/++
Removes `shared` qualifier, if any, from type `T`.
Note that while `immutable` is implicitly `shared`, it is unaffected by
Unshared. Only explict `shared` is removed.
+/
template Unshared(T)
{
static if (is(T == shared U, U))
alias Unshared = U;
else
alias Unshared = T;
}

///
@safe unittest
{
static assert(is(Unshared!int == int));
static assert(is(Unshared!(const int) == const int));
static assert(is(Unshared!(immutable int) == immutable int));

static assert(is(Unshared!(shared int) == int));
static assert(is(Unshared!(shared(const int)) == const int));

static assert(is(Unshared!(shared(int[])) == shared(int)[]));
}

@safe unittest
{
static assert(is(Unshared!( int) == int));
static assert(is(Unshared!( const int) == const int));
static assert(is(Unshared!( inout int) == inout int));
static assert(is(Unshared!( inout const int) == inout const int));
static assert(is(Unshared!(shared int) == int));
static assert(is(Unshared!(shared const int) == const int));
static assert(is(Unshared!(shared inout int) == inout int));
static assert(is(Unshared!(shared inout const int) == inout const int));
static assert(is(Unshared!( immutable int) == immutable int));
}

version (StdDdoc)
{
/**
Expand Down

0 comments on commit 1febc32

Please sign in to comment.