Skip to content
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

Support implicit broadening using the try operator #11

Open
Zk2u opened this issue May 6, 2024 · 1 comment
Open

Support implicit broadening using the try operator #11

Zk2u opened this issue May 6, 2024 · 1 comment

Comments

@Zk2u
Copy link

Zk2u commented May 6, 2024

Taking the example:

use terrors::OneOf;

struct Timeout;
struct AllocationFailure;

fn allocate_box() -> Result<Box<u8>, OneOf<(AllocationFailure,)>> {
    Err(AllocationFailure.into())
}

fn send() -> Result<(), OneOf<(Timeout,)>> {
    Err(Timeout.into())
}

fn allocate_and_send() -> Result<(), OneOf<(AllocationFailure, Timeout)>> {
    let boxed_byte: Box<u8> = allocate_box().map_err(OneOf::broaden)?;
    send().map_err(OneOf::broaden)?;

    Ok(())
}

// ...

Instead of using allocate_box().map_err(OneOf::broaden)?, we could implement Into for any subset of a OneOf so this can be simplified to just allocate_box()?, and the ? operator will implicitly broaden the error.

@spacejam
Copy link
Member

I haven't found a way to support this using the current type system due to the requirement of having an additional Index type which allows the prolog-like type system to resolve correctly. If you or anyone else finds a way to support this with the stable From trait I would be very curious to see how it is possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants