Skip to content

Commit ce6a1ef

Browse files
committed
Merge pull request SSheldon#9 from madsmtm/encode-unsized
Implement `Encode` for unsized types as well
2 parents 3d4596a + 39153ad commit ce6a1ef

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

objc_encode/src/encode.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -79,35 +79,38 @@ slice_encode_impl!(
7979
27, 28, 29, 30, 31, 32
8080
);
8181

82-
/*
83-
External crates cannot implement Encode for pointers or Optionals, but they
84-
*can* implement it for references. rust-lang/rust#25126
85-
86-
As a workaround, we provide implementations for these types that return the
87-
same encoding as references.
88-
*/
89-
unsafe impl<T> Encode for *const T
82+
// External crates cannot implement Encode for pointers or [`Option`], but
83+
// they *can* implement it for references. See
84+
// [rust-lang/rust#25126](https://github.com/rust-lang/rust/issues/25126)
85+
//
86+
// So, as a workaround, we provide implementations for these types that return
87+
// the same encoding as references.
88+
//
89+
// Using `?Sized` is safe here because we delegate to other implementations
90+
// (which will verify that the implementation is safe for the unsized type).
91+
92+
unsafe impl<T: ?Sized> Encode for *const T
9093
where
9194
for<'b> &'b T: Encode,
9295
{
9396
const ENCODING: Encoding<'static> = <&T>::ENCODING;
9497
}
9598

96-
unsafe impl<T> Encode for *mut T
99+
unsafe impl<T: ?Sized> Encode for *mut T
97100
where
98101
for<'b> &'b mut T: Encode,
99102
{
100103
const ENCODING: Encoding<'static> = <&mut T>::ENCODING;
101104
}
102105

103-
unsafe impl<'a, T> Encode for Option<&'a T>
106+
unsafe impl<'a, T: ?Sized> Encode for Option<&'a T>
104107
where
105108
for<'b> &'b T: Encode,
106109
{
107110
const ENCODING: Encoding<'static> = <&T>::ENCODING;
108111
}
109112

110-
unsafe impl<'a, T> Encode for Option<&'a mut T>
113+
unsafe impl<'a, T: ?Sized> Encode for Option<&'a mut T>
111114
where
112115
for<'b> &'b mut T: Encode,
113116
{

0 commit comments

Comments
 (0)