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 Jan 27, 2023
1 parent fb93988 commit 5cf62fe
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
11 changes: 7 additions & 4 deletions crates/objc2-encode/src/encode.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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 @@ -459,6 +459,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 @@ -468,9 +471,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 @@ -798,6 +798,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 All @@ -17,4 +17,7 @@ fn main() {
is_encode::<UnsafeCell<&u8>>();
// But this shouldn't be
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 @@ -94,3 +94,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 5cf62fe

Please sign in to comment.