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 is_compatible method to event content enums to fix Any*Event deserialization #52
Conversation
I wouldn't want to hardcode more knowledge about the specific event types in the macros. I was thinking more of adding methods to the generated event content enums to determine which variant to deserialize. |
This would still require a manual/custom derive for the |
I'm not sure it's worth making it a macro. It could just be one implementation for |
How do I get at the actual deserialized content? // inside of AnyEvent Deserialize impl
let json = JsonValue::deserialize(deserializer)?;
let raw: Box<RawJsonValue> = util::get_field(&json, "content")?;
let ev_type: String = util::get_field(&json, "type")?;
let content: ?????? =
ruma_events::EventContent::from_parts(&ev_type, raw).map_err(D::Error::custom)?;
// then call the Any*EventContent's new method to figure out the variant Do I need to manually do what |
I was thinking of something like // for AnyRoomEvent
match ev_type.as_str() {
"m.room.redaction" => Self::Redaction(from_json_value(json).map_err(...)?),
t if AnyMessageEventContent::is_compatible(t) => Self::Message(from_json_value(json).map_err(...)?),
t if AnyStateEventContent::is_compatible(t) => Self::State(from_json_value(json).map_err(...)?),
} |
d8106d4
to
ca69ed9
Compare
@@ -1,2 +1,2 @@ | |||
/target | |||
target |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
criterion
generates a ruma-events/target dir I figured this should be ignored?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. I don't like it doing that, but I guess we can't do much about it.
// Since we are in a workspace the default `cargo bench` still picks up | ||
// the args passed to it to avoid this use the above command. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand this comment. What's "the default cargo bench
"? Also, it's not clear to me what is avoided.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing this is about the -- --save-baseline
syntax, but that's just an established convention for unix-style CLI tools – don't process stuff after a --
as flags / options. We don't need to document that here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cargo bench
works but if you use criterions suggestion cargo bench -- --save-baseline <name>
it fails with the error cargo bench unknown option --save-baseline
I'm assuming this has something to do with the fact that we are in a workspace? I should change it to say the above and that the workaround is
cargo bench --bench event_deserialize -- --save-baseline <name>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know what that error is about. Does it make a difference whether you run that command from the repo root vs ruma-events
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope at first, I thought it did but it gave me the same error both times. I've had this happen before turning off the bench harness worked the other time I found this and used the [lib] harness = false
so I have no idea exactly why it won't work but I'd figure workspaces still being a bit flaky with some things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what I have now
// `cargo bench` works but if you use `cargo bench -- --save-baseline <name>`
// or pass any other args to it it fails with the error
// `cargo bench unknown option --save-baseline`.
// To pass args to criterion use this form
// `cargo bench --bench <name of the bench> -- --save-baseline <name>`
…evels of specificity
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks pretty good. What do the benchmark results look like on your machine?
So fully generic is about 1/2 as fast as even The |
ffb6fdf
to
c2600f9
Compare
c2600f9
to
be80107
Compare
Will something like this work?