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

Fix build warnings and a few assorted tweaks #13

Merged
merged 2 commits into from
Sep 19, 2023

Conversation

fu5ha
Copy link
Contributor

@fu5ha fu5ha commented Sep 12, 2023

This makes the repo workspace build under stable by putting the criterion real_blackbox feature under nightly feature in the benchmark crate. More convenient to develop with stable rust-analyzer that way. Also, adds a separate "deserialize" category to the benchmark which deserializes to owned types rather than partially zero-copy reader structs.

It also fixes some various warnings that existed on the code and reworks a bit of the net example to show off creating separate formula types from the actual serialize/deserialize structs.

Copy link
Owner

@zakarumych zakarumych left a comment

Choose a reason for hiding this comment

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

Than you for your PR.

I can't help but wonder why would you separate Formula types from Serialize and Deserialize types.

src/tests/net.rs Outdated
};

#[alkahest(Formula)]
pub enum GameMessageFormula {
Client(Ref<ClientMessageFormula>),
Copy link
Owner

Choose a reason for hiding this comment

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

The Ref looks unnecessary here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yeah, I was just experimenting with it -- this is actually the reason I initially wanted to separate the formula from the serialize and deserialize types, to insert a wrapping handler in the mix. Ultimately I'm trying to play with making a Ref-like wrapper that would help to do schema evolution (forward/backward compatibility). But it is of course unnecessary in this case.

Copy link
Owner

Choose a reason for hiding this comment

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

What exactly are properties of Ref-like wrapper you have in mind?
Current Ref IMHO is enough to allow separate evolution of field's Formula.
Currently user may evolve schema and manually ensure desired compatibility.

I have the following plan to better support them:

  • Add validation to check compatibility between two Formulas.
  • Add attributes to mark new fields when deriving Formula. Then compatibility will be checked between versions. All in proc-macro execution time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, not sure if I quite follow. If you serialize some data with Ref<SomeTV1>, add a field, then try to deserialize the old data with Ref<SomeTV2>, that will not work, right?

It would be nice to have the ability to provide default values for fields added and then based on size of serialized data detect which fields are missing and fill them in.

You could also put another wrapper that is like Lazy<Ref<T>> where instead of filling in with default values, you could "attempt to get" the type at different versions, i.e. with different numbers of fields. Similar to "probe" types I made in protoss experiment for rkyv: https://github.com/fu5ha/protoss/blob/master/protoss_test/src/derive.rs#L169-L173

Copy link
Owner

Choose a reason for hiding this comment

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

Yes, you would need to have defaults for new fields for backward compatibility.
For forward compatibility it would be enough to mark formula as non-exhaustive, so it would just skip fields added at the end.
How about Lazy<AnyRef> that can be viewed as Lazy<AnyRef<T>>.
Where the only difference between Ref<T> and AnyRef<T> is that binary format of AnyRef doesn't depend on T

Copy link
Owner

Choose a reason for hiding this comment

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

Maybe Lazy layer is not needed.

struct AnyRef {...}

impl AnyRef {
  fn decode<F: Formula, T: Deserialize<F>>() -> Option<T> { ... }
}

This requires more work 🤣

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah exactly, I was thinking of making a single thing similar to what you propose that's like a hybrid.

Ideally it would still be typed based on the main type it represents "some version" of. Then decode would only let you ask for one of the known versions of that type

Copy link
Contributor Author

Choose a reason for hiding this comment

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

struct AnyVersionRef<T: Versioned> {...}

impl<T: Versioned> AnyVersionRef<T> {
  fn decode_version<V: VersionOf<T>>(&self) -> Option<V> { ... }
  fn latest_with_defaults(self) -> T { ... }
}

Needs more thinking about how exactly to integrate the deserialize and formula types/bounds but thats the basic vision I guess.


for message in packet.game_messages.iter::<GameMessageRead>() {
match message.unwrap() {
GameMessageRead::Client(ClientMessageRead::ClientData { nickname, clan }) => {
drop(nickname);
drop(clan);
let _ = nickname;
Copy link
Owner

Choose a reason for hiding this comment

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

Any reason for this besides style?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Clippy doesn't like dropping references and says to ignore it via _ instead. But yeah it's ultimately just style.

Copy link
Owner

Choose a reason for hiding this comment

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

If default-configured clippy says so I have no objections.

pub enum GameMessageRead<'de> {
Client(ClientMessageRead<'de>),
Server(ServerMessageRead<'de>),
}

#[alkahest(Formula)]
pub enum ClientMessageFormula {
ClientData { nickname: Ref<str>, clan: Ref<str> },
Copy link
Owner

Choose a reason for hiding this comment

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

Ref<str> and String are distinct but actually equivalent formulas.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right :D Which is cool

Copy link
Owner

Choose a reason for hiding this comment

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

So why making separate type? ClientMessage as Formula would be the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was not obvious to me from documentation what a formula actually is -- that it is a rust type that mirrors the type you want that contains types which have a direct exact representation. Making this split it becomes more obvious what's going on -- in my eyes at least. And it shows how you can create multiple formulas which the same type can serialize as / deserialize from, or customize the exact representation with wrappers like Ref. The point of all this change was to try what it would look like to 1. separate all the formulas 2. make tweaks like adding Ref to build my intuition on the versioned wrapper we talked about in the other thread.

Whether the proper place for that is in a test like here: perhaps not x) but I was just playing around with it to get a better idea of how things work.

Probably it should go in a separate test/example to show it better. I would be fine with reverting the formula changes in this file for sure.

@zakarumych zakarumych merged commit bc0e59c into zakarumych:main Sep 19, 2023
4 of 70 checks passed
@zakarumych
Copy link
Owner

I will merge this and add my own changes on top.
Thank you for your contribution and interest in the project :)

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.

2 participants