Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions import/object.di
Original file line number Diff line number Diff line change
Expand Up @@ -545,12 +545,16 @@ unittest
assert(b == [ 1:"one", 2:"two", 3:"three" ]);
}

void clear(T)(T obj) if (is(T == class))
// Scheduled for deprecation in December 2012.
// Please use destroy instead of clear.
alias destroy clear;

void destroy(T)(T obj) if (is(T == class))
{
rt_finalize(cast(void*)obj);
}

void clear(T)(ref T obj) if (is(T == struct))
void destroy(T)(ref T obj) if (is(T == struct))
{
typeid(T).destroy(&obj);
auto buf = (cast(ubyte*) &obj)[0 .. T.sizeof];
Expand All @@ -561,12 +565,12 @@ void clear(T)(ref T obj) if (is(T == struct))
buf[] = init[];
}

void clear(T : U[n], U, size_t n)(ref T obj)
void destroy(T : U[n], U, size_t n)(ref T obj)
{
obj = T.init;
}

void clear(T)(ref T obj)
void destroy(T)(ref T obj)
if (!is(T == struct) && !is(T == class) && !_isStaticArray!T)
{
obj = T.init;
Expand Down
36 changes: 23 additions & 13 deletions src/object_.d
Original file line number Diff line number Diff line change
Expand Up @@ -2243,7 +2243,17 @@ unittest
assert(a == b);
}

void clear(T)(T obj) if (is(T == class))
// Scheduled for deprecation in December 2012.
Copy link
Contributor

Choose a reason for hiding this comment

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

Does having DDoc on aliases work? If so, this could probably be made a doc comment.

Copy link
Contributor

Choose a reason for hiding this comment

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

They don't; we have the same problem whenever we want to deprecate equals_t.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, they do work. For instance, DateTimeException is an alias for TimeException, and it shows up in the docs. I just didn't put ddoc on the alias, because clear wasn't actually documented in the first place.

// Please use destroy instead of clear.
alias destroy clear;

/++
Destroys the given object and puts it in an invalid state. It's used to
destroy an object so that any cleanup which its destructor or finalizer
does is done and so that it no longer references any other objects. It does
$(I not) initiate a GC cycle or free any GC memory.
+/
void destroy(T)(T obj) if (is(T == class))
{
rt_finalize(cast(void*)obj);
}
Expand All @@ -2254,7 +2264,7 @@ version(unittest) unittest
class A { string s = "A"; this() {} }
auto a = new A;
a.s = "asd";
clear(a);
destroy(a);
assert(a.s == "A");
}
{
Expand All @@ -2270,7 +2280,7 @@ version(unittest) unittest
}
auto a = new B;
a.s = "asd";
clear(a);
destroy(a);
assert(destroyed);
assert(a.s == "B");
}
Expand All @@ -2287,12 +2297,12 @@ version(unittest) unittest
}
auto a = new C;
a.s = "asd";
clear(a);
destroy(a);
assert(a.s == "C");
}
}

void clear(T)(ref T obj) if (is(T == struct))
void destroy(T)(ref T obj) if (is(T == struct))
{
typeid(T).destroy( &obj );
auto buf = (cast(ubyte*) &obj)[0 .. T.sizeof];
Expand All @@ -2309,7 +2319,7 @@ version(unittest) unittest
struct A { string s = "A"; }
A a;
a.s = "asd";
clear(a);
destroy(a);
assert(a.s == "A");
}
{
Expand All @@ -2335,14 +2345,14 @@ version(unittest) unittest
B a;
a.s = "asd";
a.c.s = "jkl";
clear(a);
destroy(a);
assert(destroyed == 2);
assert(a.s == "B");
assert(a.c.s == "C" );
}
}

void clear(T : U[n], U, size_t n)(ref T obj)
void destroy(T : U[n], U, size_t n)(ref T obj)
{
obj = T.init;
}
Expand All @@ -2352,11 +2362,11 @@ version(unittest) unittest
int[2] a;
a[0] = 1;
a[1] = 2;
clear(a);
destroy(a);
assert(a == [ 0, 0 ]);
}

void clear(T)(ref T obj)
void destroy(T)(ref T obj)
if (!is(T == struct) && !is(T == class) && !_isStaticArray!T)
{
obj = T.init;
Expand All @@ -2376,12 +2386,12 @@ version(unittest) unittest
{
{
int a = 42;
clear(a);
destroy(a);
assert(a == 0);
}
{
float a = 42;
clear(a);
destroy(a);
assert(isnan(a));
}
}
Expand Down Expand Up @@ -2455,7 +2465,7 @@ version (unittest) unittest

version (none)
{
// enforce() copied from Phobos std.contracts for clear(), left out until
// enforce() copied from Phobos std.contracts for destroy(), left out until
// we decide whether to use it.


Expand Down