Fix tests

This commit is contained in:
asonix 2020-06-19 13:06:46 -05:00
parent 11ff7ca55a
commit d714bfcff6
2 changed files with 40 additions and 25 deletions

View file

@ -139,7 +139,7 @@ where
/// async fn main() -> actix_fs::Result<()> {
/// let file = actix_fs::file::open("foo.txt").await?;
/// let (file, meta) = actix_fs::file::metadata(file).await?;
/// let perms = meta.permissions();
/// let mut perms = meta.permissions();
/// perms.set_readonly(true);
/// actix_fs::file::set_permissions(file, perms).await?;
/// Ok(())
@ -167,10 +167,12 @@ pub async fn set_permissions(file: File, perm: Permissions) -> Result<File, Erro
/// # Examples
///
/// ```no_run
/// use bytes::Bytes;
///
/// #[actix_rt::main]
/// async fn main() -> actix_fs::Result<()> {
/// let f = actix_fs::file::create("foo.txt").await?;
/// let f = actix_fs::file::write(f, b"Hello, world!").await?;
/// let f = actix_fs::file::write(f, Bytes::from_static(&*b"Hello, world!")).await?;
///
/// actix_fs::file::sync_all(f).await?;
/// Ok(())
@ -201,10 +203,12 @@ pub async fn sync_all(file: File) -> Result<File, Error> {
/// # Examples
///
/// ```no_run
/// use bytes::Bytes;
///
/// #[actix_rt::main]
/// async fn main() -> actix_fs::Result<()> {
/// let f = actix_fs::file::create("foo.txt").await?;
/// let f = actix_fs::file::write_all(f, b"Hello, world!").await?;
/// let f = actix_fs::file::write(f, Bytes::from_static(&*b"Hello, world!")).await?;
///
/// actix_fs::file::sync_data(f).await?;
/// Ok(())
@ -275,6 +279,7 @@ pub async fn set_len(file: File, size: u64) -> Result<File, Error> {
/// async fn main() -> actix_fs::Result<()> {
/// let f = actix_fs::file::open("foo.txt").await?;
/// let (f, pos) = actix_fs::file::seek(f, std::io::SeekFrom::Start(8)).await?;
/// Ok(())
/// }
/// ```
pub async fn seek(mut file: File, seek: io::SeekFrom) -> Result<(File, u64), Error> {
@ -314,7 +319,7 @@ pub async fn seek(mut file: File, seek: io::SeekFrom) -> Result<(File, u64), Err
///
/// let bytes = actix_fs::file::read(f).await?;
///
/// println!("The bytes: {:?}", bytes;
/// println!("The bytes: {:?}", bytes);
/// Ok(())
/// }
/// ```
@ -332,6 +337,8 @@ pub async fn read(file: File) -> Result<Bytes, Error> {
/// Produce a new stream of bytes from this File
///
/// ```no_run
/// use futures::stream::StreamExt;
///
/// #[actix_rt::main]
/// async fn main() -> actix_fs::Result<()> {
/// let f = actix_fs::file::open("foo.txt").await?;
@ -409,15 +416,17 @@ pub async fn read_to_string(file: File) -> Result<String, Error> {
/// # Examples
///
/// ```no_run
/// use bytes::Bytes;
///
/// #[actix_rt::main]
/// async fn main() -> actix_fs::Result<()> {
/// let file = actix_fs::file::create("foo.txt").await?;
///
/// actix_fs::file::write(file, b"some bytes").await?;
/// actix_fs::file::write(file, Bytes::from_static(&*b"some bytes")).await?;
/// Ok(())
/// }
/// ```
pub async fn write<B>(file: File, bytes: B) -> Result<(), Error>
pub async fn write<B>(file: File, bytes: B) -> Result<File, Error>
where
B: Into<Bytes>,
{
@ -425,7 +434,7 @@ where
sink.send(bytes.into()).await?;
sink.close().await?;
Ok(())
Ok(sink.file.take().expect("Panick in write future"))
}
/// Write a stream of bytes into this writer.
@ -450,11 +459,11 @@ where
/// let file = actix_fs::file::create("foo.txt").await?;
/// let stream = actix_fs::read_to_stream("bar.txt").await?;
///
/// actix_fs::file::write(file, stream).await?;
/// actix_fs::file::write_stream(file, stream).await?;
/// Ok(())
/// }
/// ```
pub async fn write_stream<S, E>(file: File, mut stream: S) -> Result<(), E>
pub async fn write_stream<S, E>(file: File, mut stream: S) -> Result<File, E>
where
S: Stream<Item = Result<Bytes, E>> + Unpin,
E: From<Error> + Unpin,
@ -463,7 +472,7 @@ where
sink.send_all(&mut stream).await?;
sink.close().await?;
Ok(())
Ok(sink.file.take().expect("Panick in write future"))
}
impl FileStream {
@ -485,10 +494,13 @@ impl FileStream {
/// This can be used to only read certain bytes from a given file
///
/// ```no_run
/// use futures::stream::StreamExt;
/// use std::io::SeekFrom;
///
/// #[actix_rt::main]
/// async fn main() -> actix_fs::Result<()> {
/// let f = actix_fs::file::open("foo.txt").await?;
/// let (f, _) = actix_fs::file::seek(std::io::SeekFrom::Start(16)).await?;
/// let (f, _) = actix_fs::file::seek(f, SeekFrom::Start(16)).await?;
/// let mut stream = actix_fs::file::read_to_stream(f).await?.chunk_size(16);
///
/// if let Some(res) = stream.next().await {
@ -681,7 +693,7 @@ mod tests {
let bytes_to_be_written = b"abcdefg";
run(async move {
let file = create(TEST_FILE).await?;
write(file, bytes_to_be_written.to_vec().into()).await?;
write(file, bytes_to_be_written.to_vec()).await?;
let file = open(TEST_FILE).await?;
let bytes = read(file).await?;

View file

@ -9,7 +9,7 @@
//! use std::io::SeekFrom;
//!
//! #[actix_rt::main]
//! async fn main() -> Result<()> {
//! async fn main() -> actix_fs::Result<()> {
//! let file = actix_fs::file::open("tests/read.txt").await?;
//! let (file, position) = actix_fs::file::seek(file, SeekFrom::Start(7)).await?;
//! let bytes = actix_fs::file::read(file).await?;
@ -136,10 +136,10 @@ where
/// the length of the `to` file as reported by `metadata`.
///
/// If youre wanting to copy the contents of one file to another and youre
/// working with [`File`]s, see the [`io::copy`] function.
/// working with [`file`]s, see the [`io::copy`] function.
///
/// [`io::copy`]: ../io/fn.copy.html
/// [`File`]: ./struct.File.html
/// [`file`]: ./struct.File.html
///
/// # Platform-specific behavior
///
@ -168,7 +168,7 @@ where
///
/// ```no_run
/// #[actix_rt::main]
/// fn main() -> actix_fs::Result<()> {
/// async fn main() -> actix_fs::Result<()> {
/// actix_fs::copy("foo.txt", "bar.txt").await?; // Copy foo.txt to bar.txt
/// Ok(())
/// }
@ -357,7 +357,7 @@ where
/// Read the entire contents of a file into a bytes vector.
///
/// This is a convenience function for using [`File::open`] and [`read_to_end`]
/// This is a convenience function for using [`file::open`] and [`read_to_end`]
/// with fewer imports and without an intermediate variable. It pre-allocates a
/// buffer based on the file size when available, so it is generally faster than
/// reading into a vector created with `Vec::new()`.
@ -453,6 +453,7 @@ where
///
/// ```no_run
/// use std::net::SocketAddr;
/// use futures::stream::StreamExt;
///
/// #[actix_rt::main]
/// async fn main() -> actix_fs::Result<()> {
@ -475,7 +476,7 @@ where
/// Read the entire contents of a file into a string.
///
/// This is a convenience function for using [`File::open`] and [`read_to_string`]
/// This is a convenience function for using [`file::open`] and [`read_to_string`]
/// with fewer imports and without an intermediate variable. It pre-allocates a
/// buffer based on the file size when available, so it is generally faster than
/// reading into a string created with `String::new()`.
@ -759,7 +760,7 @@ where
/// This function will create a file if it does not exist,
/// and will entirely replace its contents if it does.
///
/// This is a convenience function for using [`File::create`] and [`write_all`]
/// This is a convenience function for using [`file::create`] and [`write_all`]
/// with fewer imports.
///
/// [`file::create`]: struct.File.html#method.create
@ -768,14 +769,16 @@ where
/// # Examples
///
/// ```no_run
/// use bytes::Bytes;
///
/// #[actix_rt::main]
/// async fn main() -> actix_fs::Result<()> {
/// actix_fs::write("foo.txt", b"Lorem ipsum").await?;
/// actix_fs::write("foo.txt", Bytes::from_static(&*b"Lorem ipsum")).await?;
/// actix_fs::write("bar.txt", "dolor sit").await?;
/// Ok(())
/// }
/// ```
pub async fn write<P, B>(path: P, contents: B) -> Result<()>
pub async fn write<P, B>(path: P, contents: B) -> Result<fs::File>
where
P: AsRef<Path> + Send + 'static,
B: Into<Bytes>,
@ -790,7 +793,7 @@ where
/// This function will create a file if it does not exist,
/// and will entirely replace its contents if it does.
///
/// This is a convenience function for using [`File::create`] and [`write_all`]
/// This is a convenience function for using [`file::create`] and [`write_all`]
/// with fewer imports.
///
/// [`file::create`]: struct.File.html#method.create
@ -807,13 +810,13 @@ where
/// Ok(())
/// }
/// ```
pub async fn write_stream<P, S, E>(path: P, stream: S) -> Result<()>
pub async fn write_stream<P, S, E>(path: P, stream: S) -> std::result::Result<fs::File, E>
where
P: AsRef<Path> + Send + 'static,
S: Stream<Item = Result<Bytes>> + Unpin,
S: Stream<Item = std::result::Result<Bytes, E>> + Unpin,
E: From<Error> + Unpin,
{
let f = file::create(path).await?;
file::write_stream(f, stream).await
file::write_stream::<S, E>(f, stream).await
}