Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ownership in objc2-foundation #29

Closed
madsmtm opened this issue Sep 5, 2021 · 2 comments
Closed

Fix ownership in objc2-foundation #29

madsmtm opened this issue Sep 5, 2021 · 2 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@madsmtm
Copy link
Owner

madsmtm commented Sep 5, 2021

After #21 I can go through objc2-foundation and re-evaluate how we should do the ownership stuff. Especially difficult with generic objects like NSArray!

The idea is it we fix the soundness issues in SSheldon/rust-objc-foundation#10, see my comment there.

Probably some rework of INSObject is required.

Should also figure out the safety of "downgrading" (e.g. &MyObject -> &NSObject)? And from there &NSObject -> Id<NSObject, Shared> (which deletes the original lifetime). What about when MyObject<'a> itself carries lifetime information? Maybe add lifetime to Id? Moved to #58

Also, should we add an ObjectType/RetainAble/... type, since not all Message types can actually be used in Id? Moved to #66

Idea:

unsafe trait INSObject {
    type Own: Ownership;
    // ...

    fn new() -> Id<Self, O> { ... }
}

unsafe trait INSCopying: INSObject {
    fn copy(&self) -> Id<Self, Shared>;
}

unsafe trait INSMutableCopying: INSObject<Own = Owned> {
    fn copy_mut(&self) -> Id<Self, Owned>;
}

unsafe impl INSObject for NSString {
    type Own = Shared; // This means there may never exist an `&mut NSString` (and by extension no `Id<NSString, Owned>`)!
}
unsafe impl INSObject for NSMutableString {
    type Own = Owned; // Both `Id<NSString, Owned>` and `Id<NSString, Shared>` are possible
}
@madsmtm madsmtm added bug Something isn't working enhancement New feature or request labels Sep 5, 2021
@madsmtm madsmtm mentioned this issue Sep 5, 2021
80 tasks
@madsmtm
Copy link
Owner Author

madsmtm commented Sep 5, 2021

NSArray + NSEnumerator stuff (haven't looked at the current implementation, so these are very vague ideas):

trait INSArray: INSObject {
    type Item: INSObject; // ??
    type Enumerator = NSEnumerator; // ???

    fn iter(&'a self) -> Self::Enumerator<'a, Self, Shared>;

    // Can't iterate mutably over `NSString`?
    fn iter_mut<'a>(&'a mut self) -> Self::Enumerator<'a, Self, Owned>
    where
        <Self::Item as INSObject>::Own = Owned;
}

struct NSEnumerator<'a, T, O: Ownership>(...);

impl Iterator for NSEnumerator<'a, T, Shared> {
    type Item = &'a T;
    fn next(&'a mut self) -> Self::Item {...}
}

impl Iterator for NSEnumerator<'a, T, Owned> {
    type Item = &'a mut T;
    fn next(&'a mut self) -> Self::Item {...}
}

// How does NSFastEnumeration work?

NSArray::iter return type depends on what NSEnumerator actually does? If it:

  • References the array internally:
    • Retained<NSEnumerator<'a, T>, Owned> or &'a mut NSEnumerator<T>
  • Clones and/or retains the array:
    • Retained<NSEnumerator<T>, Owned> or &'pool mut NSEnumerator<T>

Iterator::Item depends on what NSEnumerator -next actually does? If it:

  • Returns retained references:
    • Item = Retained<T, O>
      • Is this safe or are we missing some lifetime info?
  • Returns raw pointers into the enumerator / array:
    • Item = &'a T

@madsmtm madsmtm mentioned this issue Sep 9, 2021
13 tasks
@madsmtm madsmtm mentioned this issue Oct 5, 2021
6 tasks
@madsmtm madsmtm changed the title Fix ownership in objc_foundation Fix ownership in objc-foundation Nov 3, 2021
@madsmtm madsmtm changed the title Fix ownership in objc-foundation Fix ownership in objc2-foundation Nov 3, 2021
@madsmtm madsmtm mentioned this issue Nov 3, 2021
34 tasks
@madsmtm
Copy link
Owner Author

madsmtm commented Nov 3, 2021

Fixed in #40, other points are moved to other issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant