@@ -45,6 +45,7 @@ pub mod prelude {
45
45
pub use crate :: ser:: SerializeInner ;
46
46
pub use crate :: traits:: * ;
47
47
pub use crate :: utils:: * ;
48
+ pub use crate :: PhantomDeserData ;
48
49
#[ cfg( feature = "derive" ) ]
49
50
pub use epserde_derive:: Epserde ;
50
51
}
@@ -63,6 +64,37 @@ pub fn pad_align_to(value: usize, align_to: usize) -> usize {
63
64
value. wrapping_neg ( ) & ( align_to - 1 )
64
65
}
65
66
67
+ /// A type semantically equivalent to [`PhantomData`], but whose type parameter
68
+ /// is replaced with its associated deserialization type.
69
+ ///
70
+ /// In some case, you might find yourself with a deep-copy type that has a type
71
+ /// parameter `T` appearing both in a field and in a [`PhantomData`]. In this
72
+ /// case, the type will not compile, as in its associated deserialization type
73
+ /// `T` will be replaced by `T::DeserType`, but the [`PhantomData`] field will
74
+ /// still contain `T`. To fix this issue, you can use [`PhantomDeserData`]
75
+ /// instead.
76
+ ///
77
+ /// # Examples
78
+ ///
79
+ /// This code will not compile:
80
+ /// ```compile_fail
81
+ /// use epserde::prelude::*;
82
+ /// #[derive(Epserde, Debug, PartialEq, Eq, Clone, Default)]
83
+ /// struct Data<T> {
84
+ /// data: T,
85
+ /// phantom: PhantomData<T>,
86
+ /// }
87
+ /// ```
88
+ ///
89
+ /// This code, instead, will compile:
90
+ /// ```
91
+ /// use epserde::prelude::*;
92
+ /// #[derive(Epserde, Debug, PartialEq, Eq, Clone, Default)]
93
+ /// struct Data<T> {
94
+ /// data: T,
95
+ /// phantom: PhantomDeserData<T>,
96
+ /// }
97
+ /// ```
66
98
#[ derive( Debug , Default , Clone , Copy , PartialEq , Eq , Hash ) ]
67
99
pub struct PhantomDeserData < T : ?Sized > ( pub PhantomData < T > ) ;
68
100
0 commit comments