How to get tokio-io's async_read for a File handle

大城市里の小女人 提交于 2020-01-04 01:53:09

问题


I want to stream lines out of a file handle and I do not know how to satisfy the trait bound that a File has async_read:

use std::fs::File;
use std::io::{ BufReader, BufRead };
use tokio_core::reactor::Handle;
use tokio_io::io::lines;
use tokio_io::AsyncRead;

struct DockerLog {
    path: String
}

impl DockerLog {
    pub fn new(path: String) -> DockerLog {
        DockerLog {
            path: path
        }
    }
    pub fn read_lines(&self, handle: &Handle) {
        let file : File = File::open(&self.path[..]).unwrap();
        let l = lines(BufReader::new(file));

    }
}

The error:

error[E0277]: the trait bound `std::fs::File: tokio_io::AsyncRead` is not satisfied
  --> src/container/docker_logs.rs:20:17
   |
20 |         let l = lines(BufReader::new(file));
   |                 ^^^^^ the trait `tokio_io::AsyncRead` is not implemented for `std::fs::File`
   |
   = note: required because of the requirements on the impl of `tokio_io::AsyncRead` for `std::io::BufReader<std::fs::File>`
   = note: required by `tokio_io::io::lines`

Looking over the AsyncRead, it seems like a File doesn't mark that it has non-blocking reads. Do I need to downgrade the File to a raw_fd and then do something there?

来源:https://stackoverflow.com/questions/44313906/how-to-get-tokio-ios-async-read-for-a-file-handle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!