Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive

Comments

core.lifetime: Add copyEmplace() to simulate copy-construction#3239

Merged
dlang-bot merged 4 commits intodlang:masterfrom
kinke:copyEmplace
Oct 19, 2020
Merged

core.lifetime: Add copyEmplace() to simulate copy-construction#3239
dlang-bot merged 4 commits intodlang:masterfrom
kinke:copyEmplace

Conversation

@kinke
Copy link
Contributor

@kinke kinke commented Oct 18, 2020

With a function signature analogous to moveEmplace() - source ref first, then target ref. Allowing non-mutable target types T is required to invoke the correct copy constructor, see the tests.

std.variant has a similar implementation (but insufficient wrt. copy constructors), and there might be more potential usage sites in
druntime/Phobos/user code.

Destructing a partially copied static array has been adopted from copy-constructing a new dynamic array:

size_t i;
try
{
for (i = 0; i < to.length; i++)
{
auto elem = cast(Unqual!T*)&to[i];
// Copy construction is defined as bit copy followed by postblit.
memcpy(elem, &from[i], element_size);
postblitRecurse(*elem);
}
}
catch (Exception o)
{
/* Destroy, in reverse order, what we've constructed so far
*/
while (i--)
{
auto elem = cast(Unqual!T*)&to[i];
destroy(*elem);
}
throw o;
}

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @kinke! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + druntime#3239"

@kinke kinke force-pushed the copyEmplace branch 2 times, most recently from cced01b to 0ed01fd Compare October 18, 2020 03:20
With a function signature analogous to moveEmplace() - source ref first,
then target ref. Allowing non-mutable target types T is required to
invoke the correct copy constructor, see the tests.

std.variant has a similar implementation (but insufficient wrt. copy
constructors), and there might be more potential usage sites in
druntime/Phobos/user code.

Destructing a partially copied static array has been adopted from
core.internal.array.construction._d_arrayctor().
For copy-constructing a new dynamic array, either from a slice or a
single element. This makes the implementation use copy constructors now.
For copy-constructing the appended element(s). This makes the
implementation use copy constructors now.
@thewilsonator
Copy link
Contributor

Looks fine to me. cc @RazvanN7

@dlang-bot dlang-bot merged commit 3b52bcd into dlang:master Oct 19, 2020
@kinke kinke deleted the copyEmplace branch October 19, 2020 16:50
kinke added a commit to kinke/druntime that referenced this pull request Oct 21, 2020
A tiny follow-up to dlang#3239. Trying to use it in std.variant showed this
issue.
kinke added a commit to kinke/druntime that referenced this pull request Oct 31, 2020
A tiny follow-up to dlang#3239. Trying to use it in std.variant showed this
issue.
n8sh pushed a commit to n8sh/druntime that referenced this pull request Mar 3, 2021
A tiny follow-up to dlang#3239. Trying to use it in std.variant showed this
issue.
* source = value to be copied into target
* target = uninitialized value to be initialized with a copy of source
*/
void copyEmplace(S, T)(ref S source, ref T target) @system
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this marked as @system?

Copy link
Contributor Author

@kinke kinke Oct 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it was because of overwriting target, without destructing it first, so it's dangerous.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh and because it accepts non-mutable target types T too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants