Skip to content

Commit

Permalink
typecons documentation update
Browse files Browse the repository at this point in the history
* moved examples into unittests
  • Loading branch information
burner committed Jan 29, 2015
1 parent 224238a commit 5f633b7
Showing 1 changed file with 77 additions and 69 deletions.
146 changes: 77 additions & 69 deletions std/typecons.d
Expand Up @@ -1134,19 +1134,6 @@ unittest
/**
Returns a $(D Tuple) object instantiated and initialized according to
the arguments.
Example:
----
auto value = tuple(5, 6.7, "hello");
assert(value[0] == 5);
assert(value[1] == 6.7);
assert(value[2] == "hello");
// Field names can be provided.
auto entry = tuple!("index", "value")(4, "Hello");
assert(entry.index == 4);
assert(entry.value == "Hello");
----
*/

template tuple(Names...)
Expand Down Expand Up @@ -1190,6 +1177,20 @@ template tuple(Names...)
}
}

///
unittest
{
auto value = tuple(5, 6.7, "hello");
assert(value[0] == 5);
assert(value[1] == 6.7);
assert(value[2] == "hello");

// Field names can be provided.
auto entry = tuple!("index", "value")(4, "Hello");
assert(entry.index == 4);
assert(entry.value == "Hello");
}

/**
Returns $(D true) if and only if $(D T) is an instance of the
$(D Tuple) struct template.
Expand All @@ -1206,14 +1207,18 @@ template isTuple(T)
}
}

///
unittest
{
static assert(isTuple!(Tuple!()));
static assert(isTuple!(Tuple!(int)));
static assert(isTuple!(Tuple!(int, real, string)));
static assert(isTuple!(Tuple!(int, "x", real, "y")));
static assert(isTuple!(Tuple!(int, Tuple!(real), string)));
}

unittest
{
static assert(isTuple!(const Tuple!(int)));
static assert(isTuple!(immutable Tuple!(int)));

Expand Down Expand Up @@ -1489,14 +1494,6 @@ unittest
/**
Order the provided members to minimize size while preserving alignment.
Returns a declaration to be mixed in.
Example:
---
struct Banner {
mixin(alignForSize!(byte[6], double)(["name", "height"]));
}
---
Alignment is not always optimal for 80-bit reals, nor for structs declared
as align(1).
*/
Expand Down Expand Up @@ -1528,6 +1525,14 @@ string alignForSize(E...)(string[] names...)
return s;
}

///
unittest
{
struct Banner {
mixin(alignForSize!(byte[6], double)(["name", "height"]));
}
}

unittest
{
enum x = alignForSize!(int[], char[3], short, double[5])("x", "y","z", "w");
Expand All @@ -1551,15 +1556,6 @@ the absence of a value. If default constructed, a $(D
Nullable!T) object starts in the null state. Assigning it renders it
non-null. Calling $(D nullify) can nullify it again.
Example:
----
Nullable!int a;
assert(a.isNull);
a = 5;
assert(!a.isNull);
assert(a == 5);
----
Practically $(D Nullable!T) stores a $(D T) and a $(D bool).
*/
struct Nullable(T)
Expand Down Expand Up @@ -1638,6 +1634,16 @@ $(D this) must not be in the null state.
alias get this;
}

///
unittest
{
Nullable!int a;
assert(a.isNull);
a = 5;
assert(!a.isNull);
assert(a == 5);
}

unittest
{
import std.exception : assertThrown;
Expand Down Expand Up @@ -4021,20 +4027,6 @@ autoInit == RefCountedAutoInitialize.no), user code must call either
$(D refCountedStore.isInitialized) or $(D refCountedStore.ensureInitialized)
before attempting to access the payload. Not doing so results in null
pointer dereference.
Example:
----
// A pair of an $(D int) and a $(D size_t) - the latter being the
// reference count - will be dynamically allocated
auto rc1 = RefCounted!int(5);
assert(rc1 == 5);
// No more allocation, add just one extra reference count
auto rc2 = rc1;
// Reference semantics
rc2 = 42;
assert(rc1 == 42);
// the pair will be freed when rc1 and rc2 go out of scope
----
*/
struct RefCounted(T, RefCountedAutoInitialize autoInit =
RefCountedAutoInitialize.yes)
Expand Down Expand Up @@ -4228,6 +4220,21 @@ assert(refCountedStore.isInitialized)).
alias refCountedPayload this;
}

///
unittest
{
// A pair of an $(D int) and a $(D size_t) - the latter being the
// reference count - will be dynamically allocated
auto rc1 = RefCounted!int(5);
assert(rc1 == 5);
// No more allocation, add just one extra reference count
auto rc2 = rc1;
// Reference semantics
rc2 = 42;
assert(rc1 == 42);
// the pair will be freed when rc1 and rc2 go out of scope
}

unittest
{
RefCounted!int* p;
Expand Down Expand Up @@ -4313,30 +4320,6 @@ unittest

/**
Make proxy for $(D a).
Example:
----
struct MyInt
{
private int value;
mixin Proxy!value;
this(int n){ value = n; }
}
MyInt n = 10;
// Enable operations that original type has.
++n;
assert(n == 11);
assert(n * 2 == 22);
void func(int n) { }
// Disable implicit conversions to original type.
//int x = n;
//func(n);
----
*/
mixin template Proxy(alias a)
{
Expand Down Expand Up @@ -4522,6 +4505,31 @@ mixin template Proxy(alias a)
}
}

///
unittest
{
struct MyInt
{
private int value;
mixin Proxy!value;

this(int n){ value = n; }
}

MyInt n = 10;

// Enable operations that original type has.
++n;
assert(n == 11);
assert(n * 2 == 22);

void func(int n) { }

// Disable implicit conversions to original type.
//int x = n;
//func(n);
}

unittest
{
static struct MyInt
Expand Down Expand Up @@ -5584,8 +5592,8 @@ struct No
enum opDispatch = Flag!name.no;
}
}
//template no(string name) { enum Flag!name no = Flag!name.no; }

///
unittest
{
Flag!"abc" flag1;
Expand Down

0 comments on commit 5f633b7

Please sign in to comment.