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

Some suggestions regarding objc2-foundation and block2 #615

Open
libark opened this issue May 11, 2024 · 2 comments
Open

Some suggestions regarding objc2-foundation and block2 #615

libark opened this issue May 11, 2024 · 2 comments
Labels
A-framework Affects the framework crates and the translator for them A-objc2 Affects the `objc2`, `objc-sys` and/or `objc2-encode` crates

Comments

@libark
Copy link

libark commented May 11, 2024

  1. When using NSArray or NSDictionary, their values are usually generic types, so they need to be defined using AnyObject, such as NSMutableDictionary::<NSString, AnyObject>. However, for types like NSNumber, multiple calls to into_super or unsafe methods such as Id::cast are required to convert them to AnyObject. Considering the frequency of this operation, could a safe method be provided to complete the conversion from NSNumber to AnyObject in one step?

  2. When using many Apple frameworks, it is necessary to pass the static CFString exported from the framework to NSString. Could a method be added for NSString, create NString from a raw pointer?

  3. When using block2, all parameter and return value types must implement Encode trait, but many Apple frameworks do not use Objective-C interfaces and types. Could a feature be added to block2 to allow it to be used in C interface framework wrappers, replace block?

@madsmtm
Copy link
Owner

madsmtm commented May 17, 2024

Thanks for the review!

  1. When using NSArray or NSDictionary, their values are usually generic types, so they need to be defined using AnyObject, such as NSMutableDictionary::<NSString, AnyObject>. However, for types like NSNumber, multiple calls to into_super or unsafe methods such as Id::cast are required to convert them to AnyObject. Considering the frequency of this operation, could a safe method be provided to complete the conversion from NSNumber to AnyObject in one step?

This would indeed be very nice, it's tracked in #518.

  1. When using many Apple frameworks, it is necessary to pass the static CFString exported from the framework to NSString. Could a method be added for NSString, create NString from a raw pointer?

Hmm, the issue is that the CFString is from core-foundation, and I would like to avoid a dependency on that if possible. The layout of NSString is guaranteed though, so creating it from a raw pointer is fairly simple:

let s: CFString = ...;
let ptr: CFStringRef = s.as_concrete_TypeRef();
let ptr: *const NSString = s.cast();
// SAFETY: CFString is toll-free bridged with NSString
let s: &NSString = unsafe { ptr.as_ref().unwrap() };
  1. When using block2, all parameter and return value types must implement Encode trait, but many Apple frameworks do not use Objective-C interfaces and types. Could a feature be added to block2 to allow it to be used in C interface framework wrappers, replace block?

The problem here is that in the future, I'd like to use these encodings to emit block encodings properly, which is used by some methods, see #442 (and #434 for a workaround).

Specifically for core-foundation, you can use something like:

#[repr(transparent)]
struct CustomCFStringRef(CFStringRef);

// SAFETY: `CustomCFStringRef` is `#[repr(transparent)]` over `CFStringRef`
unsafe impl Encode for CustomCFStringRef {
    const ENCODING: Encoding = Encoding::Pointer(&Encoding::Struct("__CFString", &[]));
}

And pass CustomCFStringRef to/from the block.

@madsmtm
Copy link
Owner

madsmtm commented May 17, 2024

I've added more detailed documentation on interop with core-foundation in eb39164

@madsmtm madsmtm added A-objc2 Affects the `objc2`, `objc-sys` and/or `objc2-encode` crates A-framework Affects the framework crates and the translator for them labels May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-framework Affects the framework crates and the translator for them A-objc2 Affects the `objc2`, `objc-sys` and/or `objc2-encode` crates
Projects
None yet
Development

No branches or pull requests

2 participants