diff options
Diffstat (limited to 'tokio-fs/src/file/mod.rs')
-rw-r--r-- | tokio-fs/src/file/mod.rs | 352 |
1 files changed, 153 insertions, 199 deletions
diff --git a/tokio-fs/src/file/mod.rs b/tokio-fs/src/file/mod.rs index 13601b39..c51bf5b5 100644 --- a/tokio-fs/src/file/mod.rs +++ b/tokio-fs/src/file/mod.rs @@ -16,13 +16,11 @@ pub use self::open::OpenFuture; pub use self::open_options::OpenOptions; pub use self::seek::SeekFuture; -use tokio_io::{AsyncRead, AsyncWrite}; - use futures::Poll; - use std::fs::{File as StdFile, Metadata, Permissions}; use std::io::{self, Read, Seek, Write}; use std::path::Path; +use tokio_io::{AsyncRead, AsyncWrite}; /// A reference to an open file on the filesystem. /// @@ -42,39 +40,32 @@ use std::path::Path; /// Create a new file and asynchronously write bytes to it: /// /// ```no_run -/// extern crate tokio; -/// /// use tokio::prelude::{AsyncWrite, Future}; /// -/// fn main() { -/// let task = tokio::fs::File::create("foo.txt") -/// .and_then(|mut file| file.poll_write(b"hello, world!")) -/// .map(|res| { -/// println!("{:?}", res); -/// }).map_err(|err| eprintln!("IO error: {:?}", err)); +/// let task = tokio::fs::File::create("foo.txt") +/// .and_then(|mut file| file.poll_write(b"hello, world!")) +/// .map(|res| { +/// println!("{:?}", res); +/// }).map_err(|err| eprintln!("IO error: {:?}", err)); /// -/// tokio::run(task); -/// } +/// tokio::run(task); /// ``` /// /// Read the contents of a file into a buffer /// /// ```no_run -/// extern crate tokio; -/// /// use tokio::prelude::{AsyncRead, Future}; /// -/// fn main() { -/// let task = tokio::fs::File::open("foo.txt") -/// .and_then(|mut file| { -/// let mut contents = vec![]; -/// file.read_buf(&mut contents) -/// .map(|res| { -/// println!("{:?}", res); -/// }) -/// }).map_err(|err| eprintln!("IO error: {:?}", err)); -/// tokio::run(task); -/// } +/// let task = tokio::fs::File::open("foo.txt") +/// .and_then(|mut file| { +/// let mut contents = vec![]; +/// file.read_buf(&mut contents) +/// .map(|res| { +/// println!("{:?}", res); +/// }) +/// }).map_err(|err| eprintln!("IO error: {:?}", err)); +/// +/// tokio::run(task); /// ``` #[derive(Debug)] pub struct File { @@ -98,18 +89,17 @@ impl File { /// # Examples /// /// ```no_run - /// # extern crate tokio; /// use tokio::prelude::Future; - /// fn main() { - /// let task = tokio::fs::File::open("foo.txt").and_then(|file| { - /// // do something with the file ... - /// file.metadata().map(|md| println!("{:?}", md)) - /// }).map_err(|e| { - /// // handle errors - /// eprintln!("IO error: {:?}", e); - /// }); - /// tokio::run(task); - /// } + /// + /// let task = tokio::fs::File::open("foo.txt").and_then(|file| { + /// // do something with the file ... + /// file.metadata().map(|md| println!("{:?}", md)) + /// }).map_err(|e| { + /// // handle errors + /// eprintln!("IO error: {:?}", e); + /// }); + /// + /// tokio::run(task); /// ``` pub fn open<P>(path: P) -> OpenFuture<P> where @@ -137,19 +127,18 @@ impl File { /// # Examples /// /// ```no_run - /// # extern crate tokio; /// use tokio::prelude::Future; - /// fn main() { - /// let task = tokio::fs::File::create("foo.txt") - /// .and_then(|file| { - /// // do something with the created file ... - /// file.metadata().map(|md| println!("{:?}", md)) - /// }).map_err(|e| { - /// // handle errors - /// eprintln!("IO error: {:?}", e); - /// }); - /// tokio::run(task); - /// } + /// + /// let task = tokio::fs::File::create("foo.txt") + /// .and_then(|file| { + /// // do something with the created file ... + /// file.metadata().map(|md| println!("{:?}", md)) + /// }).map_err(|e| { + /// // handle errors + /// eprintln!("IO error: {:?}", e); + /// }); + /// + /// tokio::run(task); /// ``` pub fn create<P>(path: P) -> CreateFuture<P> where @@ -165,13 +154,10 @@ impl File { /// /// Examples /// ```no_run - /// # extern crate tokio; /// use std::fs::File; /// - /// fn main() { - /// let std_file = File::open("foo.txt").unwrap(); - /// let file = tokio::fs::File::from_std(std_file); - /// } + /// let std_file = File::open("foo.txt").unwrap(); + /// let file = tokio::fs::File::from_std(std_file); /// ``` pub fn from_std(std: StdFile) -> File { File { std: Some(std) } @@ -193,23 +179,20 @@ impl File { /// # Examples /// /// ```no_run - /// # extern crate tokio; /// use tokio::prelude::Future; /// use std::io::SeekFrom; /// - /// fn main() { - /// let task = tokio::fs::File::open("foo.txt") - /// // move cursor 6 bytes from the start of the file - /// .and_then(|mut file| file.poll_seek(SeekFrom::Start(6))) - /// .map(|res| { - /// println!("{:?}", res); - /// }).map_err(|err| eprintln!("IO error: {:?}", err)); + /// let task = tokio::fs::File::open("foo.txt") + /// // move cursor 6 bytes from the start of the file + /// .and_then(|mut file| file.poll_seek(SeekFrom::Start(6))) + /// .map(|res| { + /// println!("{:?}", res); + /// }).map_err(|err| eprintln!("IO error: {:?}", err)); /// - /// tokio::run(task); - /// } + /// tokio::run(task); /// ``` pub fn poll_seek(&mut self, pos: io::SeekFrom) -> Poll<u64, io::Error> { - ::blocking_io(|| self.std().seek(pos)) + crate::blocking_io(|| self.std().seek(pos)) } /// Seek to an offset, in bytes, in a stream. @@ -222,20 +205,17 @@ impl File { /// # Examples /// /// ```no_run - /// # extern crate tokio; /// use tokio::prelude::Future; /// use std::io::SeekFrom; /// - /// fn main() { - /// let task = tokio::fs::File::create("foo.txt") - /// .and_then(|file| file.seek(SeekFrom::Start(6))) - /// .map(|file| { - /// // handle returned file .. - /// # println!("{:?}", file); - /// }).map_err(|err| eprintln!("IO error: {:?}", err)); + /// let task = tokio::fs::File::create("foo.txt") + /// .and_then(|file| file.seek(SeekFrom::Start(6))) + /// .map(|file| { + /// // handle returned file .. + /// # println!("{:?}", file); + /// }).map_err(|err| eprintln!("IO error: {:?}", err)); /// - /// tokio::run(task); - /// } + /// tokio::run(task); /// ``` pub fn seek(self, pos: io::SeekFrom) -> SeekFuture { SeekFuture::new(self, pos) @@ -249,25 +229,22 @@ impl File { /// # Examples /// /// ```no_run - /// # extern crate tokio; /// use tokio::prelude::{AsyncWrite, Future}; /// - /// fn main() { - /// let task = tokio::fs::File::create("foo.txt") - /// .and_then(|mut file| { - /// file.poll_write(b"hello, world!")?; - /// file.poll_sync_all() - /// }) - /// .map(|res| { - /// // handle returned result .. - /// # println!("{:?}", res); - /// }).map_err(|err| eprintln!("IO error: {:?}", err)); - /// - /// tokio::run(task); - /// } + /// let task = tokio::fs::File::create("foo.txt") + /// .and_then(|mut file| { + /// file.poll_write(b"hello, world!")?; + /// file.poll_sync_all() + /// }) + /// .map(|res| { + /// // handle returned result .. + /// # println!("{:?}", res); + /// }).map_err(|err| eprintln!("IO error: {:?}", err)); + /// + /// tokio::run(task); /// ``` pub fn poll_sync_all(&mut self) -> Poll<(), io::Error> { - ::blocking_io(|| self.std().sync_all()) + crate::blocking_io(|| self.std().sync_all()) } /// This function is similar to `poll_sync_all`, except that it may not @@ -282,25 +259,22 @@ impl File { /// # Examples /// /// ```no_run - /// # extern crate tokio; /// use tokio::prelude::{AsyncWrite, Future}; /// - /// fn main() { - /// let task = tokio::fs::File::create("foo.txt") - /// .and_then(|mut file| { - /// file.poll_write(b"hello, world!")?; - /// file.poll_sync_data() - /// }) - /// .map(|res| { - /// // handle returned result .. - /// # println!("{:?}", res); - /// }).map_err(|err| eprintln!("IO error: {:?}", err)); - /// - /// tokio::run(task); - /// } + /// let task = tokio::fs::File::create("foo.txt") + /// .and_then(|mut file| { + /// file.poll_write(b"hello, world!")?; + /// file.poll_sync_data() + /// }) + /// .map(|res| { + /// // handle returned result .. + /// # println!("{:?}", res); + /// }).map_err(|err| eprintln!("IO error: {:?}", err)); + /// + /// tokio::run(task); /// ``` pub fn poll_sync_data(&mut self) -> Poll<(), io::Error> { - ::blocking_io(|| self.std().sync_data()) + crate::blocking_io(|| self.std().sync_data()) } /// Truncates or extends the underlying file, updating the size of this file to become size. @@ -318,24 +292,21 @@ impl File { /// # Examples /// /// ```no_run - /// # extern crate tokio; /// use tokio::prelude::Future; /// - /// fn main() { - /// let task = tokio::fs::File::create("foo.txt") - /// .and_then(|mut file| { - /// file.poll_set_len(10) - /// }) - /// .map(|res| { - /// // handle returned result .. - /// # println!("{:?}", res); - /// }).map_err(|err| eprintln!("IO error: {:?}", err)); - /// - /// tokio::run(task); - /// } + /// let task = tokio::fs::File::create("foo.txt") + /// .and_then(|mut file| { + /// file.poll_set_len(10) + /// }) + /// .map(|res| { + /// // handle returned result .. + /// # println!("{:?}", res); + /// }).map_err(|err| eprintln!("IO error: {:?}", err)); + /// + /// tokio::run(task); /// ``` pub fn poll_set_len(&mut self, size: u64) -> Poll<(), io::Error> { - ::blocking_io(|| self.std().set_len(size)) + crate::blocking_io(|| self.std().set_len(size)) } /// Queries metadata about the underlying file. @@ -343,18 +314,15 @@ impl File { /// # Examples /// /// ```no_run - /// # extern crate tokio; /// use tokio::prelude::Future; /// - /// fn main() { - /// let task = tokio::fs::File::create("foo.txt") - /// .and_then(|file| file.metadata()) - /// .map(|metadata| { - /// println!("{:?}", metadata); - /// }).map_err(|err| eprintln!("IO error: {:?}", err)); + /// let task = tokio::fs::File::create("foo.txt") + /// .and_then(|file| file.metadata()) + /// .map(|metadata| { + /// println!("{:?}", metadata); + /// }).map_err(|err| eprintln!("IO error: {:?}", err)); /// - /// tokio::run(task); - /// } + /// tokio::run(task); /// ``` pub fn metadata(self) -> MetadataFuture { MetadataFuture::new(self) @@ -365,22 +333,19 @@ impl File { /// # Examples /// /// ```no_run - /// # extern crate tokio; /// use tokio::prelude::Future; /// - /// fn main() { - /// let task = tokio::fs::File::create("foo.txt") - /// .and_then(|mut file| file.poll_metadata()) - /// .map(|metadata| { - /// // metadata is of type Async::Ready<Metadata> - /// println!("{:?}", metadata); - /// }).map_err(|err| eprintln!("IO error: {:?}", err)); + /// let task = tokio::fs::File::create("foo.txt") + /// .and_then(|mut file| file.poll_metadata()) + /// .map(|metadata| { + /// // metadata is of type Async::Ready<Metadata> + /// println!("{:?}", metadata); + /// }).map_err(|err| eprintln!("IO error: {:?}", err)); /// - /// tokio::run(task); - /// } + /// tokio::run(task); /// ``` pub fn poll_metadata(&mut self) -> Poll<Metadata, io::Error> { - ::blocking_io(|| self.std().metadata()) + crate::blocking_io(|| self.std().metadata()) } /// Create a new `File` instance that shares the same underlying file handle @@ -390,22 +355,19 @@ impl File { /// # Examples /// /// ```no_run - /// # extern crate tokio; /// use tokio::prelude::Future; /// - /// fn main() { - /// let task = tokio::fs::File::create("foo.txt") - /// .and_then(|mut file| file.poll_try_clone()) - /// .map(|clone| { - /// // do something with the clone - /// # println!("{:?}", clone); - /// }).map_err(|err| eprintln!("IO error: {:?}", err)); + /// let task = tokio::fs::File::create("foo.txt") + /// .and_then(|mut file| file.poll_try_clone()) + /// .map(|clone| { + /// // do something with the clone + /// # println!("{:?}", clone); + /// }).map_err(|err| eprintln!("IO error: {:?}", err)); /// - /// tokio::run(task); - /// } + /// tokio::run(task); /// ``` pub fn poll_try_clone(&mut self) -> Poll<File, io::Error> { - ::blocking_io(|| { + crate::blocking_io(|| { let std = self.std().try_clone()?; Ok(File::from_std(std)) }) @@ -416,28 +378,26 @@ impl File { /// File instances simultaneously. /// /// # Examples + /// /// ```no_run - /// # extern crate tokio; /// use tokio::prelude::Future; /// - /// fn main() { - /// let task = tokio::fs::File::create("foo.txt") - /// .and_then(|file| { - /// file.try_clone() - /// .map(|(file, clone)| { - /// // do something with the file and the clone - /// # println!("{:?} {:?}", file, clone); - /// }) - /// .map_err(|(file, err)| { - /// // you get the original file back if there's an error - /// # println!("{:?}", file); - /// err - /// }) - /// }) - /// .map_err(|err| eprintln!("IO error: {:?}", err)); - /// - /// tokio::run(task); - /// } + /// let task = tokio::fs::File::create("foo.txt") + /// .and_then(|file| { + /// file.try_clone() + /// .map(|(file, clone)| { + /// // do something with the file and the clone + /// # println!("{:?} {:?}", file, clone); + /// }) + /// .map_err(|(file, err)| { + /// // you get the original file back if there's an error + /// # println!("{:?}", file); + /// err + /// }) + /// }) + /// .map_err(|err| eprintln!("IO error: {:?}", err)); + /// + /// tokio::run(task); /// ``` pub fn try_clone(self) -> CloneFuture { CloneFuture::new(self) @@ -462,26 +422,23 @@ impl File { /// # Examples /// /// ```no_run - /// # extern crate tokio; /// use tokio::prelude::Future; /// - /// fn main() { - /// let task = tokio::fs::File::create("foo.txt") - /// .and_then(|file| file.metadata()) - /// .map(|(mut file, metadata)| { - /// let mut perms = metadata.permissions(); - /// perms.set_readonly(true); - /// match file.poll_set_permissions(perms) { - /// Err(e) => eprintln!("{}", e), - /// _ => println!("permissions set!"), - /// } - /// }).map_err(|err| eprintln!("IO error: {:?}", err)); - /// - /// tokio::run(task); - /// } + /// let task = tokio::fs::File::create("foo.txt") + /// .and_then(|file| file.metadata()) + /// .map(|(mut file, metadata)| { + /// let mut perms = metadata.permissions(); + /// perms.set_readonly(true); + /// match file.poll_set_permissions(perms) { + /// Err(e) => eprintln!("{}", e), + /// _ => println!("permissions set!"), + /// } + /// }).map_err(|err| eprintln!("IO error: {:?}", err)); + /// + /// tokio::run(task); /// ``` pub fn poll_set_permissions(&mut self, perm: Permissions) -> Poll<(), io::Error> { - ::blocking_io(|| self.std().set_permissions(perm)) + crate::blocking_io(|| self.std().set_permissions(perm)) } /// Destructures the `tokio_fs::File` into a [`std::fs::File`][std]. @@ -495,19 +452,16 @@ impl File { /// # Examples /// /// ```no_run - /// # extern crate tokio; /// use tokio::prelude::Future; /// - /// fn main() { - /// let task = tokio::fs::File::create("foo.txt") - /// .map(|file| { - /// let std_file = file.into_std(); - /// // do something with the std::fs::File - /// # println!("{:?}", std_file); - /// }).map_err(|err| eprintln!("IO error: {:?}", err)); + /// let task = tokio::fs::File::create("foo.txt") + /// .map(|file| { + /// let std_file = file.into_std(); + /// // do something with the std::fs::File + /// # println!("{:?}", std_file); + /// }).map_err(|err| eprintln!("IO error: {:?}", err)); /// - /// tokio::run(task); - /// } + /// tokio::run(task); /// ``` pub fn into_std(mut self) -> StdFile { self.std.take().expect("`File` instance already shutdown") @@ -520,7 +474,7 @@ impl File { impl Read for File { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { - ::would_block(|| self.std().read(buf)) + crate::would_block(|| self.std().read(buf)) } } @@ -532,17 +486,17 @@ impl AsyncRead for File { impl Write for File { fn write(&mut self, buf: &[u8]) -> io::Result<usize> { - ::would_block(|| self.std().write(buf)) + crate::would_block(|| self.std().write(buf)) } fn flush(&mut self) -> io::Result<()> { - ::would_block(|| self.std().flush()) + crate::would_block(|| self.std().flush()) } } impl AsyncWrite for File { fn shutdown(&mut self) -> Poll<(), io::Error> { - ::blocking_io(|| { + crate::blocking_io(|| { self.std = None; Ok(()) }) |