Completely rework result handling #478
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR completely changes how result handling is done.
A fundamental problem with the current approach is that the following values are legal to return from a handler:
Err(..)
variant.Result
into an internal panic, if it's theErr(..)
variant.Result
into aValue
.So let's say we want to know what the type of the expression is through a trait. For the first two variants, the type should be determined by
T
:But what about the second variant? Well, the type is supposed to be
RESULT_TYPE
because aResult
is an internal type, which leads to an implementation like this:But the keen-eyed might note that this implementation conflicts with the first one.
This PR introduces a separate type for propagating internal errors called
VmResult
. This type is completely unrelated tostd::result::Result
and is used internally whereverResult<T, VmError>
was previously used.By having a separate type, the first implementation becomes obvious, and it no longer conflicts with the
Result<T, E>
type:A downside is that since
try_trait_v2
is not stable (yet), we can't use the try operator?
on it. So we also introducerune::vm_try!
to fill this gap, like thetry!
macro used to in Rust. Another downside is thatResult
combinators such asResult::map_err
are not present.Wherever appropriate we try to convert this into
Result<T, VmError>
so that it works as intended, however this is not always doable. If you ever encounter aVmResult
in the wild but you need aResult<T, VmError>
you can callVmResult::into_result
.The use of
FromValue
andToValue
is also somewhat complicated as a result. Two more utility methods have been added (which are also long overdue):rune::from_value<T>(value: Value) -> Result<T, VmError>
.rune::to_value<T>(value: T) -> Result<Value, VmError>
.As a result of this PR, documentation types now display the correct return values, from this:
To this: