强制 io::stdio 和 BufReader 之间的 BufRead 特性兼容性 [英] Forcing BufRead trait compatibility between io::stdio and BufReader

查看:21
本文介绍了强制 io::stdio 和 BufReader 之间的 BufRead 特性兼容性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试键入一个 io::stdio 实例和一个包装文件的 BufReader ,以便我可以编写假设缓冲输入源的代码.我尝试了几种尝试将 io::stdio() 转换为 BufRead 类型的变体,但都失败了:

I'm trying to type-pun an io::stdio instance and a BufReader wrapping a file so that I can write code assuming a buffered input source. I've tried several variations of trying to cast an io::stdio() to a BufRead type and all have failed with one or more variations of:

error: mismatched types:
 expected `std::io::stdio::StdinLock<'_>`,
    found `std::io::buffered::BufReader<std::fs::File>`
(expected struct `std::io::stdio::StdinLock`,
    found struct `std::io::buffered::BufReader`) [E0308]
csv_to_json.rs:26     reader = BufReader::new(file.unwrap());

代码如下:

use std::io;
use std::io::BufReader;
use std::io::prelude::*;
use std::env;
use std::process::exit;
use std::fs::File;

fn usage() {
    println!("Usage: cat input.csv | csv_to_json");
}

fn main() {
    let stdin = io::stdin();
    let mut reader = stdin.lock();   // Assignment. (1)
    let args: Vec<String> = env::args().collect();

    if args.len() > 1 {
        usage();
        exit(1);
    } else if args.len() == 1 {
        let file = File::open(args[0].clone());
        if !file.is_ok() {
            usage();
            exit(1);
        }
        reader = BufReader::new(file.unwrap());  // Reassignment. This is the line that fails.
    }

    // Rest of the code works with reader assuming a buffered input source.
}

我尝试过的事情:

let mut reader : BufRead = io::stdin();  // Fails.
let mut reader : BufReader<Read> = io::stdin();  // Fails.
let mut reader : BufReader<_> = io::stdin(); // Fails
let mut reader : BufRead = io::stdin().lock(); // Fails later.
let mut reader = BufReader::new(io::stdin());  // Fails with type mismatch.
let mut reader = BufReader::new(io::stdin().lock());  // Fails with type mismatch.

我什至尝试将该代码提取到它自己的函数中,但返回类型签名因类似的错误条件而失败.如何创建一个阅​​读器"变量,它是 stdio 上的 BufferedReaderFile 上的 BufferedReader到处都面临类型错误?

I've even tried extracting that code to its own function and the return type signature fails with similar error conditions. How can I create a "reader" variable that's either a BufferedReader over stdio or a BufferedReader over a File without facing type errors everywhere?

推荐答案

这会起作用.

use std::io;
use std::io::BufReader;
use std::io::prelude::*;
use std::env;
use std::process::exit;
use std::fs::File;

fn usage() {
    println!("Usage: cat input.csv | csv_to_json");
}

fn main() {
    let stdin = io::stdin();
    let mut reader = Box::new(stdin.lock()) as Box<BufRead>;
    let args: Vec<String> = env::args().collect();

    if args.len() > 1 {
        usage();
        exit(1);
    } else if args.len() == 1 {
        let file = File::open(args[0].clone());
        if !file.is_ok() {
            usage();
            exit(1);
        }
        reader = Box::new(BufReader::new(file.unwrap()));
    }
}

这篇关于强制 io::stdio 和 BufReader 之间的 BufRead 特性兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆