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

std::error::Error is not implemented for input_stream::Error #1

Open
vadimcn opened this issue May 24, 2018 · 1 comment
Open

std::error::Error is not implemented for input_stream::Error #1

vadimcn opened this issue May 24, 2018 · 1 comment

Comments

@vadimcn
Copy link

vadimcn commented May 24, 2018

... so it isn't usable with the ? operator.

@adrian-budau
Copy link
Owner

the ? operator has nothing to do with the std::error::Error. I'm guessing you are trying to use error-chain with this library. As far as i know error-chain is being deprecated so it would be nice to migrate to failure.

If you want to obtain a std::error::Error you can call compat() on any error you obtain from this library (one of the benefits of the failure crate is this compatibility with other error handling crates).

For example to use with error-chain

extern crate input_stream;
#[macro_use]
extern crate error_chain;
extern crate failure;

use std::io::stdin;
use input_stream::InputStream;
use failure::ResultExt as FailureResultExt;

error_chain!{}

fn run() -> Result<()> {
    let stdin = stdin();
    let mut input = InputStream::new(stdin.lock());
    let a: usize = input.scan().compat().chain_err(|| "Error in input-stream")?;
    let b: usize = input.scan().compat().chain_err(|| "Error in input-stream")?;
    println!("Hello sum: {}", a + b);
    Ok(())
}

fn main() {
    run().unwrap();
}

or if I guessed wrong and you were using only Box<Error> compat() should still do the trick:

extern crate input_stream;
extern crate failure;

use std::error::Error;
use std::io::stdin;
use input_stream::InputStream;
use failure::ResultExt as FailureResultExt;

fn run() -> Result<(), Box<Error>> {
    let stdin = stdin();
    let mut input = InputStream::new(stdin.lock());
    let a: usize = input.scan().compat()?;
    let b: usize = input.scan().compat()?;
    println!("Hello sum: {}", a + b);
    Ok(())
}

fn main() {
    run().unwrap();
}

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