-
Notifications
You must be signed in to change notification settings - Fork 155
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
JNIEnv::with_local_frame
should be able to return any value, not just jni::results::Result<JObject>
#399
Comments
Yeah, overall it seems like there are quite a few trade offs and it's tricky to find something that's ergonomic for the largest number of use cases. I think we can probably also keep in mind the possibility for applications/crates to create slightly more ergonomic wrappers in situations where they use the API frequently and they can be more explicit about the Result/Error type they want to use in their crate. Although the nesting of With the separate Results it does also seem like a reasonable separation of concerns, even though it's extra work to unwrap. The outer Result conceptually relates to the |
To give an update here @argv-minus-one - I tried out your iteration of this which has At least for how I was starting to use It'll probably also make sense to update the |
This is to continue the conversation begun in #398 (comment).
In brief, we want to change
JNIEnv::with_local_frame
to be able to return any result type, not justjni::results::Result<JObject>
.@rib, I'm mostly okay with your proposed change, but I have one issue with it. Because
with_local_frame
returnsjni::results::Result<T>
, and because the closure will always perform JNI calls and therefore potentially fail withjni::Results::Error
,with_local_frame
will always return nestedResult
s. This has already given you some trouble.There is a method
Result::flatten
to unnestResult
s, but:The nested
Result
s can easily be unnested with the?
operator in some situations:But that's only true if
with_local_frame
is called in a function that returnsResult
and the error type implements bothFrom<jni::errors::Error>
andFrom<E>
(whereE
is the error type returned by the closure). Otherwise, theResult
s have to be unnested explicitly:I'm not sure if this is actually better or not, but here's another idea: have the
with_local_frame
closure return aResult
, but with any error type that implementsFrom<jni::errors::Error>
. That way, a different error type can be used without nestingResult
s. If there is a problem pushing or popping the local frame, the error is converted into the closure's error type.The catch is the compiler won't be able to infer the closure's error type unless it's spelled out somewhere, like with an explicit return type on the closure, with a turbofish on
with_local_frame
, or by directly returning the result of a function call without wrapping it inOk
. (A default for the type parameterE
would solve this problem, but unfortunately Rust doesn't support that.)Neither approach could be truly called ergonomic, but it seems easier to write out an explicit error type on the closure than to unnest
Result
s and convert errors explicitly. At least, I think so. Let me know what you think of this.If we do go with this approach, I'll add documentation about writing out the error type explicitly.
The text was updated successfully, but these errors were encountered: