Skip to content

Completely rework result handling #478

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

Merged
merged 10 commits into from
Apr 5, 2023
Merged

Completely rework result handling #478

merged 10 commits into from
Apr 5, 2023

Conversation

udoprog
Copy link
Collaborator

@udoprog udoprog commented Apr 5, 2023

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:

  • Result<T, VmError> - which converts the error into an internal vm error if it's the Err(..) variant.
  • Result<T, Panic> - which converts the Result into an internal panic, if it's the Err(..) variant.
  • Result<T, E> - which converts the Result into a Value.

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:

impl<T> TypeOf Result<T, VmError> where T: TypeOf {
    fn type_of() -> FullTypeOf {
        T::type_of()
    }
}

But what about the second variant? Well, the type is supposed to be RESULT_TYPE because a Result is an internal type, which leads to an implementation like this:

impl<T, E> TypeOf Result<T, E> {
    fn type_of() -> FullTypeOf {
        RESULT_TYPE.into()
    }
}

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 to std::result::Result and is used internally wherever Result<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:

impl<T> TypeOf VmResult<T> where T: TypeOf {
    fn type_of() -> FullTypeOf {
        T::type_of()
    }
}

A downside is that since try_trait_v2 is not stable (yet), we can't use the try operator ? on it. So we also introduce rune::vm_try! to fill this gap, like the try! macro used to in Rust. Another downside is that Result combinators such as Result::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 a VmResult in the wild but you need a Result<T, VmError> you can call VmResult::into_result.

The use of FromValue and ToValue 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:

image

To this:

image

@udoprog udoprog added the enhancement New feature or request label Apr 5, 2023
@udoprog udoprog merged commit 4d2d63c into main Apr 5, 2023
@udoprog udoprog deleted the vm-result branch April 5, 2023 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant