Skip to content
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

Re-introduce object param parsing #526

Merged
merged 2 commits into from
Oct 15, 2021
Merged

Conversation

maciejhirsz
Copy link
Contributor

@maciejhirsz maciejhirsz commented Oct 15, 2021

Fixes #445.

The trick is that instead of putting fn argument types (which may be generic) on the struct like it used to, e.g.:

#[derive(jsonrpsee :: types :: __reexports :: serde :: Deserialize)]
#[serde(crate = "jsonrpsee :: types :: __reexports :: serde")]
struct ParamsObject {
    gun: Gun,
    map: HashMap<u8, u8>,
}
let parsed: ParamsObject = params.parse()?;

We make a struct that's entirely generic, so that all types are always used in scope where they belong:

#[derive(jsonrpsee :: types :: __reexports :: serde :: Deserialize)]
#[serde(crate = "jsonrpsee :: types :: __reexports :: serde")]
struct ParamsObject<G0, G1> {
    gun: G0,
    map: G1,
}
let parsed: ParamsObject<Gun, HashMap<u8, u8>> = params.parse()?;

As a bonus this removes the need to inject lifetimes into references (no need to transform &str into &'a str etc.), so that whole thing can be removed.

Copy link
Member

@niklasad1 niklasad1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 , nice your wizard

@dvdplm
Copy link
Contributor

dvdplm commented Oct 15, 2021

As a bonus this removes the need to inject lifetimes into references (no need to transform &str into &'a str etc.), so that whole thing can be removed.

That is … magic!

@maciejhirsz maciejhirsz merged commit af16b39 into master Oct 15, 2021
@maciejhirsz maciejhirsz deleted the mh-support-map-params branch October 15, 2021 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[proc macros]: support for named parameters (JSON Map/Object)
3 participants