-
Notifications
You must be signed in to change notification settings - Fork 3
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
Inconvenient apis for append_repeated_*
#16
Comments
append_*
append_repeated_*
Thanks for bringing this up. I addressed the first two in #17 but am not really happy with the diff. It requires a lot of additional type annotations as inference does not work for:
This issue might be mostly in test code since literals are more common there. Any thoughts on that PR? |
This is a very good point. I doubt this is possible, no matter safe or unsafe Rust. A |
Well, technically it is possible, if you leak all string slices back to the vector of strings, but this is simply a bad solution
What do you mean by reusable scratch area? |
But a vector of Strings is a list of { ptr, capacity, len } structs (probablly 3x
You can have a |
Right, they are different sizes, that's why I say it's bad(along with the fact that leaks are bad), just for example:
Yeah, that could be helpful in some cases I will remember this workaround, thanks :) |
#18 has a solution for appending repeated message. It keeps &Anybuf elements possible. This way a single message can be added multiple times without cloning. |
Ah nice, now I see what you mean: Reuse the raw memory of the vector because you don't need it anymore and know the slice of references is shorter. Interesting idea for sure but nothing you want to show an auditor :D |
Nice! Can't wait to use it 🚀
Right, didn't think about having same message in repeating message. Thanks for covering it as well :) |
Could you try latest main in your project? If everything is fine, I can make a breaking 0.4.0 release with the two changes discussed. |
Yes, thanks for the change! It was compatible with previous version in my case, and was able to remove every .append_string(2, &self.receiver)
.append_string(3, &self.token_a)
.append_string(4, &self.token_b)
- .append_repeated_string(
- 5,
- self.amounts_a
- .iter()
- .map(String::as_str)
- .collect::<Vec<_>>()
- .as_ref(),
- )
- .append_repeated_string(
- 6,
- self.amounts_b
- .iter()
- .map(String::as_str)
- .collect::<Vec<_>>()
- .as_ref(),
- )
+ .append_repeated_string(5, &self.amounts_a)
+ .append_repeated_string(6, &self.amounts_b)
.append_repeated_int64(7, &self.tick_indexes_a_to_b)
.append_repeated_uint64(8, &self.fees)
- .append_repeated_message(9, options_messages.iter().collect::<Vec<_>>().as_ref())
+ .append_repeated_message(9, &options_messages)
.into_vec()
- .collect::<Vec<_>>()
- .as_ref(),
- )
+ .append_repeated_string(5, &self.amounts_a)
+ .append_repeated_string(6, &self.amounts_b)
.append_repeated_int64(7, &self.tick_indexes_a_to_b)
.append_repeated_uint64(8, &self.fees)
- .append_repeated_message(9, options_messages.iter().collect::<Vec<_>>().as_ref())
+ .append_repeated_message(9, &options_messages) |
Another thing to discuss, if it will be better to take let coins_anybuf: Vec<Anybuf> = self
.register_fee
.iter()
.map(Coin::to_anybuf)
.collect::<Vec<_>>();
Anybuf::new()
.append_uint64(1, self.msg_submit_tx_max_messages)
.append_repeated_message(2, &coins_anybuf) With this change we won't need to collect it: let coins_anybuf = self
.register_fee
.iter()
.map(Coin::to_anybuf);
Anybuf::new()
.append_uint64(1, self.msg_submit_tx_max_messages)
.append_repeated_message(2, coins_anybuf) Not sure about drawbacks, except of bigger binaries when used carelessly |
Okay, I see. Not bad. But let's break this down into separate steps. I'll release a version with the changes we discussed already. For everything else, let's make a new ticket. |
Released as part of 0.4.0 |
Hey, I really like using anybuf, but there are a couple of inconvenient methods
Append repeated strings
Currently
append_repeated_string
accepts&[&str]
but IMO it could be improved to take&[impl AsRef<str>]
instead. The reason why it's inconvenient, because in the cases where you haveVec<String>
right now it's unavoidable in safe rust to not allocate a new vectorVec<&str>
and take slice of that:Append repeated bytes
Since
append_bytes
already acceptsimpl AsRef<[u8]>
, I would assume repeated should take&[impl AsRef<[u8]>]
as wellAppend repeated message
Currently
append_repeated_message
accepts&[&Anybuf]
, but similar to the methods above it's tricky to get this type:For this one I would suggest to take simply
&[Anybuf]
.The text was updated successfully, but these errors were encountered: