Skip to content

Commit

Permalink
Allow containers to be used in safe code entirely (dlang-community#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
GabyForceQ committed Nov 10, 2018
1 parent 760a040 commit 000c16d
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 38 deletions.
2 changes: 2 additions & 0 deletions src/containers/cyclicbuffer.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

module containers.cyclicbuffer;

@trusted:

private import core.exception : onRangeError;
private import stdx.allocator.mallocator : Mallocator;
private import std.range.primitives : empty, front, back, popFront, popBack;
Expand Down
4 changes: 3 additions & 1 deletion src/containers/dynamicarray.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

module containers.dynamicarray;

@trusted:

private import containers.internal.node : shouldAddGCRange;
private import stdx.allocator.mallocator : Mallocator;

Expand Down Expand Up @@ -563,7 +565,7 @@ version(emsi_containers_unittest) unittest

}

version(emsi_containers_unittest) @system unittest
version(emsi_containers_unittest) unittest
{
DynamicArray!int a;
a.reserve(1000);
Expand Down
12 changes: 7 additions & 5 deletions src/containers/hashmap.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

module containers.hashmap;

@trusted:

private import containers.internal.hash;
private import containers.internal.node : shouldAddGCRange;
private import stdx.allocator.mallocator : Mallocator;
Expand Down Expand Up @@ -189,7 +191,7 @@ struct HashMap(K, V, Allocator = Mallocator, alias hashFunction = generateHash!K
* Returns: pointer to the value corresponding to the given key,
* or null if the key is not present in the HashMap.
*/
inout(V)* opBinaryRight(string op)(const K key) inout nothrow @trusted if (op == "in")
inout(V)* opBinaryRight(string op)(const K key) inout nothrow if (op == "in")
{
size_t i;
auto n = find(key, i);
Expand Down Expand Up @@ -237,7 +239,7 @@ struct HashMap(K, V, Allocator = Mallocator, alias hashFunction = generateHash!K
/**
* Returns: a range of the keys in this map.
*/
auto byKey(this This)() inout @trusted
auto byKey(this This)() inout
{
return MapRange!(This, IterType.key)(cast(Unqual!(This)*) &this);
}
Expand Down Expand Up @@ -266,7 +268,7 @@ struct HashMap(K, V, Allocator = Mallocator, alias hashFunction = generateHash!K
/**
* Returns: a range of the values in this map.
*/
auto byValue(this This)() inout @trusted
auto byValue(this This)() inout
{
return MapRange!(This, IterType.value)(cast(Unqual!(This)*) &this);
}
Expand Down Expand Up @@ -298,7 +300,7 @@ struct HashMap(K, V, Allocator = Mallocator, alias hashFunction = generateHash!K
* Returns: a range of the kev/value pairs in this map. The element type of
* this range is a struct with `key` and `value` fields.
*/
auto byKeyValue(this This)() inout @trusted
auto byKeyValue(this This)() inout
{
return MapRange!(This, IterType.both)(cast(Unqual!(This)*) &this);
}
Expand Down Expand Up @@ -512,7 +514,7 @@ private:
/**
* Rehash the map.
*/
void rehash() @trusted
void rehash()
{
import std.conv : emplace;
immutable size_t newLength = buckets.length << 1;
Expand Down
8 changes: 5 additions & 3 deletions src/containers/hashset.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

module containers.hashset;

@trusted:

private import containers.internal.hash : generateHash, hashToIndex;
private import containers.internal.node : shouldAddGCRange;
private import stdx.allocator.mallocator : Mallocator;
Expand Down Expand Up @@ -193,7 +195,7 @@ struct HashSet(T, Allocator = Mallocator, alias hashFunction = generateHash!T,
/**
* Forward range interface
*/
auto opSlice(this This)() nothrow @nogc @trusted
auto opSlice(this This)() nothrow @nogc
{
return Range!(This)(&this);
}
Expand Down Expand Up @@ -250,7 +252,7 @@ private:
return cast(ET) currentNode.items[nodeIndex].value;
}

void popFront() nothrow @trusted @nogc
void popFront() nothrow @nogc
{
if (nodeIndex + 1 < currentNode.l)
{
Expand Down Expand Up @@ -308,7 +310,7 @@ private:
return (numberOfNodes / cast(float) buckets.length) > 0.75f;
}

void rehash() @trusted
void rehash()
{
import stdx.allocator : makeArray, dispose;
import core.memory : GC;
Expand Down
2 changes: 2 additions & 0 deletions src/containers/immutablehashset.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

module containers.immutablehashset;

@trusted:

/**
* The immutable hash set is useful for constructing a read-only collection that
* supports quickly determining if an element is present.
Expand Down
2 changes: 1 addition & 1 deletion src/containers/internal/hash.d
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ else
/**
* A variant of the FNV-1a (64) hashing algorithm.
*/
hash_t generateHash(T)(T value) pure nothrow @nogc @trusted if (is(T == string))
hash_t generateHash(T)(T value) pure nothrow @nogc if (is(T == string))
{
hash_t h = 0xcbf29ce484222325;
foreach (const ubyte c; cast(ubyte[]) value)
Expand Down
2 changes: 2 additions & 0 deletions src/containers/openhashset.d
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
module containers.openhashset;

@trusted:

private import containers.internal.hash;
private import containers.internal.node : shouldAddGCRange;
private import stdx.allocator.common : stateSize;
Expand Down
4 changes: 3 additions & 1 deletion src/containers/simdset.d
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
module containers.simdset;

@trusted:

private import stdx.allocator.mallocator : Mallocator;

/**
Expand Down Expand Up @@ -66,7 +68,7 @@ version (D_InlineAsm_X86_64) struct SimdSet(T, Allocator = Mallocator)
* Returns:
* true if the set contains the given item
*/
bool contains(T item) const pure nothrow @nogc @trusted
bool contains(T item) const pure nothrow @nogc
{
if (_length == 0)
return false;
Expand Down
8 changes: 5 additions & 3 deletions src/containers/slist.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

module containers.slist;

@trusted:

private import containers.internal.node : shouldAddGCRange;
private import stdx.allocator.mallocator : Mallocator;

Expand Down Expand Up @@ -164,7 +166,7 @@ struct SList(T, Allocator = Mallocator, bool supportGC = shouldAddGCRange!T)
* Complexity: O(1)
* Params: t = the item to insert into the list
*/
void insertFront(T t) @trusted
void insertFront(T t)
{
_front = make!Node(allocator, _front, t);
static if (useGC)
Expand Down Expand Up @@ -200,7 +202,7 @@ struct SList(T, Allocator = Mallocator, bool supportGC = shouldAddGCRange!T)
* Complexity: O(length)
* Returns: true if a value was removed.
*/
bool remove(V)(V value) @trusted
bool remove(V)(V value)
{
Node* prev = null;
Node* cur = _front;
Expand Down Expand Up @@ -271,7 +273,7 @@ private:
static struct Range(ThisT)
{
public:
ET front() pure nothrow @property @trusted @nogc
ET front() pure nothrow @property @nogc
{
return cast(typeof(return)) current.value;
}
Expand Down
20 changes: 11 additions & 9 deletions src/containers/treemap.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

module containers.treemap;

@trusted:

private import containers.internal.node : shouldAddGCRange;
private import stdx.allocator.mallocator : Mallocator;

Expand Down Expand Up @@ -54,7 +56,7 @@ struct TreeMap(K, V, Allocator = Mallocator, alias less = "a < b",
/**
* Inserts or overwrites the given key-value pair.
*/
void insert(const K key, V value) @trusted
void insert(const K key, V value)
{
auto tme = TreeMapElement(cast(ContainerStorageType!K) key, value);
auto r = tree.equalRange(tme);
Expand Down Expand Up @@ -85,7 +87,7 @@ struct TreeMap(K, V, Allocator = Mallocator, alias less = "a < b",
/**
* Returns: the value associated with the given key, or the given `defaultValue`.
*/
auto get(this This)(const K key, lazy V defaultValue) inout @trusted
auto get(this This)(const K key, lazy V defaultValue) inout
{
alias CET = ContainerElementType!(This, V);
auto tme = TreeMapElement(key);
Expand Down Expand Up @@ -133,7 +135,7 @@ struct TreeMap(K, V, Allocator = Mallocator, alias less = "a < b",
/**
* Returns: true if the mapping contains the given key
*/
bool containsKey(const K key) inout pure nothrow @nogc @trusted
bool containsKey(const K key) inout pure nothrow @nogc
{
auto tme = TreeMapElement(cast(ContainerStorageType!K) key);
return tree.contains(tme);
Expand All @@ -158,7 +160,7 @@ struct TreeMap(K, V, Allocator = Mallocator, alias less = "a < b",
/**
* Returns: a GC-allocated array of the keys in the map
*/
auto keys(this This)() inout pure @property @trusted
auto keys(this This)() inout pure @property
{
import std.array : array;

Expand All @@ -168,7 +170,7 @@ struct TreeMap(K, V, Allocator = Mallocator, alias less = "a < b",
/**
* Returns: a range of the keys in the map
*/
auto byKey(this This)() inout pure @trusted @nogc
auto byKey(this This)() inout pure @nogc
{
import std.algorithm.iteration : map;
alias CETK = ContainerElementType!(This, K);
Expand All @@ -179,7 +181,7 @@ struct TreeMap(K, V, Allocator = Mallocator, alias less = "a < b",
/**
* Returns: a GC-allocated array of the values in the map
*/
auto values(this This)() inout pure @property @trusted
auto values(this This)() inout pure @property
{
import std.array : array;

Expand All @@ -189,7 +191,7 @@ struct TreeMap(K, V, Allocator = Mallocator, alias less = "a < b",
/**
* Returns: a range of the values in the map
*/
auto byValue(this This)() inout pure @trusted @nogc
auto byValue(this This)() inout pure @nogc
{
import std.algorithm.iteration : map;
alias CETV = ContainerElementType!(This, V);
Expand All @@ -204,7 +206,7 @@ struct TreeMap(K, V, Allocator = Mallocator, alias less = "a < b",
* Returns: a range of the kev/value pairs in this map. The element type of
* this range is a struct with `key` and `value` fields.
*/
auto byKeyValue(this This)() inout pure @trusted
auto byKeyValue(this This)() inout pure
{
import std.algorithm.iteration : map;
alias CETV = ContainerElementType!(This, V);
Expand Down Expand Up @@ -241,7 +243,7 @@ private:
TreeType tree;
}

version(emsi_containers_unittest) @system unittest
version(emsi_containers_unittest) unittest
{
TreeMap!(string, string) tm;
tm["test1"] = "hello";
Expand Down
Loading

0 comments on commit 000c16d

Please sign in to comment.