Skip to content

Commit c00d765

Browse files
committed
Implement Encode and RefEncode for Cell
1 parent 4f84eab commit c00d765

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

crates/objc2/src/encode/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
//! method argument, but is a very common return type, and hence implements
6060
//! [`Encode`].
6161
62-
use core::cell::UnsafeCell;
62+
use core::cell::{Cell, UnsafeCell};
6363
use core::ffi::c_void;
6464
use core::mem::{self, ManuallyDrop, MaybeUninit};
6565
use core::num::{
@@ -492,6 +492,9 @@ encode_impls_transparent! {
492492
// (e.g. an `Option<UnsafeCell<&u8>>` impl is not available).
493493
UnsafeCell<T: ?Sized>,
494494

495+
// SAFETY: Guaranteed to have the same layout as `UnsafeCell<T>`.
496+
Cell<T: ?Sized>,
497+
495498
// The inner field is not public, so may not be safe.
496499
// TODO: Pin<T>,
497500

@@ -501,9 +504,6 @@ encode_impls_transparent! {
501504
// SAFETY: Guaranteed to have the same layout and ABI as `T`.
502505
Wrapping<T>,
503506

504-
// It might have requirements that would disourage this impl?
505-
// TODO: Cell<T>
506-
507507
// TODO: Types that need to be made repr(transparent) first:
508508
// - core::cell::Ref?
509509
// - core::cell::RefCell?
@@ -705,6 +705,9 @@ mod tests {
705705
assert_eq!(<&ManuallyDrop<Option<&u8>>>::ENCODING, <&&u8>::ENCODING);
706706

707707
assert_eq!(<UnsafeCell<u8>>::ENCODING, u8::ENCODING);
708+
assert_eq!(<UnsafeCell<&u8>>::ENCODING, <&u8>::ENCODING);
709+
assert_eq!(<Cell<u8>>::ENCODING, u8::ENCODING);
710+
assert_eq!(<Cell<&u8>>::ENCODING, <&u8>::ENCODING);
708711
// assert_eq!(<Pin<u8>>::ENCODING, u8::ENCODING);
709712
assert_eq!(<MaybeUninit<u8>>::ENCODING, u8::ENCODING);
710713
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;
@@ -28,4 +28,7 @@ fn main() {
2828
is_encode::<UnsafeCell<&u8>>();
2929
// But this mustn't
3030
is_encode::<Option<UnsafeCell<&u8>>>();
31+
32+
// Same
33+
is_encode::<Option<Cell<&u8>>>();
3134
}

crates/test-ui/ui/not_encode.stderr

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,26 @@ note: required by a bound in `is_encode`
250250
|
251251
| fn is_encode<T: Encode>() {}
252252
| ^^^^^^ required by this bound in `is_encode`
253+
254+
error[E0277]: the trait bound `Cell<&u8>: OptionEncode` is not satisfied
255+
--> ui/not_encode.rs
256+
|
257+
| is_encode::<Option<Cell<&u8>>>();
258+
| ^^^^^^^^^^^^^^^^^ the trait `OptionEncode` is not implemented for `Cell<&u8>`
259+
|
260+
= help: the following other types implement trait `OptionEncode`:
261+
&'a T
262+
&'a mut T
263+
NonNull<T>
264+
NonNull<c_void>
265+
NonZeroI16
266+
NonZeroI32
267+
NonZeroI64
268+
NonZeroI8
269+
and $N others
270+
= note: required for `Option<Cell<&u8>>` to implement `Encode`
271+
note: required by a bound in `is_encode`
272+
--> ui/not_encode.rs
273+
|
274+
| fn is_encode<T: Encode>() {}
275+
| ^^^^^^ required by this bound in `is_encode`

0 commit comments

Comments
 (0)