Stream: git-wasmtime

Topic: wasmtime / PR #1790 wiggle: allow user-configurable error...


view this post on Zulip Wasmtime GitHub notifications bot (May 29 2020 at 20:09):

pchickey opened PR #1790 from pch/wiggle_error_transforms to master:

One issue we've had with wiggle ergonomics is that we often want to use a rich error type (a Rust enum that uses thiserror::Error, or maybe an anyhow::Error) to write the library code that ends up in an impl of the wiggle-defined module traits. However, those module traits require an error type that corresponds to the witx document.

Because Rust doesn't have try, there's not a lot of good mechanical ways to automatically replace all uses of a witx document error type like wasi's Errno with a rich type and a conversion function.
So, this PR adds a totally opt-in facility to wiggle to perform such a conversion on the user's behalf.

The new test src/wiggle/tests/errors.rs shows how the types work out on a modified version of the array test. A new optional wiggle proc-macro argument errors gives a mapping of witx identifiers (less the leading $, because proc macro parsing is enough trouble without that) to the user's desired error type, e.g.:

wiggle::from_witx!({
    witx: ["tests/arrays.witx"],
    ctx: WasiCtx,
    errors: { errno => RichError },
});

When the errors config is provided, a UserErrorConversion trait is defined in types that the user must implement for their ctx type. The method names in this enum are determined mechanically.

impl<'a> types::UserErrorConversion for WasiCtx<'a> {
    fn errno_from_rich_error(&self, _e: RichError) -> types::Errno {
        unimplemented!();
    }
}

The RichError type is then used in place of types::Errno in the witx module traits:

impl<'a> arrays::Arrays for WasiCtx<'a> {
    fn populate_excuses(&self, _excuses: &types::ExcuseArray) -> Result<(), RichError> {
        unimplemented!()
    }
   (snip)
}

Open questions:

<!--

Please ensure that the following steps are all taken care of before submitting
the PR.

Please ensure all communication adheres to the code of conduct.
-->

view this post on Zulip Wasmtime GitHub notifications bot (May 29 2020 at 20:09):

pchickey requested alexcrichton and kubkon for a review on PR #1790.

view this post on Zulip Wasmtime GitHub notifications bot (May 29 2020 at 20:09):

pchickey requested alexcrichton and kubkon for a review on PR #1790.

view this post on Zulip Wasmtime GitHub notifications bot (May 29 2020 at 20:10):

pchickey edited PR #1790 from pch/wiggle_error_transforms to master:

One issue we've had with wiggle ergonomics is that we often want to use a rich error type (a Rust enum that uses thiserror::Error, or maybe an anyhow::Error) to write the library code that ends up in an impl of the wiggle-defined module traits. However, those module traits require an error type that corresponds to the witx document.

Because Rust doesn't have try, there's not a lot of good mechanical ways to automatically replace all uses of a witx document error type like wasi's Errno with a rich type and a conversion function.
So, this PR adds a totally opt-in facility to wiggle to perform such a conversion on the user's behalf.

The new test src/wiggle/tests/errors.rs shows how the types work out on a modified version of the array test. A new optional wiggle proc-macro argument errors gives a mapping of witx identifiers (less the leading $, because proc macro parsing is enough trouble without that) to the user's desired error type, e.g.:

wiggle::from_witx!({
    witx: ["tests/arrays.witx"],
    ctx: WasiCtx,
    errors: { errno => RichError },
});

When the errors config is provided, a UserErrorConversion trait is defined in types that the user must implement for their ctx type. The method names in this enum are determined mechanically.

impl<'a> types::UserErrorConversion for WasiCtx<'a> {
    fn errno_from_rich_error(&self, _e: RichError) -> types::Errno {
        unimplemented!();
    }
}

The RichError type is then used in place of types::Errno in the witx module traits:

impl<'a> arrays::Arrays for WasiCtx<'a> {
    fn populate_excuses(&self, _excuses: &types::ExcuseArray) -> Result<(), RichError> {
        unimplemented!()
    }
   (snip)
}

Open questions:

<!--

Please ensure that the following steps are all taken care of before submitting
the PR.

Please ensure all communication adheres to the code of conduct.
-->

view this post on Zulip Wasmtime GitHub notifications bot (May 29 2020 at 20:12):

pchickey edited PR #1790 from pch/wiggle_error_transforms to master:

One issue we've had with wiggle ergonomics is that we often want to use a rich error type (a Rust enum that uses thiserror::Error, or maybe an anyhow::Error) to write the library code that ends up in an impl of the wiggle-defined module traits. However, those module traits require an error type that corresponds to the witx document.

Because Rust doesn't have try, there's not a lot of good mechanical ways to automatically replace all uses of a witx document error type like wasi's Errno with a rich type and a conversion function.
So, this PR adds a totally opt-in facility to wiggle to perform such a conversion on the user's behalf.

The new test src/wiggle/tests/errors.rs shows how the types work out on a modified version of the array test. A new optional wiggle proc-macro argument errors gives a mapping of witx identifiers (less the leading $, because proc macro parsing is enough trouble without that) to the user's desired error type, e.g.:

pub enum RichError { (snip) }
wiggle::from_witx!({
    witx: ["tests/arrays.witx"],
    ctx: WasiCtx,
    errors: { errno => RichError },
});

When the errors config is provided, a UserErrorConversion trait is defined in types that the user must implement for their ctx type. The method names in this enum are determined mechanically.

impl<'a> types::UserErrorConversion for WasiCtx<'a> {
    fn errno_from_rich_error(&self, _e: RichError) -> types::Errno {
        unimplemented!();
    }
}

The RichError type is then used in place of types::Errno in the witx module traits:

impl<'a> arrays::Arrays for WasiCtx<'a> {
    fn populate_excuses(&self, _excuses: &types::ExcuseArray) -> Result<(), RichError> {
        unimplemented!()
    }
   (snip)
}

Open questions:

<!--

Please ensure that the following steps are all taken care of before submitting
the PR.

Please ensure all communication adheres to the code of conduct.
-->

view this post on Zulip Wasmtime GitHub notifications bot (May 29 2020 at 20:28):

alexcrichton submitted PR Review.

view this post on Zulip Wasmtime GitHub notifications bot (May 29 2020 at 20:52):

kubkon submitted PR Review.

view this post on Zulip Wasmtime GitHub notifications bot (May 30 2020 at 21:07):

pchickey updated PR #1790 from pch/wiggle_error_transforms to master:

One issue we've had with wiggle ergonomics is that we often want to use a rich error type (a Rust enum that uses thiserror::Error, or maybe an anyhow::Error) to write the library code that ends up in an impl of the wiggle-defined module traits. However, those module traits require an error type that corresponds to the witx document.

Because Rust doesn't have try, there's not a lot of good mechanical ways to automatically replace all uses of a witx document error type like wasi's Errno with a rich type and a conversion function.
So, this PR adds a totally opt-in facility to wiggle to perform such a conversion on the user's behalf.

The new test src/wiggle/tests/errors.rs shows how the types work out on a modified version of the array test. A new optional wiggle proc-macro argument errors gives a mapping of witx identifiers (less the leading $, because proc macro parsing is enough trouble without that) to the user's desired error type, e.g.:

pub enum RichError { (snip) }
wiggle::from_witx!({
    witx: ["tests/arrays.witx"],
    ctx: WasiCtx,
    errors: { errno => RichError },
});

When the errors config is provided, a UserErrorConversion trait is defined in types that the user must implement for their ctx type. The method names in this enum are determined mechanically.

impl<'a> types::UserErrorConversion for WasiCtx<'a> {
    fn errno_from_rich_error(&self, _e: RichError) -> types::Errno {
        unimplemented!();
    }
}

The RichError type is then used in place of types::Errno in the witx module traits:

impl<'a> arrays::Arrays for WasiCtx<'a> {
    fn populate_excuses(&self, _excuses: &types::ExcuseArray) -> Result<(), RichError> {
        unimplemented!()
    }
   (snip)
}

Open questions:

<!--

Please ensure that the following steps are all taken care of before submitting
the PR.

Please ensure all communication adheres to the code of conduct.
-->

view this post on Zulip Wasmtime GitHub notifications bot (Jun 01 2020 at 22:12):

pchickey requested alexcrichton for a review on PR #1790.

view this post on Zulip Wasmtime GitHub notifications bot (Jun 02 2020 at 16:37):

alexcrichton submitted PR Review.

view this post on Zulip Wasmtime GitHub notifications bot (Jun 02 2020 at 16:38):

pchickey merged PR #1790.


Last updated: Nov 22 2024 at 16:03 UTC