Skip to content

Commit

Permalink
Merge pull request #672 from madsmtm/rename-id-retained
Browse files Browse the repository at this point in the history
Rename internal instances of "id" to "retained"
  • Loading branch information
madsmtm authored Nov 21, 2024
2 parents abfbcd9 + 371fc73 commit 40daa2b
Show file tree
Hide file tree
Showing 46 changed files with 471 additions and 439 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,5 @@ files.extend-exclude = [
]
# Comes from the `clang` crate
default.extend-identifiers.ParmDecl = "ParmDecl"
# To be done as a larger rename
default.extend-identifiers.MessageRecieveId = "MessageRecieveId"
# Used in Metal, LOD = level of detail
default.extend-words.lod = "lod"
64 changes: 32 additions & 32 deletions crates/objc2/src/__macro_helpers/declare_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ use super::{CopyOrMutCopy, Init, MaybeUnwrap, New, Other};
#[repr(transparent)]
#[derive(Debug)]
#[allow(dead_code)]
pub struct IdReturnValue(pub(crate) *mut AnyObject);
pub struct RetainedReturnValue(pub(crate) *mut AnyObject);

// SAFETY: `IdReturnValue` is `#[repr(transparent)]`
unsafe impl Encode for IdReturnValue {
// SAFETY: `RetainedReturnValue` is `#[repr(transparent)]`
unsafe impl Encode for RetainedReturnValue {
const ENCODING: Encoding = <*mut AnyObject>::ENCODING;
}

Expand All @@ -37,100 +37,100 @@ unsafe impl Encode for IdReturnValue {
// can't actually modify the `self` argument (e.g. `let self = foo(self)` is
// not allowed).
//
// See `MsgSendId` and `RetainSemantics` for details on the retain semantics
// See `MsgSendRetained` and `RetainSemantics` for details on the retain semantics
// we're following here.
pub trait MessageRecieveId<Receiver, Ret> {
fn into_return(obj: Ret) -> IdReturnValue;
pub trait MessageReceiveRetained<Receiver, Ret> {
fn into_return(obj: Ret) -> RetainedReturnValue;
}

// Receiver and return type have no correlation
impl<Receiver, Ret> MessageRecieveId<Receiver, Ret> for New
impl<Receiver, Ret> MessageReceiveRetained<Receiver, Ret> for New
where
Receiver: MessageReceiver,
Ret: MaybeOptionId,
Ret: MaybeOptionRetained,
{
#[inline]
fn into_return(obj: Ret) -> IdReturnValue {
fn into_return(obj: Ret) -> RetainedReturnValue {
obj.consumed_return()
}
}

// Explicitly left unimplemented for now!
// impl MessageRecieveId<impl MessageReceiver, Allocated<T>> for Alloc {}
// impl MessageReceiveRetained<impl MessageReceiver, Allocated<T>> for Alloc {}

// Note: `MethodImplementation` allows for `Allocated` as the receiver, so we
// restrict it here to only be when the selector is `init`.
//
// Additionally, the receiver and return type must have the same generic
// parameter `T`.
impl<Ret, T> MessageRecieveId<Allocated<T>, Ret> for Init
impl<Ret, T> MessageReceiveRetained<Allocated<T>, Ret> for Init
where
T: Message,
Ret: MaybeOptionId<Input = Option<Retained<T>>>,
Ret: MaybeOptionRetained<Input = Option<Retained<T>>>,
{
#[inline]
fn into_return(obj: Ret) -> IdReturnValue {
fn into_return(obj: Ret) -> RetainedReturnValue {
obj.consumed_return()
}
}

// Receiver and return type have no correlation
impl<Receiver, Ret> MessageRecieveId<Receiver, Ret> for CopyOrMutCopy
impl<Receiver, Ret> MessageReceiveRetained<Receiver, Ret> for CopyOrMutCopy
where
Receiver: MessageReceiver,
Ret: MaybeOptionId,
Ret: MaybeOptionRetained,
{
#[inline]
fn into_return(obj: Ret) -> IdReturnValue {
fn into_return(obj: Ret) -> RetainedReturnValue {
obj.consumed_return()
}
}

// Receiver and return type have no correlation
impl<Receiver, Ret> MessageRecieveId<Receiver, Ret> for Other
impl<Receiver, Ret> MessageReceiveRetained<Receiver, Ret> for Other
where
Receiver: MessageReceiver,
Ret: MaybeOptionId,
Ret: MaybeOptionRetained,
{
#[inline]
fn into_return(obj: Ret) -> IdReturnValue {
fn into_return(obj: Ret) -> RetainedReturnValue {
obj.autorelease_return()
}
}

/// Helper trait for specifying an `Retained<T>` or an `Option<Retained<T>>`.
///
/// (Both of those are valid return types from declare_class! `#[method_id]`).
pub trait MaybeOptionId: MaybeUnwrap {
fn consumed_return(self) -> IdReturnValue;
fn autorelease_return(self) -> IdReturnValue;
pub trait MaybeOptionRetained: MaybeUnwrap {
fn consumed_return(self) -> RetainedReturnValue;
fn autorelease_return(self) -> RetainedReturnValue;
}

impl<T: Message> MaybeOptionId for Retained<T> {
impl<T: Message> MaybeOptionRetained for Retained<T> {
#[inline]
fn consumed_return(self) -> IdReturnValue {
fn consumed_return(self) -> RetainedReturnValue {
let ptr: *mut T = Retained::into_raw(self);
IdReturnValue(ptr.cast())
RetainedReturnValue(ptr.cast())
}

#[inline]
fn autorelease_return(self) -> IdReturnValue {
fn autorelease_return(self) -> RetainedReturnValue {
let ptr: *mut T = Retained::autorelease_return(self);
IdReturnValue(ptr.cast())
RetainedReturnValue(ptr.cast())
}
}

impl<T: Message> MaybeOptionId for Option<Retained<T>> {
impl<T: Message> MaybeOptionRetained for Option<Retained<T>> {
#[inline]
fn consumed_return(self) -> IdReturnValue {
fn consumed_return(self) -> RetainedReturnValue {
let ptr: *mut T = Retained::consume_as_ptr_option(self);
IdReturnValue(ptr.cast())
RetainedReturnValue(ptr.cast())
}

#[inline]
fn autorelease_return(self) -> IdReturnValue {
fn autorelease_return(self) -> RetainedReturnValue {
let ptr: *mut T = Retained::autorelease_return_option(self);
IdReturnValue(ptr.cast())
RetainedReturnValue(ptr.cast())
}
}

Expand Down
5 changes: 3 additions & 2 deletions crates/objc2/src/__macro_helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ pub use self::class::{MainThreadOnlyDoesNotImplSendSync, ValidThreadKind};
pub use self::common_selectors::{alloc_sel, dealloc_sel, init_sel, new_sel};
pub use self::convert::{ConvertArgument, ConvertArguments, ConvertReturn, TupleExtender};
pub use self::declare_class::{
ClassBuilderHelper, ClassProtocolMethodsBuilder, IdReturnValue, MaybeOptionId, MessageRecieveId,
ClassBuilderHelper, ClassProtocolMethodsBuilder, MaybeOptionRetained, MessageReceiveRetained,
RetainedReturnValue,
};
pub use self::declared_ivars::DeclaredIvarsHelper;
pub use self::image_info::ImageInfo;
Expand All @@ -40,7 +41,7 @@ pub use self::method_family::{
};
pub use self::module_info::ModuleInfo;
pub use self::msg_send::MsgSend;
pub use self::msg_send_retained::{MaybeUnwrap, MsgSendId, MsgSendSuperId};
pub use self::msg_send_retained::{MaybeUnwrap, MsgSendRetained, MsgSendSuperRetained};
pub use self::os_version::{is_available, AvailableVersion, OSVersion};
pub use self::sync_unsafe_cell::SyncUnsafeCell;

Expand Down
2 changes: 1 addition & 1 deletion crates/objc2/src/__macro_helpers/msg_send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub trait MsgSend: Sized {
unsafe { self.send_super_message(<Self::Inner as ClassType>::Super::class(), sel, args) }
}

// Error functions below. See MsgSendId::send_message_id_error for further
// Error functions below. See MsgSendRetained::send_message_retained_error for further
// details.
//
// Some of this could be abstracted away using closures, but that would
Expand Down
Loading

0 comments on commit 40daa2b

Please sign in to comment.