Skip to content

Add Rkyv support - #110

Merged
danlehmann merged 4 commits into
danlehmann:mainfrom
IzawGithub:rkyv
Aug 4, 2026
Merged

Add Rkyv support#110
danlehmann merged 4 commits into
danlehmann:mainfrom
IzawGithub:rkyv

Conversation

@IzawGithub

Copy link
Copy Markdown
Contributor

In the same vein as Serde, add Rkyv support with an optional feature.

An example (and also my) use case is for writing savestates for an emulator. Rkyv is really good at making small ser file, and combining it with arbitrary-int for type system invariant fits well.

The code seems weirdly simple too but the tests cases works fine.

@danlehmann

Copy link
Copy Markdown
Owner

Thanks, this looks great. Could you please setup automated tests (see e.g. #111 which does this). Happy to take it then

IzawGithub added a commit to IzawGithub/arbitrary-int that referenced this pull request Jul 28, 2026
@IzawGithub

Copy link
Copy Markdown
Contributor Author

Added the CI job and synced to main, not sure if you wanted to squash or not so I rebased -> forced the changes

Comment thread src/signed.rs
@danlehmann

Copy link
Copy Markdown
Owner

I did a merge from main so that tests run as they were broken on main (they're fixed now) - feel free to overwrite that with a git push -f yourself again

@IzawGithub

Copy link
Copy Markdown
Contributor Author

Ok so not completely fixed, because rkyv::access can still return ok when it's not, I think because it takes bytes as being independent of each other instead of being the same value? It's a bit weird and I'm just a basic rkyv user so idk about internal.

I've asked on the Rkyv discord, I'll keep you updated when I get an answer

@IzawGithub

Copy link
Copy Markdown
Contributor Author

Worked on this again:

I don't think it's actually possible to remove the wrapper archival type, because Rkyv internally use rend to deal with endianness to store it's serialization format.
It wraps every type integer type that doesn't have trivial endianness (like u8 or i8), which means that what I did on 3fe243c doesn't actually work and you get stuck with the internal rend type not implementing arbitrary_int::Integer when trying to call any rkyv function with anything that isn't trivial.

So I'm back with where I started with the Archived... types, I've fixed tests and added a new one to make sure that rend types works.
I think this solution works ok?

I'm really not a fan of having the unsafe pointer cast to convert from rend -> arbitrary_int.
I've ran miri on both x86_64-unknown-linux-gnu and s390x-unknown-linux-gnu to make sure that it work on both little and big endian and it doesn't report anything.
A PR on rend to add a trait to convert from their types to native int would solve this unsafe.

Not sure if I should also add a section with type_alias!(ArchivedUInt(u8), (ArchivedU1, 1), ...).
It would make it easier to use rkyv::access but that would be an additional 246 type aliases polluting the namespace for something that would almost never be used.

Feel free to squash if you're ok with this, or I can rebase clean if you want

The Verify impls cast the archived value (e.g. rend's u16_le) to the
native UInt/Int and range-checked the byte-swapped value on
mismatched-endian hosts: valid archives were rejected and crafted
out-of-range archives could pass verification. rend already provides
From impls to native, so convert via T: From<T::Archived> instead of
the unsafe cast, and add a multi-byte round-trip test.
@danlehmann

Copy link
Copy Markdown
Owner

@IzawGithub please take a look at my suggestion - this gets rid of the unsafe and fixes big-endian

@IzawGithub

IzawGithub commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

I've got no idea how you managed to get that From bound working, I checked the rend doc and didn't see any impl to convert to native? But that's way better than the unsafe block I had.

Want me to clean and put you as co-author on base 209a642?

@danlehmann

Copy link
Copy Markdown
Owner

When merging we'll squash, so I am happy with this PR from my end. Are there more changes you want to do make?

@IzawGithub

Copy link
Copy Markdown
Contributor Author

Nope, it looks fine so you can merge iyw

@danlehmann
danlehmann merged commit 518b656 into danlehmann:main Aug 4, 2026
17 checks passed
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.

3 participants