Skip to content

Register new proto in runtime #820

@pronebird

Description

@pronebird

Hi,

Trying to register my own protocol for use with NSXPCInterface. For some reason, the protocol is never registered and <dyn NSConnectionInterface>::protocol() returns None.

Trying to register protocol manually but I arrive at objc exception:

thread '' (1637226) panicked at crates/nym-ipc/src/xpc/client.rs:71:6:
objc exception!: Some(exception <NSException: 0x106227730> 'NSInvalidArgumentException' reason: NSXPCInterface: Unable to get extended method signature from Protocol data (NSConnectionInterface / write:). Use of clang is required for NSXPCInterface.)

Some good reads too: https://pyobjc.readthedocs.io/en/latest/notes/using-nsxpcinterface.html

Besides xpc shenanigans which I don't think we can workaround without using objc. My first question is, how do we ensure that protocols defined in Rust are registered with runtime? This seems to automatic in objc right?

Example

extern_protocol!(
    #[allow(clippy::missing_safety_doc)]
    pub unsafe trait NSConnectionInterface {
        #[unsafe(method(write:))]
        fn write(&self, buf: &NSData);
    }
);

const CONNECTION_INTERFACE_PROTOCOL: &CStr = c"NSConnectionInterface";
pub(crate) fn connection_interface() -> Retained<NSXPCInterface> {
    let proto = match <dyn NSConnectionInterface>::protocol() {
        Some(proto) => proto,
        None => {
            let mut builder = ProtocolBuilder::new(CONNECTION_INTERFACE_PROTOCOL)
                .expect("Failed to create protocol builder");
            builder.add_method_description::<(&NSData,), ()>(objc2::sel!(write:), true);
            let proto = builder.register();
            proto
        }
    };

    unsafe { NSXPCInterface::interfaceWithProtocol(proto) }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions