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

Support Serialize + Deserialize for dashu-ratio #20

Closed
wants to merge 5 commits into from
Closed

Support Serialize + Deserialize for dashu-ratio #20

wants to merge 5 commits into from

Conversation

oovm
Copy link
Contributor

@oovm oovm commented Nov 24, 2022

  • string form
"-5/11"
  • object form
{
    "sign": true,
    "numerator": [5],
    "denominator": [11]
}

serializer.serialize_str(&self.to_string())
} else {
let sign = self.sign();
let numerator = self.numerator().clone().into_parts().1.to_le_bytes();
Copy link
Contributor Author

@oovm oovm Nov 24, 2022

Choose a reason for hiding this comment

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

There is one more clone here, because the slice cannot be obtained directly

@oovm
Copy link
Contributor Author

oovm commented Nov 24, 2022

Well, sorry, seems not work with no-std

@cmpute
Copy link
Owner

cmpute commented Nov 24, 2022

Thanks for your contribution, but the implementation seems over-complicate for me. Do you have any specific reason to serialize the rational number as a struct with three parts? I was planning to serialize the rational number just as a pair of integers.

Besides, you can take a look at #11, where I listed all thirdparty crates I planned to support, so you don't have to implement them yourselves.

@oovm
Copy link
Contributor Author

oovm commented Nov 25, 2022

How about dividing into the following three binary representations?

  1. [ ] => 0 / 1
  2. [i64, bytes<numerator>] => numerator / 1
  3. [i64, bytes<numerator>, u64, bytes<denominator>] => numerator / denominator

@oovm
Copy link
Contributor Author

oovm commented Nov 25, 2022

Maybe VLQ is better than i64 and u64, since it also supports non-64-bit platforms.

It is best to introduce the byteorder library, byteorder defines a set of convenient binary reading interfaces.

@cmpute
Copy link
Owner

cmpute commented Nov 25, 2022

I don't think we need to handle the VLQ implementation ourselves, it could be handled by serializers like the postcard. I already have serde support for UBig and IBig, we should reuse the implementation of that. (Although I am planning to refactor it to use byte array instead of the word array which is currently used).

What I have planned for the (binary) serialization formats is:

  • UBig serialized as a byte array (with given length)
  • IBig serialized as a byte array with the sign stored in the last byte
  • RBig serialized as a (named) tuple of (UBig, IBig)
  • FBig serialized as a (named) tuple of (IBig, isize, usize)

For text serialization, I would just use the format result of each type.

@cmpute
Copy link
Owner

cmpute commented Nov 26, 2022

serde support for dashu-float and dashu-ratio has been added in d184f7c, the improvements on serializing UBig and IBig will be added after releasing the next minor version, because that is a break change.

Thanks for your help but I will close this issue for now.

@cmpute cmpute closed this Nov 26, 2022
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