Skip to content

Commit

Permalink
Add comment about copy
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaTheFoxgirl committed Oct 3, 2024
1 parent 668b6a9 commit 4598a06
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
17 changes: 14 additions & 3 deletions source/numem/collections/vector.d
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,15 @@ public:
@trusted
this(ref selfType rhs) {
if (rhs.memory) {

// NOTE: We need to turn these into pointers because
// The D compiler otherwise thinks its supposed
// to free the operands.
auto self = (cast(selfType*)&this);
auto other = (cast(selfType*)&rhs);

this.resize_(rhs.size_);
this._memcpy(this.memory, rhs.memory, rhs.size_);
this._memcpy(self.memory, other.memory, rhs.size_);
}
}

Expand All @@ -176,8 +183,12 @@ public:
@trusted
this(ref return scope inout(selfType) rhs) inout {
if (rhs.memory) {
auto self = (cast(selfType)this);
auto other = (cast(selfType)rhs);

// NOTE: We need to turn these into pointers because
// The D compiler otherwise thinks its supposed
// to free the operands.
auto self = (cast(selfType*)&this);
auto other = (cast(selfType*)&rhs);

self.resize_(rhs.size_);
other._memcpy(self.memory, other.memory, other.size_);
Expand Down
16 changes: 8 additions & 8 deletions source/numem/string.d
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public:
*/
@trusted
this(ref return scope selfType rhs) {

// NOTE: We need to turn these into pointers because
// The D compiler otherwise thinks its supposed
// to free the operands.
selfType* self = &this;
selfType* other = &rhs;
(*self).set_((*other)[]);
Expand All @@ -114,6 +118,10 @@ public:
@trusted
this(ref return scope inout(selfType) rhs) inout {
if (rhs.size > 0) {

// NOTE: We need to turn these into pointers because
// The D compiler otherwise thinks its supposed
// to free the operands.
selfType* self = (cast(selfType*)&this);
selfType* other = (cast(selfType*)&rhs);
(*self).set_((*other)[]);
Expand Down Expand Up @@ -494,11 +502,6 @@ unittest {
version(unittest) {
struct MyStruct {
@nogc:
this(ref return scope inout(MyStruct) src) inout
{
foreach (i, ref inout field; src.tupleof)
this.tupleof[i] = field;
}
nstring str;
}
}
Expand All @@ -511,9 +514,6 @@ unittest {
struct_ ~= MyStruct(nstring("a"));
struct_ ~= MyStruct(nstring("b"));

writeln(struct_[0..$]);
writeln(struct_[0].str);

vector!MyStruct copy = struct_;
nogc_delete(copy);

Expand Down

0 comments on commit 4598a06

Please sign in to comment.