Closed Ms2ger closed 8 years ago
Ok, now I understand better what's expected. I'll try to do something based on this piece of information.
The Result<Self, Error>
approach makes the most sense to me.
I would prefer one of the two variants that allow try!()
.
C @jdm @Manishearth
Result<Result<Self, String>, ()>
makes the most sense to me, especially the unit part, because that means caller just need to try!
the outer Result
to return early in case of pending exception.
I'm in favour of Result<ConversionResult<Self>, ()>
. I find results inside results confusing to read, and match try!(..) { Ok(...) => ...}
is particularly surprising.
After discussing on IRC, @jdm, @Ms2ger and myself decided on
enum ConversionResult<T> {
Success(T),
Failure(Cow<'static, str>),
}
fn from_jsval(...) -> Result<ConversionResult<Self>, ()>;
Cow<'static, str>
, that is.
Ok, I'll give it a try.
@Ms2ger What if the failure case was a ConversionResult::Failure(ConversionError)
, and the inner error can be promoted to a throw with a unsafe fn throw(self, cx: *mut JSObject) -> Result<ConversionResult<_>, ()>
method?
This way we can do:
match try!(T::from_jsval(cx, ...)) {
ConversionResult::Success(x) => x,
ConversionResult::Failure(error) => error.throw(cx)
}
There are three cases to consider when converting a JS value to a Rust value:
An example of case 3 happens when converting
{ get [Symbol.iterator]() { throw 7; } }
to asequence
type.Right now, the signature is
and we use
throw_type_error
to set a pending exception in the second case.That implies it is impossible for callers to detect whether case 2 or 3 occurred. This is fine for most cases, but unions need to be able to make that distinction (and not set a pending exception for the second case).
Possible solutions
Just what I could come up with right now; feel free to propose other options.
Each section shows any
enum
s used in the proposal, followed by the common conversion case and the union case.Result<Self, Error>
Result<Result<Self, String>, ()>
Result<ConversionResult<Self>, ()>
ConversionResult<Self>
CC @nox @GuillaumeGomez