Skip to content

Add rc::Unknown and rc::MaybeOwnership #87

Closed
@madsmtm

Description

@madsmtm

Rough sketch:

When generating bindings to Objective-C classes automatically (e.g. with bindgen, see #85), it would be nice to return a reference counting pointer (e.g. an Id). Sadly, however, it is not possible to know the ownership of that without inspection of the entire class; even handing out immutable references can easily trigger undefined behaviour (e.g. if there was a Id<T, Owned> somewhere else in the program).

Therefore it would be beneficial to add a reference-counting pointer that does not guarantee any form of ownership, like the StrongPtr removed in e08e87d. See also upstream SSheldon/rust-objc#24.

This can probably be accomplished by adding a rc::Unknown, similar to rc::Owned and rc::Shared.
On initial inspection, you would just implement rc::Ownership for rc::Unknown, but that makes all high-level code unable to deal with the "normal" case where you want to know the ownership, so we should consider adding rc::MaybeOwnership as well to deal with this case.

Example code changes:

pub trait MaybeOwnership: private::Sealed + 'static {}
pub trait Ownership: MaybeOwnership {}

impl MaybeOwnership for Unknown {}
impl MaybeOwnership for Owned {}
impl MaybeOwnership for Shared {}
impl Ownership for Owned {}
impl Ownership for Shared {}


pub struct Id<T, O: MaybeOwnership> { ... }

impl<T: Message> Id<T, Unknown> {
    pub unsafe fn into_shared(self) -> Id<T, Shared> { ... }
    pub unsafe fn into_owned(self) -> Id<T, Owned> { ... }
}

// Still only `Owned` and `Shared` are `Deref`!
impl<T, O: Ownership> Deref for Id<T, O> { ... }

// `Unknown` can also be sent messages (since Objective-C doesn't care about ownership)
unsafe impl<T: Message, O: MaybeOwnership> MessageReceiver for Id<T, O> { ... }

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions