Compare commits
No commits in common. "main" and "v1.9.0" have entirely different histories.
|
|
@ -88,9 +88,6 @@ struct Optional(T) {
|
|||
version (Have_asdf) {
|
||||
import asdf;
|
||||
|
||||
static if (
|
||||
__traits(compiles, deserialize!T(Asdf.init))
|
||||
) {
|
||||
/**
|
||||
* Deserializes an optional value from raw ASDF data. This function
|
||||
* is defined to allow for automatic deserialization of Optionals when
|
||||
|
|
@ -100,12 +97,6 @@ struct Optional(T) {
|
|||
* Returns: An exception if one is thrown.
|
||||
*/
|
||||
SerdeException deserializeFromAsdf(Asdf data) {
|
||||
static if (!__traits(compiles, {T t; t = T.init;})) {
|
||||
return new SerdeException(
|
||||
"Cannot deserialize Optional that contains immutable or otherwise unassignable value. " ~
|
||||
"Ensure that the following code compiles: T t; t = T.init; for the given type T."
|
||||
);
|
||||
} else {
|
||||
if (data == null) {
|
||||
this.isNull = true;
|
||||
this.value = T.init;
|
||||
|
|
@ -115,7 +106,6 @@ struct Optional(T) {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes an optional value to allow for the automatic serialization
|
||||
|
|
@ -123,12 +113,11 @@ struct Optional(T) {
|
|||
* Params:
|
||||
* serializer = The serializer to use (provided by ASDF).
|
||||
*/
|
||||
void serialize(S)(ref S serializer) const {
|
||||
void serialize(S)(ref S serializer) {
|
||||
if (this.isNull) {
|
||||
serializer.putValue(null);
|
||||
} else {
|
||||
serializeValue(serializer, this.value);
|
||||
}
|
||||
serializer.putValue(this.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -147,18 +136,6 @@ auto mapIfPresent(alias fn, T)(Optional!T opt) {
|
|||
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 {
|
||||
Optional!string s = Optional!string.of("hello");
|
||||
assert(!s.isNull);
|
||||
|
|
@ -202,29 +179,5 @@ unittest {
|
|||
assert(sa.b && sa.b.value == "hello world!");
|
||||
assert(sa.c && sa.c.value == 3.14f);
|
||||
assert(sa.d && sa.d.value == Sd(42, 67));
|
||||
|
||||
// Check that optionals containing immutable data aren't supported for serialization & deserialization:
|
||||
struct Invalid {
|
||||
immutable int x;
|
||||
}
|
||||
Optional!Invalid opt = Optional!(Invalid).of(Invalid(42));
|
||||
static assert(__traits(compiles, serializeToJson(opt)));
|
||||
try {
|
||||
deserialize!(Optional!(Invalid))(`{"x": 123}`);
|
||||
assert(
|
||||
false,
|
||||
"Failed to ensure that an exception is thrown when attempting to deserialize an incompatible Optional."
|
||||
);
|
||||
} catch (SerdeException exc) {
|
||||
// pass.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Tests for toOptional.
|
||||
unittest {
|
||||
int x = 5;
|
||||
auto optX = x.toOptional;
|
||||
assert(!optX.isNull);
|
||||
assert(optX.value == 5);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue