-
Notifications
You must be signed in to change notification settings - Fork 34
[Based on #26] Support reference types (and a pinch of DSTs) #28
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
base: master
Are you sure you want to change the base?
Conversation
By its very nature, Abomonation does very unsafe (and UB-ish) things. But we should strive to explain these as well as we can in the current state of the unsafe Rust formalization effort in order to reduce the potential for known misuse.
9b4ebdc to
dababc1
Compare
dababc1 to
bb89010
Compare
|
I have pushed a draft of the |
bb89010 to
544af4c
Compare
|
Alright, I think this one is now ready to go (though by virtue of being on the top of the PR stack, it will probably go in last). |
| assert!(result == &record); | ||
| assert!(rest.len() == 0); | ||
| if bytes.pop().is_some() { | ||
| assert_eq!(unsafe { decode::<T>(&mut bytes[..]) }, None); |
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.
One-line fix for zero sized type support. Not bad!
tests/tests.rs
Outdated
| let mut bytes = Vec::new(); | ||
| fn _test_pass<'bytes, T: Abomonation<'bytes> + Debug + Eq>( | ||
| mut bytes: &'bytes mut Vec<u8>, record: T | ||
| ) { |
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.
These changes work around what I believe to be a limitation of Rust's current API vocabulary with respect to lifetimes.
I could not find a way to express "we want T to implement Abomonation<'bytes> for all possibly 'bytes lifetimes on this function's stack". We only have "it should work for all slices of bytes" (which excludes types containing references) and "it should work for whatever byte container was passed as a parameter to the function, which will end up borrowed forever" (what I ended up using).
Amusingly enough, this is closely related, but not quite identical, to the reason why a full port of Abomonated to the new API is not possible.
Some variant of the ugly trick that I used to make Abomonated work could probably also work here, but I don't think that this shady unsafe trick is warranted in a test harness, of all things.
If you have an idea of how to extract this dirty trick into a general-purpose utility that any client code can easily and safely use, then I may revisit this opinion.
544af4c to
660f7bd
Compare
660f7bd to
af3e52d
Compare
af3e52d to
26e309d
Compare
9bd8671 to
22756de
Compare
22756de to
8ba2a08
Compare
This PR implements
Abomonationfor most references types, and a few DSTs along the way. It is what I had in mind while opening #27 . The main advantage of implementingAbomonationfor these types is that it becomes much easier to serialize types containing Rust references (which are, after all, almost like every other pointer).This PR raises a few questions that we may want to discuss here:
AbomonationDST impls of this PR are only here 1/as a fun experiment and 2/because they are a more convenient place for deduplicating impls than free functions)Abomonation, then we'll also need to makeencode/decode/measureand tests acceptT: ?Sized, which from a quick attempt won't be so easy. We may also want?Sizedbounds on some genericAbomonationimpls like that ofBox<T>Abomonationfordyn Traitand&[mut] dyn TraitwhereTrait: Abomonation?For convenience reasons, this PR is based on #22, #24, #25 and #26. Reviewing these PRs first is recommended, but if you want to review this one in isolation, please consider doing so commit-by-commit in order to get a clean diff.
Fixes #27 .