Skip to content

Commit

Permalink
Implement Encode and RefEncode for Cell
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Feb 1, 2023
1 parent 8dcd141 commit 630032a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
11 changes: 7 additions & 4 deletions crates/objc2/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
//! method argument, but is a very common return type, and hence implements
//! [`Encode`].
use core::cell::UnsafeCell;
use core::cell::{Cell, UnsafeCell};
use core::ffi::c_void;
use core::mem::{self, ManuallyDrop, MaybeUninit};
use core::num::{
Expand Down Expand Up @@ -492,6 +492,9 @@ encode_impls_transparent! {
// (e.g. an `Option<UnsafeCell<&u8>>` impl is not available).
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 @@ -501,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 @@ -705,6 +705,9 @@ mod tests {
assert_eq!(<&ManuallyDrop<Option<&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);
Expand Down
5 changes: 4 additions & 1 deletion crates/test-ui/ui/not_encode.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Verify that certain things we don't want to be encode aren't.
use core::cell::UnsafeCell;
use core::cell::{Cell, UnsafeCell};
use core::ffi::c_void;

use objc2::encode::Encode;
Expand Down Expand Up @@ -28,4 +28,7 @@ fn main() {
is_encode::<UnsafeCell<&u8>>();
// But this mustn't
is_encode::<Option<UnsafeCell<&u8>>>();

// Same
is_encode::<Option<Cell<&u8>>>();
}
23 changes: 23 additions & 0 deletions crates/test-ui/ui/not_encode.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,26 @@ note: required by a bound in `is_encode`
|
| 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`

0 comments on commit 630032a

Please sign in to comment.