Compare commits
No commits in common. "main" and "v1.9.2" have entirely different histories.
|
|
@ -123,11 +123,11 @@ struct Optional(T) {
|
||||||
* Params:
|
* Params:
|
||||||
* serializer = The serializer to use (provided by ASDF).
|
* serializer = The serializer to use (provided by ASDF).
|
||||||
*/
|
*/
|
||||||
void serialize(S)(ref S serializer) const {
|
void serialize(S)(ref S serializer) {
|
||||||
if (this.isNull) {
|
if (this.isNull) {
|
||||||
serializer.putValue(null);
|
serializer.putValue(null);
|
||||||
} else {
|
} else {
|
||||||
serializeValue(serializer, this.value);
|
serializer.putValue(this.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -147,18 +147,6 @@ auto mapIfPresent(alias fn, T)(Optional!T opt) {
|
||||||
return Optional!U.of(fn(opt.value));
|
return Optional!U.of(fn(opt.value));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper function to get an Optional value for an existing value. Due to D's
|
|
||||||
* type inference, you can simply write `auto opt = toOptional(x);` to avoid
|
|
||||||
* having to write out types when constructing optionals.
|
|
||||||
* Params:
|
|
||||||
* t = The value to construct an optional from.
|
|
||||||
* Returns: The optional with the given value.
|
|
||||||
*/
|
|
||||||
Optional!T toOptional(T)(T t) {
|
|
||||||
return Optional!T.of(t);
|
|
||||||
}
|
|
||||||
|
|
||||||
unittest {
|
unittest {
|
||||||
Optional!string s = Optional!string.of("hello");
|
Optional!string s = Optional!string.of("hello");
|
||||||
assert(!s.isNull);
|
assert(!s.isNull);
|
||||||
|
|
@ -208,7 +196,7 @@ unittest {
|
||||||
immutable int x;
|
immutable int x;
|
||||||
}
|
}
|
||||||
Optional!Invalid opt = Optional!(Invalid).of(Invalid(42));
|
Optional!Invalid opt = Optional!(Invalid).of(Invalid(42));
|
||||||
static assert(__traits(compiles, serializeToJson(opt)));
|
static assert(!__traits(compiles, serializeToJson(opt)));
|
||||||
try {
|
try {
|
||||||
deserialize!(Optional!(Invalid))(`{"x": 123}`);
|
deserialize!(Optional!(Invalid))(`{"x": 123}`);
|
||||||
assert(
|
assert(
|
||||||
|
|
@ -220,11 +208,3 @@ unittest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests for toOptional.
|
|
||||||
unittest {
|
|
||||||
int x = 5;
|
|
||||||
auto optX = x.toOptional;
|
|
||||||
assert(!optX.isNull);
|
|
||||||
assert(optX.value == 5);
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue