Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/core/internal/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,25 @@ template Fields(T)
alias Fields = TypeTuple!T;
}

/// See $(REF hasElaborateMove, std,traits)
template hasElaborateMove(S)
{
static if (__traits(isStaticArray, S) && S.length)
{
enum bool hasElaborateMove = hasElaborateMove!(typeof(S.init[0]));
}
else static if (is(S == struct))
{
enum hasElaborateMove = (is(typeof(S.init.opPostMove(lvalueOf!S))) &&
!is(typeof(S.init.opPostMove(rvalueOf!S)))) ||
anySatisfy!(.hasElaborateMove, Fields!S);
}
else
{
enum bool hasElaborateMove = false;
}
}

// std.traits.hasElaborateDestructor
template hasElaborateDestructor(S)
{
Expand Down
79 changes: 79 additions & 0 deletions src/object.d
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,85 @@ if (is(Obj : Object))
assert(a < "я");
}

/**
* Recursively calls the `opPostMove` callbacks of a struct and its members if
* they're defined.
*
* When moving a struct instance, the compiler emits a call to this function
* after blitting the instance and before releasing the original instance's
* memory.
*
* Params:
* newLocation = reference to struct instance being moved into
* oldLocation = reference to the original instance
*
* Note:
* This function is tentatively defined as `nothrow` to prevent
* `opPostMove` from being defined without `nothrow`, which would allow
* for possibly confusing changes in program flow.
*/
void __move_post_blt(S)(ref S newLocation, ref S oldLocation) nothrow
if (is(S == struct))
{
static foreach (memberName; __traits(allMembers, S))
{
static if (is(typeof(__traits(getMember, S, memberName)) == struct))
{
__move_post_blt(__traits(getMember, newLocation, memberName), __traits(getMember, oldLocation, memberName));
}
}

static if (__traits(hasMember, S, "opPostMove"))
{
import core.internal.traits : lvalueOf, rvalueOf;
static assert( is(typeof(S.init.opPostMove(lvalueOf!S))) &&
!is(typeof(S.init.opPostMove(rvalueOf!S))),
"`" ~ S.stringof ~ ".opPostMove` must take exactly one argument of type `" ~ S.stringof ~ "` by reference");

newLocation.opPostMove(oldLocation);
}
}

@safe nothrow unittest
{
struct A
{
bool movedInto;
void opPostMove(const ref A oldLocation)
{
movedInto = true;
}
}
A src, dest;
__move_post_blt(dest, src);
assert(dest.movedInto);
}

@safe nothrow unittest
{
struct A
{
bool movedInto;
void opPostMove(const ref A oldLocation)
{
movedInto = true;
}
}
struct B
{
A a;

bool movedInto;
void opPostMove(const ref B oldLocation)
{
movedInto = true;
}
}
B src, dest;
__move_post_blt(dest, src);
assert(dest.movedInto && dest.a.movedInto);
}

/**
Destroys the given object and optionally resets to initial state. It's used to
_destroy an object, calling its destructor or finalizer so it no longer
Expand Down