Skip to content

Conversation

@ThierryBerger
Copy link

@ThierryBerger ThierryBerger commented Apr 9, 2025

This is actually me trying to learn rkyv 0.8 ; it's not working yet.

Current status

  • hash_map is commented, let's focus on CustomPhantom first.

Fixing example code for CustomPhantom was not trivial, because:

  • We have to use this non obvious #[rkyv(archive_bounds(CustomPhantom<T>: ArchiveWith<PhantomData<T>, Archived = PhantomData<T>>))] to make help rustc understand.
  • I had to remove as = StructWithPhantom<T::Archived>, because it requires Self to be portable, and that i32 is not.
    • This is a bit unfortunate, I'd like to have generic field to be able to use 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

   Doc-tests rkyv_wrappers

running 1 test
test rkyv_wrappers/src/custom_phantom.rs - custom_phantom::CustomPhantom (line 14) ... FAILED

failures:

---- rkyv_wrappers/src/custom_phantom.rs - custom_phantom::CustomPhantom (line 14) stdout ----
error[E0308]: mismatched types
   --> rkyv_wrappers/src/custom_phantom.rs:23:20
    |
12  | #[derive(Portable, Archive, Serialize, Deserialize, bytecheck::CheckBytes, Debug, PartialEq, Eq, Default)]
    |                    ^^^^^^^
    |                    |
    |                    expected `Place<<... as ArchiveWith<...>>::Archived>`, found `Place<PhantomData<T>>`
    |                    arguments to this function are incorrect
    |
    = note: expected struct `Place<<CustomPhantom<T> as ArchiveWith<PhantomData<T>>>::Archived>`
               found struct `Place<PhantomData<T>>`
    = help: consider constraining the associated type `<CustomPhantom<T> as ArchiveWith<PhantomData<T>>>::Archived` to `PhantomData<T>` or calling a method that returns `<CustomPhantom<T> as ArchiveWith<PhantomData<T>>>::Archived`
    = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
note: associated function defined here
   --> /home/tb/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rkyv-0.8.10/src/with.rs:114:8
    |
114 |     fn resolve_with(
    |        ^^^^^^^^^^^^
    = note: this error originates in the derive macro `Archive` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
   --> rkyv_wrappers/src/custom_phantom.rs:23:40
    |
12  | #[derive(Portable, Archive, Serialize, Deserialize, bytecheck::CheckBytes, Debug, PartialEq, Eq, Default)]
    |                                        ^^^^^^^^^^^
    |                                        |
    |                                        expected `&<CustomPhantom<T> as ArchiveWith<...>>::Archived`, found `&PhantomData<T>`
    |                                        arguments to this function are incorrect
    |
    = note: expected reference `&<CustomPhantom<T> as ArchiveWith<PhantomData<T>>>::Archived`
               found reference `&PhantomData<T>`
    = help: consider constraining the associated type `<CustomPhantom<T> as ArchiveWith<PhantomData<T>>>::Archived` to `PhantomData<T>` or calling a method that returns `<CustomPhantom<T> as ArchiveWith<PhantomData<T>>>::Archived`
    = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
note: associated function defined here
   --> /home/tb/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rkyv-0.8.10/src/with.rs:139:8
    |
139 |     fn deserialize_with(field: &F, deserializer: &mut D)
    |        ^^^^^^^^^^^^^^^^
    = note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
Couldn't compile the test.

failures:
    rkyv_wrappers/src/custom_phantom.rs - custom_phantom::CustomPhantom (line 14)

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.04s

Comment on lines 14 to 41
/// ```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);
/// ```
Copy link
Author

Choose a reason for hiding this comment

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

This snippet doesn't compile, and errors are not too easy to read.

Copy link
Author

@ThierryBerger ThierryBerger Apr 17, 2025

Choose a reason for hiding this comment

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

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 {
Copy link
Author

Choose a reason for hiding this comment

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

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,
Copy link
Author

@ThierryBerger ThierryBerger Apr 17, 2025

Choose a reason for hiding this comment

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

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 of rekyv(derive(Debug), compare(PartialEq)).
  • We could also make this example using as if i32 were generic over aDifferentT: 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).

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.

1 participant