Skip to content

Instantly share code, notes, and snippets.

@daschl daschl/sample.rs
Created Jun 19, 2018

Embed
What would you like to do?
use actix::prelude::*;
use futures::stream::once;
use futures::sync::mpsc;
use message::RawLineMessage;
use std::fs::File;
use std::io;
use std::io::{BufRead, BufReader};
use std::thread;
pub struct FileReader<T: StreamHandler<RawLineMessage, io::Error>> {
target: T,
path: String,
}
impl<T: StreamHandler<RawLineMessage, io::Error>> FileReader<T> {
pub fn new(path: &str, target: T) -> Self {
FileReader {
path: path.into(),
target,
}
}
}
impl<T: StreamHandler<RawLineMessage, io::Error>> Actor for FileReader<T> {
type Context = Context<Self>;
fn started(&mut self, ctx: &mut Self::Context) {
let file = File::open(&self.path).unwrap();
let reader = BufReader::new(file);
let (tx, rx) = mpsc::unbounded();
let child = thread::spawn(move || {
for line in reader.lines() {
tx.unbounded_send(RawLineMessage(line.expect("error decoding line")))
.expect("error doing unbounded send");
}
});
StreamHandler::add_stream(rx, ctx); // <-- this should actuall be T target?
//self.target.add_stream(rx, ctx);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.