iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 29
1

Implement I/O seek()

fn seek() {
    let dir = TmpBuilder::new().prefix("tokio-fs-tests").tempdir().unwrap();
    let file_path = dir.path().join("seek.txt");

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

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

    pool.spawn(
        OpenOptions::new()
            .create(true)
            .read(true)
            .write(true)
            .open(file_path)
            .and_then(|file| io::write_all(file, "Hello, world!"))
            .and_then(|(file, _)| file.seek(SeekFrom::End(-6)))
            .and_then(|(file, _)| io::read_exact(file, vec![0; 5]))
            .and_then(|(file, buf)| {
                assert_eq!(buf, b"world");
                file.seek(SeekFrom::Start(0))
            })
            .and_then(|(file, _)| io::read_exact(file, vec![0; 5]))
            .and_then(|(_, buf)| {
                assert_eq!(buf, b"Hello");
                Ok(())
            })
            .then(|r| {
                let _ = r.unwrap();
                tx.send(())
            }),
    );

    rx.wait().unwrap();
}

上一篇
27: I/O handling
下一篇
29: Need More Days to Finish the StellarSQL
系列文
Let's build a DBMS: StellarSQL -- a minimal SQL DBMS written in Rust30

尚未有邦友留言

立即登入留言