Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Encode and RefEncode for UnsafeCell and Cell #351

Merged
merged 2 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions crates/objc2/src/encode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
//! method argument, but is a very common return type, and hence implements
//! [`Encode`].

use core::cell::{Cell, UnsafeCell};
use core::ffi::c_void;
use core::mem::{self, ManuallyDrop, MaybeUninit};
use core::num::{
Expand Down Expand Up @@ -484,11 +485,15 @@ encode_impls_transparent! {
// TODO: With specialization: `impl Encode for ManuallyDrop<Box<T>>`
ManuallyDrop<T: ?Sized>,

// SAFETY: Guaranteed to have the same in-memory representation `T`.
//
// The fact that this has `repr(no_niche)` has no effect on us, since we
// don't unconditionally implement `Encode` generically over `Option`.
// (e.g. an `Option<UnsafeCell<&u8>>` impl is not available).
// The inner field is not public, so may not be stable.
// TODO: UnsafeCell<T>,
UnsafeCell<T: ?Sized>,

// SAFETY: Guaranteed to have the same layout as `UnsafeCell<T>`.
Cell<T: ?Sized>,

// The inner field is not public, so may not be safe.
// TODO: Pin<T>,
Expand All @@ -499,9 +504,6 @@ encode_impls_transparent! {
// SAFETY: Guaranteed to have the same layout and ABI as `T`.
Wrapping<T>,

// It might have requirements that would disourage this impl?
// TODO: Cell<T>

// TODO: Types that need to be made repr(transparent) first:
// - core::cell::Ref?
// - core::cell::RefCell?
Expand Down Expand Up @@ -702,13 +704,13 @@ mod tests {
assert_eq!(<ManuallyDrop<Option<&u8>>>::ENCODING, u8::ENCODING_REF);
assert_eq!(<&ManuallyDrop<Option<&u8>>>::ENCODING, <&&u8>::ENCODING);

// assert_eq!(<UnsafeCell<u8>>::ENCODING, u8::ENCODING);
assert_eq!(<UnsafeCell<u8>>::ENCODING, u8::ENCODING);
assert_eq!(<UnsafeCell<&u8>>::ENCODING, <&u8>::ENCODING);
assert_eq!(<Cell<u8>>::ENCODING, u8::ENCODING);
assert_eq!(<Cell<&u8>>::ENCODING, <&u8>::ENCODING);
// assert_eq!(<Pin<u8>>::ENCODING, u8::ENCODING);
assert_eq!(<MaybeUninit<u8>>::ENCODING, u8::ENCODING);
assert_eq!(<Wrapping<u8>>::ENCODING, u8::ENCODING);

// Shouldn't compile
// assert_eq!(<Option<UnsafeCell<&u8>>>::ENCODING, <&u8>::ENCODING);
}

#[test]
Expand Down
9 changes: 9 additions & 0 deletions crates/test-ui/ui/not_encode.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Verify that certain things we don't want to be encode aren't.
use core::cell::{Cell, UnsafeCell};
use core::ffi::c_void;

use objc2::encode::Encode;
Expand All @@ -22,4 +23,12 @@ fn main() {
is_encode::<fn(i32, ())>();

is_encode::<&Sel>();

// This should compile
is_encode::<UnsafeCell<&u8>>();
// But this mustn't
is_encode::<Option<UnsafeCell<&u8>>>();

// Same
is_encode::<Option<Cell<&u8>>>();
}
50 changes: 48 additions & 2 deletions crates/test-ui/ui/not_encode.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,52 @@ note: required by a bound in `is_encode`
| ^^^^^^ required by this bound in `is_encode`
help: consider removing the leading `&`-reference
|
24 - is_encode::<&Sel>();
24 + is_encode::<Sel>();
25 - is_encode::<&Sel>();
25 + is_encode::<Sel>();
|

error[E0277]: the trait bound `UnsafeCell<&u8>: OptionEncode` is not satisfied
--> ui/not_encode.rs
|
| is_encode::<Option<UnsafeCell<&u8>>>();
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `OptionEncode` is not implemented for `UnsafeCell<&u8>`
|
= help: the following other types implement trait `OptionEncode`:
&'a T
&'a mut T
NonNull<T>
NonNull<c_void>
NonZeroI16
NonZeroI32
NonZeroI64
NonZeroI8
and $N others
= note: required for `Option<UnsafeCell<&u8>>` to implement `Encode`
note: required by a bound in `is_encode`
--> ui/not_encode.rs
|
| fn is_encode<T: Encode>() {}
| ^^^^^^ required by this bound in `is_encode`

error[E0277]: the trait bound `Cell<&u8>: OptionEncode` is not satisfied
--> ui/not_encode.rs
|
| is_encode::<Option<Cell<&u8>>>();
| ^^^^^^^^^^^^^^^^^ the trait `OptionEncode` is not implemented for `Cell<&u8>`
|
= help: the following other types implement trait `OptionEncode`:
&'a T
&'a mut T
NonNull<T>
NonNull<c_void>
NonZeroI16
NonZeroI32
NonZeroI64
NonZeroI8
and $N others
= note: required for `Option<Cell<&u8>>` to implement `Encode`
note: required by a bound in `is_encode`
--> ui/not_encode.rs
|
| fn is_encode<T: Encode>() {}
| ^^^^^^ required by this bound in `is_encode`