-
Notifications
You must be signed in to change notification settings - Fork 13
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
Basic version of NSError #16
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See also fruity
's NSError
for inspiration.
I'm not sure how the Swift-side works, but I can imagine a macro that would at least make the Rust-side a bit nicer:
#[no_mangle]
#[my_nserror_macro]
pub extern "C" fn initThing(json: &NSData) -> Result<(), Id<NSError>> {
if let Err(err) = init_thing(json.bytes()) {
let value = NSString::from_str(&format!("{:?}", err));
let user_info = NSDictionary::from_keys_and_objects(&[&*NSLocalizedDescriptionKey()], vec![value]);
Err(NSError::new(0, NSString::from_str("myDomain"), user_info))
} else {
Ok(())
}
}
// After `my_nserror_macro`
#[no_mangle]
pub extern "C" fn initThing(
json: &NSData,
error: *mut *const NSError,
) -> bool {
// fn inner = The original function
if let Err(nserror) = inner(json) {
unsafe {
*error = &*nserror;
mem::forget(nserror);
}
false
} else {
true
}
}
And maybe a impl<T: std::error::Error> From<T> for Id<NSError>
or similar implementation, and vice-versa, would make it even nicer?
pub type NSInteger = isize; | ||
pub type NSUInteger = usize; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these could be useful to have in objc
, the reason I haven't added them myself is that I'm yet not completely sure they will always be isize
/usize
on more exotic platforms like Windows using GNUStep
#[allow(non_snake_case)] | ||
pub fn NSLocalizedDescriptionKey() -> Id<NSString> { | ||
NSString::from_str(&"NSLocalizedDescription") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An alternative would be to use the lazy_static!
macro, but I think your approach is fine.
In the future something like fruity
's nsstring!
could be really nice, but it is very difficult to ensure the correctness of, similar to SSheldon/rust-objc#49.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, this actually has value "NSLocalizedDescriptionKey"
on GNUStep
@@ -1,3 +1,4 @@ | |||
doc/ | |||
target/ | |||
Cargo.lock | |||
.idea |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding this to your ~/.gitignore_global
instead? Unless IntelliJ works differently than how I think it does.
Inspiration from SSheldon/rust-objc-foundation#16 Co-authored-by: Rob Napier <rob@neverwood.org>
This is not a full PR, but I want to open it up to start talking about it. There are several things in here:
NSLocalizedDescriptionKey()
, but this doesn't feel like a great way to define constants (could they be imported from ObjC directly? Could they be const rather than a function?)The objc2 repo doesn't seem ready for primetime. Is it better to extend this one or work there? Is there a better approach to all of this? My primary need is to call Rust code from Swift, and I'm looking for how to best do that. Currently I'm using this NSError as follows:
I then consume it with this:
I don't think this is a fully baked approach (and it's very ugly), so I'm looking for direction.