Skip to content

Allow using ? operator for custom error types in Function callbacks #1606

Closed
@webmaster128

Description

@webmaster128

I'm trying to port an import implementation to Wasmer Reborn. What I have so far is:

                "db_read" => Function::new_with_env(&store, &u32_to_u32, env.clone(), |env, args| {
                    let key_ptr = args[0].unwrap_i32() as u32;
                    let ptr = do_read::<S, Q>(env, key_ptr)?;
                    Ok(vec![ptr.into()])
                }),

where do_read returns a core::result::Result<u32, VmError>. Now I implemented

impl Into<wasmer_engine::RuntimeError> for VmError {
    fn into(self) -> wasmer_engine::RuntimeError {
        let msg: String = self.to_string();
        wasmer_engine::RuntimeError::new(msg)
    }
}

From<T> for U implies Into<U> for T but not the other way round.

Unfortunately the ? operator required From to be implemented, not Into. I don't know why this was chosen by Rust but I don't think it will change any time soon.

Would it be possible to require Into<RuntimeError> instead of RuntimeError in the callback like this:

    pub fn new_with_env<F, Err, Env>(store: &Store, ty: &FunctionType, env: Env, func: F) -> Self
    where
        F: Fn(&mut Env, &[Val]) -> Result<Vec<Val>, Err> + 'static,
        Err: Into<RuntimeError>,
        Env: Sized + 'static,
    {

Any other ideas how to deal with such use cases? In our old code, Wasmer did not care about the error type in the callback's Result.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions