Skip to content

Commit 1359042

Browse files
committed
Implement Encode for pointers when references do
1 parent db2e748 commit 1359042

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/encode.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,34 @@ unsafe impl Encode for *const c_void {
7373

7474
fn encode() -> Self::Encoding { Pointer::new(Primitive::Void) }
7575
}
76+
77+
/*
78+
External crates cannot implement Encode for pointers or Optionals, but they
79+
*can* implement it for references. rust-lang/rust#25126
80+
81+
As a workaround, we provide implementations for these types that return the
82+
same encoding as references.
83+
*/
84+
unsafe impl<T: 'static> Encode for *const T where for<'a> &'a T: Encode {
85+
type Encoding = <&'static T as Encode>::Encoding;
86+
87+
fn encode() -> Self::Encoding { <&T>::encode() }
88+
}
89+
90+
unsafe impl<T: 'static> Encode for *mut T where for<'a> &'a mut T: Encode {
91+
type Encoding = <&'static mut T as Encode>::Encoding;
92+
93+
fn encode() -> Self::Encoding { <&mut T>::encode() }
94+
}
95+
96+
unsafe impl<'a, T> Encode for Option<&'a T> where &'a T: Encode {
97+
type Encoding = <&'a T as Encode>::Encoding;
98+
99+
fn encode() -> Self::Encoding { <&T>::encode() }
100+
}
101+
102+
unsafe impl<'a, T> Encode for Option<&'a mut T> where &'a mut T: Encode {
103+
type Encoding = <&'a mut T as Encode>::Encoding;
104+
105+
fn encode() -> Self::Encoding { <&mut T>::encode() }
106+
}

0 commit comments

Comments
 (0)