This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 411
implement dup and idup library functions #760
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /** | ||
| * Contains traits for runtime internal usage. | ||
| * | ||
| * Copyright: Copyright Digital Mars 2014 -. | ||
| * License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>. | ||
| * Authors: Martin Nowak | ||
| * Source: $(DRUNTIMESRC core/internal/_traits.d) | ||
| */ | ||
| module core.internal.traits; | ||
|
|
||
| /// taken from std.typetuple.TypeTuple | ||
| template TypeTuple(TList...) | ||
| { | ||
| alias TypeTuple = TList; | ||
| } | ||
|
|
||
| T trustedCast(T, U)(auto ref U u) @trusted pure nothrow | ||
| { | ||
| return cast(T)u; | ||
| } | ||
|
|
||
| template Unconst(T) | ||
| { | ||
| static if (is(T U == immutable U)) alias Unconst = U; | ||
| else static if (is(T U == inout const U)) alias Unconst = U; | ||
| else static if (is(T U == inout U)) alias Unconst = U; | ||
| else static if (is(T U == const U)) alias Unconst = U; | ||
| else alias Unconst = T; | ||
| } | ||
|
|
||
| /// taken from std.traits.Unqual | ||
| template Unqual(T) | ||
| { | ||
| version (none) // Error: recursive alias declaration @@@BUG1308@@@ | ||
| { | ||
| static if (is(T U == const U)) alias Unqual = Unqual!U; | ||
| else static if (is(T U == immutable U)) alias Unqual = Unqual!U; | ||
| else static if (is(T U == inout U)) alias Unqual = Unqual!U; | ||
| else static if (is(T U == shared U)) alias Unqual = Unqual!U; | ||
| else alias Unqual = T; | ||
| } | ||
| else // workaround | ||
| { | ||
| static if (is(T U == immutable U)) alias Unqual = U; | ||
| else static if (is(T U == shared inout const U)) alias Unqual = U; | ||
| else static if (is(T U == shared inout U)) alias Unqual = U; | ||
| else static if (is(T U == shared const U)) alias Unqual = U; | ||
| else static if (is(T U == shared U)) alias Unqual = U; | ||
| else static if (is(T U == inout const U)) alias Unqual = U; | ||
| else static if (is(T U == inout U)) alias Unqual = U; | ||
| else static if (is(T U == const U)) alias Unqual = U; | ||
| else alias Unqual = T; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -628,3 +628,95 @@ version (unittest) | |
| } | ||
| } | ||
| } | ||
|
|
||
| private extern (C) void[] _d_newarrayU(const TypeInfo ti, size_t length) pure nothrow; | ||
|
|
||
| /// Provide the .dup array property. | ||
| auto dup(T)(T[] a) | ||
| if (!is(const(T) : T)) | ||
| { | ||
| import core.internal.traits : Unconst; | ||
| static assert(is(T : Unconst!T), "Cannot implicitly convert type "~T.stringof~ | ||
| " to "~Unconst!T.stringof~" in dup."); | ||
|
|
||
| // wrap unsafe _dup in @trusted to preserve @safe postblit | ||
| static if (__traits(compiles, (T b) @safe { T a = b; })) | ||
| return _trustedDup!(T, Unconst!T)(a); | ||
| else | ||
| return _dup!(T, Unconst!T)(a); | ||
| } | ||
|
|
||
| /// ditto | ||
| // const overload to support implicit conversion to immutable (unique result, see DIP29) | ||
| T[] dup(T)(const(T)[] a) | ||
| if (is(const(T) : T)) | ||
| { | ||
| // wrap unsafe _dup in @trusted to preserve @safe postblit | ||
| static if (__traits(compiles, (T b) @safe { T a = b; })) | ||
| return _trustedDup!(const(T), T)(a); | ||
| else | ||
| return _dup!(const(T), T)(a); | ||
| } | ||
|
|
||
| /// Provide the .idup array property. | ||
| immutable(T)[] idup(T)(T[] a) | ||
| { | ||
| static assert(is(T : immutable(T)), "Cannot implicitly convert type "~T.stringof~ | ||
| " to immutable in idup."); | ||
|
|
||
| // wrap unsafe _dup in @trusted to preserve @safe postblit | ||
| static if (__traits(compiles, (T b) @safe { T a = b; })) | ||
| return _trustedDup!(T, immutable(T))(a); | ||
| else | ||
| return _dup!(T, immutable(T))(a); | ||
| } | ||
|
|
||
| private U[] _trustedDup(T, U)(T[] a) @trusted | ||
| { | ||
| return _dup!(T, U)(a); | ||
| } | ||
|
|
||
| private U[] _dup(T, U)(T[] a) // pure nothrow depends on postblit | ||
| { | ||
| if (__ctfe) | ||
| { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the meantime I've submitted a pull to fix this issue dlang/dmd#3424. |
||
| U[] res; | ||
| foreach (ref e; a) | ||
| res ~= e; | ||
| return res; | ||
| } | ||
|
|
||
| import core.stdc.string : memcpy; | ||
|
|
||
| auto arr = _d_newarrayU(typeid(T[]), a.length); | ||
| memcpy(cast(void*)arr.ptr, cast(void*)a.ptr, T.sizeof * a.length); | ||
| auto res = *cast(typeof(return)*)&arr; | ||
| _doPostblit(res); | ||
| return res; | ||
| } | ||
|
|
||
| private void _doPostblit(T)(T[] ary) | ||
| { | ||
| // infer static postblit type, run postblit if any | ||
| static if (is(T == struct)) | ||
| { | ||
| import core.internal.traits : Unqual; | ||
|
|
||
| alias PostBlitT = typeof(function(void*){T a = T.init, b = a;}); | ||
| // use typeid(Unqual!T) here to skip TypeInfo_Const/Shared/... | ||
| auto postBlit = cast(PostBlitT)typeid(Unqual!T).xpostblit; | ||
| if (postBlit !is null) | ||
| { | ||
| foreach (ref el; ary) | ||
| postBlit(cast(void*)&el); | ||
| } | ||
| } | ||
| else if ((&typeid(T).postblit).funcptr !is &TypeInfo.postblit) | ||
| { | ||
| alias PostBlitT = typeof(delegate(void*){T a = T.init, b = a;}); | ||
| auto postBlit = cast(PostBlitT)&typeid(T).postblit; | ||
|
|
||
| foreach (ref el; ary) | ||
| postBlit(cast(void*)&el); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose of the _ name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you know selective imports may currently leak into other modules and they might also cause collisions in code importing this module. So the underscore avoids breaking code, e.g. if someone imports core.time and std.traits using Unqual would be ambiguous.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since Unqual is identical between them, then either std.traits should import core.internal.traits. or core.internal.traits.Unqual should have a different name than Unqual. Forcing every user of core.internal.traits to do those _ aliases is kinda awful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd use core.internal only in the runtime for now and see where this goes. I don't intend to drag over too much template machinery.
You're sidestepping the underlying issue, we need to fix the problem that private imports/symbols cause collisions in other modules.