Skip to content

Commit 5cf62fe

Browse files
committed
Implement Encode and RefEncode for Cell
1 parent fb93988 commit 5cf62fe

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

crates/objc2-encode/src/encode.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::cell::UnsafeCell;
1+
use core::cell::{Cell, UnsafeCell};
22
use core::ffi::c_void;
33
use core::mem::{self, ManuallyDrop, MaybeUninit};
44
use core::num::{
@@ -459,6 +459,9 @@ encode_impls_transparent! {
459459
// (e.g. an `Option<UnsafeCell<&u8>>` impl is not available).
460460
UnsafeCell<T: ?Sized>,
461461

462+
// SAFETY: Guaranteed to have the same layout as `UnsafeCell<T>`.
463+
Cell<T: ?Sized>,
464+
462465
// The inner field is not public, so may not be safe.
463466
// TODO: Pin<T>,
464467

@@ -468,9 +471,6 @@ encode_impls_transparent! {
468471
// SAFETY: Guaranteed to have the same layout and ABI as `T`.
469472
Wrapping<T>,
470473

471-
// It might have requirements that would disourage this impl?
472-
// TODO: Cell<T>
473-
474474
// TODO: Types that need to be made repr(transparent) first:
475475
// - core::cell::Ref?
476476
// - core::cell::RefCell?
@@ -798,6 +798,9 @@ mod tests {
798798
assert_eq!(<&ManuallyDrop<Option<&u8>>>::ENCODING, <&&u8>::ENCODING);
799799

800800
assert_eq!(<UnsafeCell<u8>>::ENCODING, u8::ENCODING);
801+
assert_eq!(<UnsafeCell<&u8>>::ENCODING, <&u8>::ENCODING);
802+
assert_eq!(<Cell<u8>>::ENCODING, u8::ENCODING);
803+
assert_eq!(<Cell<&u8>>::ENCODING, <&u8>::ENCODING);
801804
// assert_eq!(<Pin<u8>>::ENCODING, u8::ENCODING);
802805
assert_eq!(<MaybeUninit<u8>>::ENCODING, u8::ENCODING);
803806
assert_eq!(<Wrapping<u8>>::ENCODING, u8::ENCODING);

crates/test-ui/ui/not_encode.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Verify that certain things we don't want to be encode aren't.
2-
use core::cell::UnsafeCell;
2+
use core::cell::{Cell, UnsafeCell};
33
use core::ffi::c_void;
44

55
use objc2::encode::Encode;
@@ -17,4 +17,7 @@ fn main() {
1717
is_encode::<UnsafeCell<&u8>>();
1818
// But this shouldn't be
1919
is_encode::<Option<UnsafeCell<&u8>>>();
20+
21+
// Same
22+
is_encode::<Option<Cell<&u8>>>();
2023
}

crates/test-ui/ui/not_encode.stderr

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,26 @@ note: required by a bound in `is_encode`
9494
|
9595
| fn is_encode<T: Encode>() {}
9696
| ^^^^^^ required by this bound in `is_encode`
97+
98+
error[E0277]: the trait bound `Cell<&u8>: OptionEncode` is not satisfied
99+
--> ui/not_encode.rs
100+
|
101+
| is_encode::<Option<Cell<&u8>>>();
102+
| ^^^^^^^^^^^^^^^^^ the trait `OptionEncode` is not implemented for `Cell<&u8>`
103+
|
104+
= help: the following other types implement trait `OptionEncode`:
105+
&'a T
106+
&'a mut T
107+
NonNull<T>
108+
NonNull<c_void>
109+
NonZeroI16
110+
NonZeroI32
111+
NonZeroI64
112+
NonZeroI8
113+
and $N others
114+
= note: required for `Option<Cell<&u8>>` to implement `Encode`
115+
note: required by a bound in `is_encode`
116+
--> ui/not_encode.rs
117+
|
118+
| fn is_encode<T: Encode>() {}
119+
| ^^^^^^ required by this bound in `is_encode`

0 commit comments

Comments
 (0)