Fix container ctors and opAssign ref parameters

Container constructors and opAssign should accept any ref container and
not only const, otherwise the source container will be copied because
the constructor/opAssign without ref would be a better match.
This commit is contained in:
Eugen Wissner 2017-05-01 09:48:12 +02:00
parent 65c3ca14ec
commit f2aac680c5
3 changed files with 16 additions and 18 deletions

View File

@ -215,16 +215,19 @@ struct SList(T)
* If $(D_PARAM init) is passed by reference, it will be copied. * If $(D_PARAM init) is passed by reference, it will be copied.
* *
* Params: * Params:
* R = Source list type.
* init = Source list. * init = Source list.
* allocator = Allocator. * allocator = Allocator.
*/ */
this(ref SList init, shared Allocator allocator = defaultAllocator) this(R)(ref R init, shared Allocator allocator = defaultAllocator)
if (is(Unqual!R == SList))
{ {
this(init[], allocator); this(init[], allocator);
} }
/// Ditto. /// Ditto.
this(SList init, shared Allocator allocator = defaultAllocator) @trusted this(R)(R init, shared Allocator allocator = defaultAllocator) @trusted
if (is(R == SList))
{ {
this(allocator); this(allocator);
if (allocator is init.allocator) if (allocator is init.allocator)
@ -734,18 +737,19 @@ struct SList(T)
* *
* Returns: $(D_KEYWORD this). * Returns: $(D_KEYWORD this).
*/ */
ref typeof(this) opAssign(R)(const ref R that) ref typeof(this) opAssign(R)(ref R that)
if (is(Unqual!R == SList)) if (is(Unqual!R == SList))
{ {
return this = that[]; return this = that[];
} }
/// Ditto. /// Ditto.
ref typeof(this) opAssign(R)(const ref R that) ref typeof(this) opAssign(R)(R that)
if (is(Unqual!R == SList)) if (is(R == SList))
{ {
swap(this.head, that.head); swap(this.head, that.head);
swap(this.allocator_, that.allocator_); swap(this.allocator_, that.allocator_);
return this;
} }
/** /**

View File

@ -225,11 +225,11 @@ struct Vector(T)
* If $(D_PARAM init) is passed by reference, it will be copied. * If $(D_PARAM init) is passed by reference, it will be copied.
* *
* Params: * Params:
* R = Vector type. * R = Source vector type.
* init = Source vector. * init = Source vector.
* allocator = Allocator. * allocator = Allocator.
*/ */
this(R)(const ref R init, shared Allocator allocator = defaultAllocator) this(R)(ref R init, shared Allocator allocator = defaultAllocator)
if (is(Unqual!R == Vector)) if (is(Unqual!R == Vector))
{ {
this(allocator); this(allocator);
@ -1413,7 +1413,7 @@ struct Vector(T)
* *
* Returns: $(D_KEYWORD this). * Returns: $(D_KEYWORD this).
*/ */
ref typeof(this) opAssign(R)(const ref R that) ref typeof(this) opAssign(R)(ref R that)
if (is(Unqual!R == Vector)) if (is(Unqual!R == Vector))
{ {
return this = that[]; return this = that[];

View File

@ -245,16 +245,10 @@ struct Integer
ref Integer opAssign(T)(T value) nothrow @safe @nogc ref Integer opAssign(T)(T value) nothrow @safe @nogc
if (is(T == Integer)) if (is(T == Integer))
{ {
if (this.allocator is value.allocator) swap(this.rep, value.rep);
{ swap(this.sign, value.sign);
swap(this.rep, value.rep); swap(this.size, value.size);
swap(this.sign, value.sign); swap(this.allocator_, value.allocator_);
swap(this.size, value.size);
}
else
{
this = value;
}
return this; return this;
} }