Skip to content

Commit

Permalink
add T dup(T)(T obj) for shallowClone
Browse files Browse the repository at this point in the history
  • Loading branch information
mw66 committed Jun 25, 2022
1 parent 1ebc3f7 commit 6e07898
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions source/jdiutil/memory.d
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,22 @@ unittest {
main();

}

/* ========================================================================== *\
\* ========================================================================== */
// from: https://forum.dlang.org/post/acafsosotrjdswwuklob@forum.dlang.org
// use the same name as array.dup https://dlang.org/spec/arrays.html
extern (C) Object _d_newclass(TypeInfo_Class ci);

T dup(T)(T obj) { // shallowClone
if (obj is null)
return null;
ClassInfo ci = obj.classinfo;
size_t start = Object.classinfo.m_init.length;
size_t end = ci.m_init.length;
T clone = cast(T)_d_newclass(ci);
(cast(void*)clone)[start .. end] = (cast(void*)obj)[start .. end];
return clone;
}


0 comments on commit 6e07898

Please sign in to comment.