Skip to content

Refactor Request/Response TryFrom impl to allow '?' on fallible calls#108

Closed
DevinR528 wants to merge 1 commit into
ruma:masterfrom
DevinR528:tryfrom-refactor
Closed

Refactor Request/Response TryFrom impl to allow '?' on fallible calls#108
DevinR528 wants to merge 1 commit into
ruma:masterfrom
DevinR528:tryfrom-refactor

Conversation

@DevinR528

Copy link
Copy Markdown
Member

Work toward issue #6

let event_type = ::ruma_events::EventContent::event_type(&self.content);

let mut state = serializer.serialize_struct(stringify!(#ident), 7)?;
let mut state = serializer.serialize_struct(stringify!(#ident), 9)?;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I can remove this I just wanted to remember to ask if we should be dealing with this and is there any chance this could be part of the bincode deserialization problem @MTRNord was asking about on matrix (either in the Ruma room or in matrix-rust-sdk)?

@MTRNord MTRNord Jul 4, 2020

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@jplatte had some ideas why bincode doesn't work I think. json does work fine (obviously. this is matrix. if it doesn't do json this wouldn't be matrix).

I still dont understand why it doesn't work with bincode :D I just removed bincode from my project

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No, this has nothing to do with the bincode "expected valid JSON" error. If you want more details about that, ping me on Matrix.

ResponseField::NewtypeRawBody(_) => {
quote_spanned! {span=>
#field_name: response.into_body()
#field_name: resp.body().to_vec()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I was wondering whether there would be a case like this. This now being a full copy of the response makes my whole idea moot unfortunately¹. I don't want to have worse runtime performance for somewhat nicer generated code.

I have another idea though: We could hide the repetitive

match <expr> {
    Ok(val) => val,
    Err(e) => return <error type>::new(<actual error>, <reponse / request>).into(),
}

behind a helper function (or two, one for requests and one for responses). I hope that would make the macro code easier to read.

Does that makes sense to you? Do you have other ideas for how to make the code more readable?

¹: If you want a more in-depth explanation of why I don't think this can work (without try blocks 😄), ping me on Matrix.

@DevinR528 DevinR528 closed this Jul 5, 2020
@DevinR528 DevinR528 deleted the tryfrom-refactor branch May 3, 2021 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants