iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 28
1

準備期中考 焦頭爛額,先貼上不會動的 code

use tokio_fs;
use tokio_io::io;
use tokio_threadpool;

use futures::Future;
use futures::future::poll_fn;
use futures::sync::oneshot;
use rand::{thread_rng, Rng};

use std::fs::File as StdFile;
use std::io::{Read, SeekFrom};

fn write() {
    const NUM_CHARS: usize = 16 * 1_024;

    let file_path = dir.path().join("read_write.txt");

    let contents: Vec<u8> = thread_rng().gen_ascii_chars()
        .take(NUM_CHARS)
        .collect::<String>()
        .into();

    let pool = Builder::new()
        .pool_size(1)
        .build();

    let (tx, rx) = oneshot::channel();

    pool.spawn({
        let file_path = file_path.clone();
        let contents = contents.clone();

        File::create(file_path)
            .and_then(|file| file.metadata())
            .inspect(|&(_, ref metadata)| assert!(metadata.is_file()))
            .and_then(move |(file, _)| io::write_all(file, contents))
            .and_then(|(mut file, _)| {
                poll_fn(move || file.poll_sync_all())
            })
            .then(|res| {
                let _ = res.unwrap();
                tx.send(()).unwrap();
                Ok(())
            })
    });

    rx.wait().unwrap();
}

上一篇
26: Grammer `Create`
下一篇
28: I/O seek
系列文
Let's build a DBMS: StellarSQL -- a minimal SQL DBMS written in Rust30

1 則留言

0
Claire Chang
iT邦新手 5 級 ‧ 2018-11-11 22:35:53

終於是中文了!

之後會換成英文 XD

我要留言

立即登入留言