Skip to content

Commit

Permalink
Add support for the unstable arbitrary_self_types feature
Browse files Browse the repository at this point in the history
Tracking issue rust-lang/rust#44874.

Especially useful to allow `self: Allocated<Self>` in the future.
  • Loading branch information
madsmtm committed Nov 21, 2024
1 parent 40daa2b commit 602f29e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crates/objc2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ unstable-autoreleasesafe = []
# Additionally, the message sending improvements is not yet implemented.
unstable-apple-new = []

# Uses the nightly arbitrary_self_types feature to make initialization more
# ergonomic.
unstable-arbitrary-self-types = []

# Deprecated; this is the default on Apple platforms, and not applicable on other platforms.
apple = []

Expand Down
4 changes: 4 additions & 0 deletions crates/objc2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@
feature = "unstable-autoreleasesafe",
feature(negative_impls, auto_traits)
)]
#![cfg_attr(
feature = "unstable-arbitrary-self-types",
feature(arbitrary_self_types)
)]
// Note: `doc_notable_trait` doesn't really make sense for us, it's only shown
// for functions returning a specific trait.
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg, doc_cfg_hide))]
Expand Down
23 changes: 23 additions & 0 deletions crates/objc2/src/rc/allocated_partial_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ pub struct Allocated<T: ?Sized> {
}

// Explicitly don't implement `Deref`, `Message` nor `RefEncode`.
//
// We do want to implement `Receiver` though, to allow the user to type
// `self: Allocated<Self>`.
#[cfg(feature = "unstable-arbitrary-self-types")]
impl<T> core::ops::Receiver for Allocated<T> {
type Target = T;
}

impl<T: ?Sized + Message> Allocated<T> {
/// # Safety
Expand Down Expand Up @@ -349,4 +356,20 @@ mod tests {
let obj: Allocated<RcTestObject> = unsafe { Allocated::new(ptr::null_mut()) };
let _ = obj.set_ivars(());
}

#[test]
#[cfg(feature = "unstable-arbitrary-self-types")]
fn arbitrary_self_types() {
use crate::rc::Retained;
use crate::{extern_methods, AllocAnyThread};

extern_methods!(
unsafe impl RcTestObject {
#[method_id(init)]
fn init_with_self(self: Allocated<Self>) -> Retained<Self>;
}
);

let _ = RcTestObject::alloc().init_with_self();
}
}

0 comments on commit 602f29e

Please sign in to comment.