kinda weird constructing the serialization/deserialization for #activityPub types in #rust because it does mean I'm doing a lot of curl requests against my own server (and those of other people) to see what the raw data looks like in the wild
-
Asta [AMP]replied to jonny (good kind) last edited by
@[email protected] @[email protected] I think you could do this with a procedural derive macro. If the struct you run the macro on is self-contained in that it has all the information you'd need to construct your new types, then absolutely.
I feel like all the examples given of proc_macro_derive are actually... really kind of useless because they take in the AST and then "leave it as an exercise to the reader to actually use the fucking AST in any way".
I have a (convoluted and old) example here, where I basically create SI prefixed variants of field-less structs and create a set of operator overloads to allow you to do order of magnitude correct math on them (even including machine precision; if they're too far apart it won't create that addition/subtraction/etc operator overload)
https://github.com/astatide/decay/blob/dbe1dd32af27072966140f92fb0bf6645626d140/crates/decay_si_derive/src/lib.rs#L72
it's a little nasty but so long as you define your struct in a very specific way I think you can create whatever you want.
In this case I just create a new string and just start appending lines of code to it. It creates a whole new set of structs and operators.
You can see the test/result here: https://github.com/astatide/decay/blob/dbe1dd32af27072966140f92fb0bf6645626d140/crates/decay_si/src/lib.rs#L23
I kinda hate it and love it -
Asta [AMP]replied to Asta [AMP] last edited by [email protected]
@[email protected] @[email protected] (it's super not rust like but I don't really care as I feel just because it's not 'standard' doesn't mean it's unsafe w/ regards to types). It could be a bit of a footgun, admittedly.
let kM = KiloMeter; let mut d = kM(1.0); // 1.0 d += 1.0; // 2.0 assert_eq!(*d, 2.0); d += Meter(1.0); // does not convert to a meter now yay. 2.001! d += 1.0; // Now 3.001
-
Asta [AMP]replied to Asta [AMP] last edited by [email protected]
@[email protected] @[email protected] fuuuuuuck I still love operator overloads
I used them once in a genetic algorithm to do complex handling of data in a trivial, logical way. I fucking love group theory and I feel like people in comp sci tend to not for whatever reason.
If I want to "add" two apples and 3 bananas together I sure as fuck should be allowed to because "add" is an arbitrary operation over a group of "fruits" and if I want that to return a type "basket of fruits" by god who the fuck are you to stop me.