-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add @serdeProxyCast
, @serdeEnumProxy
UDAs
#408
Conversation
Why it is needed? The following code works: import mir.ser.text;
import mir.deser.text;
@serdeProxy!int
enum Foo
{
a = 1,
b = 2
}
assert(Foo.b.serializeText == "2");
assert("2".deserializeText!Foo == Foo.b);
|
Ah, got it. |
But that works as well: import mir.ser.text;
import mir.deser.text;
@serdeProxy!int
enum Foo
{
a = 1,
b = 2
}
assert((cast(Foo)6).serializeText == "6", Foo.b.serializeText);
assert("6".deserializeText!Foo == 6);
|
there is no change in how serialization works here, it partly worked before and was kind of inconsistent, that's why I added a new UDA instead of trying to change behavior in one way or the other. The mir-ion unittest also checks for this for known currently broken/valid cases: |
A special user-defined conversion logic can be done using template MyEnumProxyImpl(T)
{
@serdeProxy!T
struct MyEnumProxyImpl
{
T value;
// implement conversion To/From Enum's of T types AND base T
}
}
alias MyEnumProxy(T) = serdeProxy!(MyEnumProxyImpl!T))
@MyEnumProxy!int
enum E { a, b } Can this work for you? |
ok this worked, but imo this is something worth adding to mir: template MyEnumProxyImpl(T)
{
@serdeProxy!T
struct MyEnumProxyImpl
{
T value;
alias value this;
this(T value)
{
this.value = value;
}
}
}
alias serdeEnumProxy(T) = serdeProxy!(MyEnumProxyImpl!T); |
nvm that serdeEnumProxy implementation breaks with variants as it's a struct and doesn't get matched. I still think the addition with this PR (and the one in mir-ion) would be the most stable and most efficient. at least this is an example exception I get from this:
|
Implementation is needed on the mir-ion side for use though
@9il can we get this in? The user defined struct breaks Mir's Algebraic and fixing that is hard, while the proxy cast has a super simple implementation (just using I currently need this for my serve-d mir-ion port where I have a bunch of |
* Support `@serdeEnumProxy` (`@serdeProxyCast`) UDA See libmir/mir-algorithm#408 * adjust to review
PR on the mir-ion side will follow shortly.
Idea is that you can do this:
and Foo will be