Skip to content

Commit

Permalink
Support transfer to unique base class
Browse files Browse the repository at this point in the history
Note: Implicit return of a Derived lvalue -> Base is not supported.
Note: We don't need to disable the ref overload because postblit is
disabled now.
  • Loading branch information
ntrel committed Jul 18, 2014
1 parent 54bdef2 commit 994137a
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions std/typecons.d
Expand Up @@ -82,7 +82,7 @@ public:
isn't just a view on an lvalue (e.g., a cast).
Typical usage:
----
Unique!(Foo) f = new Foo;
Unique!Foo f = new Foo;
----
*/
this(RefT p)
Expand All @@ -102,32 +102,26 @@ public:
p = null;
assert(p is null);
}
/+ Doesn't work yet
/**
Constructor that takes a Unique of a type that is convertible to our type:
Disallow construction from lvalue (force the use of release on the source Unique)
If the source is an rvalue, null its content, so the destructor doesn't delete it
Typically used by the compiler to return $(D Unique) of derived type as $(D Unique)
of base type.
Constructor that takes a $(D Unique) of a type that is convertible to our type.
Typically used to transfer a $(D Unique) rvalue of derived type to
a $(D Unique) of base type.
Example:
----
Unique!(Base) create()
{
Unique!(Derived) d = new Derived;
return d; // Implicit Derived->Base conversion
}
----
---
class C {}
// Make u hold a new instance of C
Unique!Object u = Unique!C();
---
*/
this(U)(ref Unique!(U) u) = null;
this(U)(Unique!(U) u)
this(U)(Unique!U u)
if (is(u.RefT:RefT))
{
debug(Unique) writeln("Unique constructor converting from ", U.stringof);
_p = u._p;
u._p = null;
}
+/


~this()
{
debug(Unique) writeln("Unique destructor of ", (_p is null)? null: _p);
Expand Down Expand Up @@ -187,6 +181,20 @@ unittest
assert(u1.isEmpty);
}

unittest
{
// test conversion to base ref
class C {}
Unique!Object u = Unique!C();
assert(!u.isEmpty);

Unique!C uc = new C;
static assert(!__traits(compiles, {Unique!Object uo = uc;}));
Unique!Object uo = uc.release;
assert(uc.isEmpty);
assert(!uo.isEmpty);
}

unittest
{
debug(Unique) writeln("Unique class");
Expand Down

0 comments on commit 994137a

Please sign in to comment.