2019-05-14 10:27:36 -07:00
|
|
|
use crate::{file, File};
|
2019-07-11 11:05:49 -05:00
|
|
|
use std::future::Future;
|
|
|
|
|
use std::pin::Pin;
|
|
|
|
|
use std::task::Context;
|
|
|
|
|
use std::task::Poll;
|
2019-02-21 11:56:15 -08:00
|
|
|
use std::{fmt, io, mem, path::Path};
|
2019-02-20 23:38:49 +01:00
|
|
|
use tokio_io;
|
2019-07-11 11:05:49 -05:00
|
|
|
use tokio_io::AsyncWrite;
|
2019-02-20 23:38:49 +01:00
|
|
|
|
|
|
|
|
/// Creates a future that will open a file for writing and write the entire
|
|
|
|
|
/// contents of `contents` to it.
|
|
|
|
|
///
|
|
|
|
|
/// This is the async equivalent of `std::fs::write`.
|
|
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
|
|
|
|
/// use tokio::prelude::Future;
|
2019-05-14 10:27:36 -07:00
|
|
|
///
|
|
|
|
|
/// let buffer = b"Hello world!";
|
|
|
|
|
/// let task = tokio::fs::write("foo.txt", buffer).map(|data| {
|
|
|
|
|
/// // `data` has now been written to foo.txt. The buffer is being
|
|
|
|
|
/// // returned so it can be used for other things.
|
|
|
|
|
/// println!("foo.txt now had {} bytes written to it", data.len());
|
|
|
|
|
/// }).map_err(|e| {
|
|
|
|
|
/// // handle errors
|
|
|
|
|
/// eprintln!("IO error: {:?}", e);
|
|
|
|
|
/// });
|
|
|
|
|
///
|
|
|
|
|
/// tokio::run(task);
|
2019-02-20 23:38:49 +01:00
|
|
|
/// ```
|
2019-07-11 11:05:49 -05:00
|
|
|
pub fn write<P, C: AsRef<[u8]> + Unpin>(path: P, contents: C) -> WriteFile<P, C>
|
2019-02-20 23:38:49 +01:00
|
|
|
where
|
2019-07-11 11:05:49 -05:00
|
|
|
P: AsRef<Path> + Send + Unpin + 'static,
|
2019-02-20 23:38:49 +01:00
|
|
|
{
|
|
|
|
|
WriteFile {
|
|
|
|
|
state: State::Create(File::create(path), Some(contents)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A future used to open a file for writing and write the entire contents
|
|
|
|
|
/// of some data to it.
|
|
|
|
|
#[derive(Debug)]
|
2019-07-16 05:28:56 +09:00
|
|
|
#[must_use = "futures do nothing unless you `.await` or poll them"]
|
2019-07-11 11:05:49 -05:00
|
|
|
pub struct WriteFile<P: AsRef<Path> + Send + Unpin + 'static, C: AsRef<[u8]> + Unpin> {
|
2019-02-20 23:38:49 +01:00
|
|
|
state: State<P, C>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2019-07-11 11:05:49 -05:00
|
|
|
enum State<P: AsRef<Path> + Send + Unpin + 'static, C: AsRef<[u8]> + Unpin> {
|
2019-02-20 23:38:49 +01:00
|
|
|
Create(file::CreateFuture<P>, Option<C>),
|
2019-07-11 11:05:49 -05:00
|
|
|
Writing { f: File, buf: C, pos: usize },
|
|
|
|
|
Empty,
|
2019-02-20 23:38:49 +01:00
|
|
|
}
|
|
|
|
|
|
2019-07-11 11:05:49 -05:00
|
|
|
fn zero_write() -> io::Error {
|
|
|
|
|
io::Error::new(io::ErrorKind::WriteZero, "zero-length write")
|
|
|
|
|
}
|
2019-02-20 23:38:49 +01:00
|
|
|
|
2019-07-11 11:05:49 -05:00
|
|
|
impl<P: AsRef<Path> + Send + Unpin + 'static, C: AsRef<[u8]> + Unpin + fmt::Debug> Future
|
|
|
|
|
for WriteFile<P, C>
|
|
|
|
|
{
|
|
|
|
|
type Output = io::Result<C>;
|
|
|
|
|
|
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
|
let inner = Pin::get_mut(self);
|
|
|
|
|
match &mut inner.state {
|
|
|
|
|
State::Create(create_file, contents) => {
|
|
|
|
|
let file = ready!(Pin::new(create_file).poll(cx))?;
|
|
|
|
|
let contents = contents.take().unwrap();
|
|
|
|
|
let new_state = State::Writing {
|
|
|
|
|
f: file,
|
|
|
|
|
buf: contents,
|
|
|
|
|
pos: 0,
|
|
|
|
|
};
|
|
|
|
|
mem::replace(&mut inner.state, new_state);
|
|
|
|
|
// We just entered the Write state, need to poll it before returning.
|
|
|
|
|
return Pin::new(inner).poll(cx);
|
2019-02-20 23:38:49 +01:00
|
|
|
}
|
2019-07-11 11:05:49 -05:00
|
|
|
State::Empty => panic!("poll a WriteFile after it's done"),
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
2019-02-20 23:38:49 +01:00
|
|
|
|
2019-07-11 11:05:49 -05:00
|
|
|
match mem::replace(&mut inner.state, State::Empty) {
|
|
|
|
|
State::Writing {
|
|
|
|
|
mut f,
|
|
|
|
|
buf,
|
|
|
|
|
mut pos,
|
|
|
|
|
} => {
|
|
|
|
|
let buf_ref = buf.as_ref();
|
|
|
|
|
while pos < buf_ref.len() {
|
|
|
|
|
let n = ready!(Pin::new(&mut f).poll_write(cx, &buf_ref[pos..]))?;
|
|
|
|
|
pos += n;
|
|
|
|
|
if n == 0 {
|
|
|
|
|
return Poll::Ready(Err(zero_write()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Poll::Ready(Ok(buf))
|
|
|
|
|
}
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
}
|
2019-02-20 23:38:49 +01:00
|
|
|
}
|
|
|
|
|
}
|