Skip to content

Create an async SAM reader from stdin or file #284

Answered by zaeleus
mbhall88 asked this question in Q&A
Discussion options

You must be logged in to vote

I can't seem to get this working to create an AsyncReader from either stdin or file.

All branches of an if expression must return the same type. This is hinted by the error message "if and else have incompatible types", i.e., the inner readers tokio::io::Stdin and tokio::fs::File are of two different types. Given both readers implement AsyncBufRead, you can cast them to boxed trait objects to return a homogeneous type. (Most tokio I/O extension operations do not assume the reader is address-sensitive, so we also require Unpin.)

let reader: Box<dyn AsyncBufRead + Unpin> = if src == "-" {
    Box::new(BufReader::new(io::stdin()))
} else {
    File::open(src).await.map(BufReader::new).map(Box

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@mbhall88
Comment options

Answer selected by mbhall88
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants