WIP migration to 0.8 - #4
Conversation
| /// ```rust,ignore | ||
| /// use std::marker::PhantomData; | ||
| /// use rkyv::{ | ||
| /// Archive, Serialize, Deserialize, Infallible, vec::ArchivedVec, Archived, with::With, | ||
| /// bytecheck, Portable, Archive, Serialize, Deserialize, rancor::Infallible, vec::ArchivedVec, Archived, with::With, | ||
| /// }; | ||
| /// use rkyv_wrappers::custom_phantom::CustomPhantom; | ||
| /// #[derive(Archive, Serialize, Deserialize, Debug, PartialEq, Eq, Default)] | ||
| /// #[archive(as = "StructWithPhantom<T::Archived>", bound(archive = " | ||
| /// use rkyv::with::ArchiveWith; | ||
| /// #[repr(C)] | ||
| /// #[derive(Portable, Archive, Serialize, Deserialize, bytecheck::CheckBytes, Debug, PartialEq, Eq, Default)] | ||
| /// #[rkyv(as = StructWithPhantom<T::Archived>, archive_bounds( | ||
| /// T: Archive, | ||
| /// With<PhantomData<T>, CustomPhantom<Archived<T>>>: Archive<Archived = PhantomData<Archived<T>>> | ||
| /// "))] | ||
| /// ), deserialize_bounds( | ||
| /// T: Archive, | ||
| /// ))] | ||
| /// struct StructWithPhantom<T> { | ||
| /// pub num: i32, | ||
| /// #[with(CustomPhantom<T::Archived>)] | ||
| /// //pub num: i32, | ||
| /// #[rkyv(with = CustomPhantom<T::Archived>)] | ||
| /// pub phantom: PhantomData<T>, | ||
| /// } | ||
| /// let value = StructWithPhantom::<Vec<i32>>::default(); | ||
| /// let bytes = rkyv::to_bytes::<_, 1024>(&value).unwrap(); | ||
| /// let archived: &StructWithPhantom<ArchivedVec<i32>> = unsafe { rkyv::archived_root::<StructWithPhantom<Vec<i32>>>(&bytes) }; | ||
| /// | ||
| /// let deserialized: StructWithPhantom<Vec<i32>> = archived.deserialize(&mut Infallible).unwrap(); | ||
| /// assert_eq!(deserialized, value); | ||
| /// let value = StructWithPhantom::<Vec<rkyv::rend::i32_le>>::default(); | ||
| /// let bytes = rkyv::to_bytes::<rkyv::rancor::Error>(&value).unwrap(); | ||
| /// let archived = rkyv::access::<StructWithPhantom<ArchivedVec<rkyv::rend::i32_le>>, rkyv::rancor::Error>(&bytes) | ||
| /// .unwrap(); | ||
| /// | ||
| /// // let deserialized: StructWithPhantom<Vec<rkyv::rend::i32_le>> = archived.deserialize().unwrap(); | ||
| /// assert_eq!(archived, &value); | ||
| /// ``` |
There was a problem hiding this comment.
This snippet doesn't compile, and errors are not too easy to read.
There was a problem hiding this comment.
resolved it by adding:
#[rkyv(archive_bounds(CustomPhantom<T>: ArchiveWith<PhantomData<T>, Archived = PhantomData<T>>))], to help rustc, suggested by @djkoloski .
I also removed as = StructWithPhantom<T::Archived>, because it requires to implement PartialEq and Debug manually. An example of a working as = would be great though.
| ser::{serializers::AllocSerializer, Serializer}, | ||
| Deserialize, Infallible, | ||
| }; | ||
| // pub mod as_hashmap { |
There was a problem hiding this comment.
There's only tests for hashmap, so I commented them as I commented hashmap too ; let's focus on phantomdata first.
| /// use rkyv_wrappers::custom_phantom::CustomPhantom; | ||
| /// #[derive(Archive, Serialize, Deserialize, Debug, PartialEq, Eq, Default)] | ||
| /// #[archive(as = "StructWithPhantom<T::Archived>", bound(archive = " | ||
| /// T: Archive, |
There was a problem hiding this comment.
I figured T doesn't really need to be Archive, as CustomPhantom is Archive ; it was there only for as = "StructWithPhantom<T::Archived>", which is not relevant because T is within CustomPhantom, which has no requirement on T being archived.
- I removed the
as =because it would require manual implementation ofrekyv(derive(Debug), compare(PartialEq)). - We could also make this example using
asif i32 were generic overaDifferentT: Archive, but that's controversial and I encountered errors.
I'd still like to see such examples (especially helpful in context of my nalgebra migration).
This is actually me trying to learn rkyv 0.8 ; it's not working yet.
Current status
hash_mapis commented, let's focus onCustomPhantomfirst.Fixing example code for
CustomPhantomwas not trivial, because:#[rkyv(archive_bounds(CustomPhantom<T>: ArchiveWith<PhantomData<T>, Archived = PhantomData<T>>))]to make help rustc understand.as = StructWithPhantom<T::Archived>, because it requires Self to be portable, and thati32is not.as, but that might make the code more complex, maybe in another PR after discussion/validation?Resolved
I keep these in the desription, even though it's been resolved.
Failing test when still using
as = ...:Details