Skip to content

Commit

Permalink
Made std.exception.pointsTo "@trusted pure nothrow".
Browse files Browse the repository at this point in the history
Also made it tolerant of shared objects. It's @trusted to cast shared away because the function just compares the addresses of passed objects.
  • Loading branch information
sinfu committed Nov 18, 2010
1 parent 85b5c42 commit 0339b6a
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions std/exception.d
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,12 @@ that points to $(D target)'s representation or somewhere inside
it. Note that evaluating $(D pointsTo(x, x)) checks whether $(D x) has
internal pointers.
*/
bool pointsTo(S, T)(ref S source, ref T target)
bool pointsTo(S, T)(ref const S source, ref const T target) @trusted pure nothrow
{
static if (is(S P : U*, U))
{
const void * m = source, b = &target, e = b + target.sizeof;
const m = cast(void*) source,
b = cast(void*) &target, e = b + target.sizeof;
return b <= m && m < e;
}
else static if (is(S == struct))
Expand All @@ -379,8 +380,8 @@ bool pointsTo(S, T)(ref S source, ref T target)
}
else static if (isDynamicArray!(S))
{
const void* p1 = source.ptr, p2 = p1 + source.length,
b = &target, e = b + target.sizeof;
const p1 = cast(void*) source.ptr, p2 = p1 + source.length,
b = cast(void*) &target, e = b + target.sizeof;
return overlap(p1[0 .. p2 - p1], b[0 .. e - b]).length != 0;
}
else
Expand Down Expand Up @@ -421,6 +422,10 @@ unittest
Holder h;
pointsTo(h, h);
}

shared S3 sh3;
shared sh3sub = sh3.a[];
assert(pointsTo(sh3sub, sh3));
}

/*********************
Expand Down

0 comments on commit 0339b6a

Please sign in to comment.