Skip to content

Support error return and out parameters in define_class! #283

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

Open
madsmtm opened this issue Nov 3, 2022 · 1 comment
Open

Support error return and out parameters in define_class! #283

madsmtm opened this issue Nov 3, 2022 · 1 comment
Labels
A-objc2 Affects the `objc2`, `objc2-exception-helper` and/or `objc2-encode` crates enhancement New feature or request
Milestone

Comments

@madsmtm
Copy link
Owner

madsmtm commented Nov 3, 2022

Add the equivalent of #276 and #277 to define_class!.

Effectively the implementation is:

// Result<(), Retained<NSError>> -> Bool
match res {
    Ok(()) => Bool::YES,
    Err(err) => {
        // Intentionally not objc_autoreleaseReturnValue, since the optimization that enables will not be able to kick in
        *__err_param = objc_autorelease(err);
        Bool::NO
    },
}

// Result<Retained<T, O>, Retained<NSError>> -> *mut T
match res {
    Ok(obj) => obj.release_according_to_memory_management_rules(sel), // See #282
    Err(err) => {
        // Intentionally not objc_autoreleaseReturnValue
        *__err_param = objc_autorelease(err);
        ptr::null_mut()
    },
}

See #277 (comment) for further ABI details.

@madsmtm madsmtm added enhancement New feature or request A-objc2 Affects the `objc2`, `objc2-exception-helper` and/or `objc2-encode` crates labels Nov 3, 2022
This was referenced Nov 3, 2022
@madsmtm madsmtm added this to the objc2 v0.3 milestone Jan 27, 2023
@madsmtm madsmtm changed the title Support error return in declare_class! Support error return and out parameters in declare_class! Jan 31, 2023
@madsmtm madsmtm modified the milestones: objc2 v0.3, icrate v0.2.0 Jan 31, 2023
@madsmtm madsmtm added this to the icrate v0.2.0 milestone May 26, 2023
@madsmtm madsmtm modified the milestones: Polish icrate, Usable icrate Sep 5, 2023
@madsmtm madsmtm modified the milestones: Usable icrate, Polish icrate Dec 3, 2023
@madsmtm madsmtm changed the title Support error return and out parameters in declare_class! Support error return and out parameters in define_class! Nov 26, 2024
@madsmtm
Copy link
Owner Author

madsmtm commented Apr 23, 2025

So to be clear, in the intermediate, users must handle this themselves using something like:

// Returning boolean
#[unsafe(method(myMethodWithError:))]
fn my_method_with_error(&self, err_param: Option<&mut *mut NSError>) -> bool {
    let res: Result<(), Retained<NSError>> = todo!();

    match res {
        Ok(()) => true,
        Err(err) => {
            if let Some(err_param) = err_param {
                *err_param = Retained::autorelease_ptr(err);
            }
            false
        },
    }
}

// Returning `Retained` object
#[unsafe(method_id(myMethodWithError:))]
fn my_method_with_error(&self, err: Option<&mut *mut NSError>) -> Option<Retained<Self>> {
    let res: Result<Retained<Self>, Retained<NSError>> = todo!();

    match res {
        Ok(value) => Some(value),
        Err(err) => {
            if let Some(err_param) = err_param {
                *err_param = Retained::autorelease_ptr(err);
            }
            None
        },
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-objc2 Affects the `objc2`, `objc2-exception-helper` and/or `objc2-encode` crates enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant