Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
gonadic io
Feb 16, 2011

>>=
Look at the type signatures of the from_str and from_slice methods. Both can parse into anything that implements the Deserialize trait. If you then click on that and scroll down, you'll see that hashmap is one of those things.
It might be that you need a type annotation like you already have.

Adbot
ADBOT LOVES YOU

gonadic io
Feb 16, 2011

>>=

Dijkstracula posted:

Reading about the From and Into traits is where I would start.

While these are good they don't specifically answer the question.

Specifically this is the method signature:
https://docs.rs/serde_json/latest/serde_json/fn.from_slice.html

It returns a type parameter T, where T is a type that implements the trait Deserialize. If you then look at the docs of that trait it lists all the types that implement it, that you can parse into. Also if you used serde_derive on a struct/enum, those types would implement it too.

You absolutely would benefit a lot from reading about traits in general, but the shortest version is that they're a collection of methods. If a type implements a trait, you can call one of those methods on that type. Deserialize is a pretty complicated trait, and not a good first one to investigate (From and Into are much better there).

It is a bit magical to have a polymorphic return signature that's freeform (subject to the trait) like that and not too many things do it. The other big one you'll know is the collect method.

gonadic io fucked around with this message at 23:02 on Sep 26, 2023

gonadic io
Feb 16, 2011

>>=
It being nested is a bit annoying. I've made fake little structs like
code:
TestSubClass { parent: Option<Whatever>,  fields: Vec<(String, IvySort)> }
before, slap an Into impl in there and you can use it for your tests. But it being inside the enum variant means you have to still have an explicit match in there or at least make some test function that contains one. Also you'll want to use the `fail` macro instead of just `panic` in your tests.

gonadic io
Feb 16, 2011

>>=

Dijkstracula posted:

Thanks!

Sorry, do you mean that I can nested matches! calls? I'm fiddling with this but not pulling it off.

No, that you can't use TestSubClass or something like it until you already do the match into the IvySort::Class variant. I would define a get_class method on IvySort that returns an Option<Class> tbh, then in your test you call expect on that , and can then call matches!, that's how the syn library does it.

gonadic io
Feb 16, 2011

>>=
One of the things people from dynamic types don't really appreciate about static types at first is how easy it is to do the kind of refactoring like later adding a field. There's not really a need to leave stuff like that lying around in case you need it when it's easy to just add it when you need it. I personally would comment it out and leave little note for when I came back to it.

gonadic io
Feb 16, 2011

>>=
Also if you're using an ide, which you should be, removing the underscores is like 2 clicks. The two main ones are vscode with rust analyser plug-in, or jetbrains intellij with rust plugin.

gonadic io
Feb 16, 2011

>>=
is locking stdout for a long time like that an issue? I don't really know what its implications are tbh. I personally would be tempted to populate a vec then write out in one go after

gonadic io
Feb 16, 2011

>>=
sure yeah, thanks

gonadic io
Feb 16, 2011

>>=
You definitely want to lock rather than call print in a loop because each print call locks and then unlocks stdout anyway. I meant writing to a vec and then calling write on the vec as you did inside your inner loop. But it's probably a pretty moot point if locking stdout for your program is not an issue

gonadic io
Feb 16, 2011

>>=
being babysat by a compiler owns

gonadic io
Feb 16, 2011

>>=
there's also a -Z for inexact matching

Adbot
ADBOT LOVES YOU

gonadic io
Feb 16, 2011

>>=
my aoc hot tip is the include_str macro (or include_bytes when you want to shave some microseconds). gently caress reading from files at runtime

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply