Skip to content

Commit

Permalink
fix struct move by value
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaTheFoxgirl committed Dec 26, 2024
1 parent 439cafd commit 51e0718
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions source/numem/core/memory/lifetime.d
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,28 @@ void __copy(S, T)(ref S source, ref T target) @nogc {
Moves source to target, via destructive copy if neccesary.
source will be reset to its init state after the move.
*/
void __move(S, T)(ref S source, ref T target) @nogc {
void __move(S, T)(ref S source, ref T target) @nogc @trusted {
static if (is(T == struct) && hasElaborateDestructor!T) {
if(&source is &target)
return;

destruct!(T, false)(target);
}

return __moveImpl(source, target);
}

///
T __move(T)(ref return scope T source) @nogc @trusted {
T target = void;
__moveImpl(source, target);
return target;
}

private
pragma(inline, true)
void __moveImpl(S, T)(ref S source, ref T target) @nogc @trusted {
static if(is(T == struct)) {
static if (hasElaborateDestructor!T)
destruct!(T, false)(target);

assert(&source !is &target, "Source and target must not be identical");
__blit(target, source);

Expand Down Expand Up @@ -286,12 +303,6 @@ void __move(S, T)(ref S source, ref T target) @nogc {
}
}

///
T __move(T)(scope ref return T source) @nogc @trusted {
T target = void;
__move(source, target);
return target;
}

/**
Blits instance `from` to location `to`.
Expand Down

0 comments on commit 51e0718

Please sign in to comment.