Skip to content

Commit

Permalink
fs: less ownership, more borrowing
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Dec 20, 2019
1 parent ba7f91c commit e898bce
Show file tree
Hide file tree
Showing 22 changed files with 50 additions and 37 deletions.
2 changes: 1 addition & 1 deletion tokio/src/fs/create_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ use std::path::Path;
///
/// [std]: https://doc.rust-lang.org/std/fs/fn.create_dir.html
pub async fn create_dir(path: impl AsRef<Path>) -> io::Result<()> {
let path = path.as_ref().to_owned();
let path = path.as_ref();
asyncify(move || std::fs::create_dir(path)).await
}
2 changes: 1 addition & 1 deletion tokio/src/fs/create_dir_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ use std::path::Path;
///
/// [std]: https://doc.rust-lang.org/std/fs/fn.create_dir_all.html
pub async fn create_dir_all(path: impl AsRef<Path>) -> io::Result<()> {
let path = path.as_ref().to_owned();
let path = path.as_ref();
asyncify(move || std::fs::create_dir_all(path)).await
}
14 changes: 7 additions & 7 deletions tokio/src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl File {
/// # }
/// ```
pub async fn open(path: impl AsRef<Path>) -> io::Result<File> {
let path = path.as_ref().to_owned();
let path = path.as_ref();
let std = asyncify(|| sys::File::open(path)).await?;

Ok(File::from_std(std))
Expand Down Expand Up @@ -150,7 +150,7 @@ impl File {
/// # }
/// ```
pub async fn create(path: impl AsRef<Path>) -> io::Result<File> {
let path = path.as_ref().to_owned();
let path = path.as_ref();
let std_file = asyncify(move || sys::File::create(path)).await?;
Ok(File::from_std(std_file))
}
Expand Down Expand Up @@ -254,7 +254,7 @@ impl File {
pub async fn sync_all(&mut self) -> io::Result<()> {
self.complete_inflight().await;

let std = self.std.clone();
let std = &*self.std;
asyncify(move || std.sync_all()).await
}

Expand Down Expand Up @@ -283,7 +283,7 @@ impl File {
pub async fn sync_data(&mut self) -> io::Result<()> {
self.complete_inflight().await;

let std = self.std.clone();
let std = &*self.std;
asyncify(move || std.sync_data()).await
}

Expand Down Expand Up @@ -369,7 +369,7 @@ impl File {
/// # }
/// ```
pub async fn metadata(&self) -> io::Result<Metadata> {
let std = self.std.clone();
let std = &*self.std;
asyncify(move || std.metadata()).await
}

Expand All @@ -389,7 +389,7 @@ impl File {
/// # }
/// ```
pub async fn try_clone(&self) -> io::Result<File> {
let std = self.std.clone();
let std = &*self.std;
let std_file = asyncify(move || std.try_clone()).await?;
Ok(File::from_std(std_file))
}
Expand Down Expand Up @@ -473,7 +473,7 @@ impl File {
/// # }
/// ```
pub async fn set_permissions(&self, perm: Permissions) -> io::Result<()> {
let std = self.std.clone();
let std = &*self.std;
asyncify(move || std.set_permissions(perm)).await
}

Expand Down
4 changes: 2 additions & 2 deletions tokio/src/fs/hard_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use std::path::Path;
///
/// [std]: https://doc.rust-lang.org/std/fs/fn.hard_link.html
pub async fn hard_link(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
let src = src.as_ref().to_owned();
let dst = dst.as_ref().to_owned();
let src = src.as_ref();
let dst = dst.as_ref();

asyncify(move || std::fs::hard_link(src, dst)).await
}
2 changes: 1 addition & 1 deletion tokio/src/fs/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ use std::path::Path;

/// Queries the file system metadata for a path.
pub async fn metadata(path: impl AsRef<Path>) -> io::Result<Metadata> {
let path = path.as_ref().to_owned();
let path = path.as_ref();
asyncify(|| std::fs::metadata(path)).await
}
7 changes: 4 additions & 3 deletions tokio/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ pub use self::write::write;

use std::io;

pub(crate) async fn asyncify<F, T>(f: F) -> io::Result<T>
pub(crate) async fn asyncify<'a, F, T>(f: F) -> io::Result<T>
where
F: FnOnce() -> io::Result<T> + Send + 'static,
F: FnOnce() -> io::Result<T> + Send + Sync + 'a,
T: Send + 'static,
{
match sys::run(f).await {
match unsafe { sys::run_scoped(f) }.await {
Ok(res) => res,
Err(_) => Err(io::Error::new(
io::ErrorKind::Other,
Expand All @@ -99,5 +99,6 @@ mod sys {

// TODO: don't rename
pub(crate) use crate::runtime::spawn_blocking as run;
pub(crate) use crate::runtime::spawn_scoped as run_scoped;
pub(crate) type Blocking<T> = crate::task::JoinHandle<'static, T>;
}
4 changes: 2 additions & 2 deletions tokio/src/fs/open_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ impl OpenOptions {
///
/// [`open`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.open
pub async fn open(&self, path: impl AsRef<Path>) -> io::Result<File> {
let path = path.as_ref().to_owned();
let opts = self.0.clone();
let path = path.as_ref();
let opts = &self.0;

let std = asyncify(move || opts.open(path)).await?;
Ok(File::from_std(std))
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/fs/os/unix/symlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use std::path::Path;
///
/// [std]: https://doc.rust-lang.org/std/os/unix/fs/fn.symlink.html
pub async fn symlink(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
let src = src.as_ref().to_owned();
let dst = dst.as_ref().to_owned();
let src = src.as_ref();
let dst = dst.as_ref();

asyncify(move || std::os::unix::fs::symlink(src, dst)).await
}
4 changes: 2 additions & 2 deletions tokio/src/fs/os/windows/symlink_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use std::path::Path;
///
/// [std]: https://doc.rust-lang.org/std/os/windows/fs/fn.symlink_dir.html
pub async fn symlink_dir(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
let src = src.as_ref().to_owned();
let dst = dst.as_ref().to_owned();
let src = src.as_ref();
let dst = dst.as_ref();

asyncify(move || std::os::windows::fs::symlink_dir(src, dst)).await
}
4 changes: 2 additions & 2 deletions tokio/src/fs/os/windows/symlink_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use std::path::Path;
///
/// [std]: https://doc.rust-lang.org/std/os/windows/fs/fn.symlink_file.html
pub async fn symlink_file(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
let src = src.as_ref().to_owned();
let dst = dst.as_ref().to_owned();
let src = src.as_ref();
let dst = dst.as_ref();

asyncify(move || std::os::windows::fs::symlink_file(src, dst)).await
}
2 changes: 1 addition & 1 deletion tokio/src/fs/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ use std::{io, path::Path};
/// # }
/// ```
pub async fn read(path: impl AsRef<Path>) -> io::Result<Vec<u8>> {
let path = path.as_ref().to_owned();
let path = path.as_ref();
asyncify(move || std::fs::read(path)).await
}
2 changes: 1 addition & 1 deletion tokio/src/fs/read_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ use std::path::{Path, PathBuf};
///
/// [std]: std::fs::read_link
pub async fn read_link(path: impl AsRef<Path>) -> io::Result<PathBuf> {
let path = path.as_ref().to_owned();
let path = path.as_ref();
asyncify(move || std::fs::read_link(path)).await
}
2 changes: 1 addition & 1 deletion tokio/src/fs/read_to_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ use std::{io, path::Path};
/// # }
/// ```
pub async fn read_to_string(path: impl AsRef<Path>) -> io::Result<String> {
let path = path.as_ref().to_owned();
let path = path.as_ref();
asyncify(move || std::fs::read_to_string(path)).await
}
2 changes: 1 addition & 1 deletion tokio/src/fs/remove_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ use std::path::Path;
///
/// This is an async version of [`std::fs::remove_dir`](std::fs::remove_dir)
pub async fn remove_dir(path: impl AsRef<Path>) -> io::Result<()> {
let path = path.as_ref().to_owned();
let path = path.as_ref();
asyncify(move || std::fs::remove_dir(path)).await
}
2 changes: 1 addition & 1 deletion tokio/src/fs/remove_dir_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ use std::path::Path;
///
/// [std]: https://doc.rust-lang.org/std/fs/fn.remove_dir_all.html
pub async fn remove_dir_all(path: impl AsRef<Path>) -> io::Result<()> {
let path = path.as_ref().to_owned();
let path = path.as_ref();
asyncify(move || std::fs::remove_dir_all(path)).await
}
2 changes: 1 addition & 1 deletion tokio/src/fs/remove_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ use std::path::Path;
///
/// [std]: https://doc.rust-lang.org/std/fs/fn.remove_file.html
pub async fn remove_file(path: impl AsRef<Path>) -> io::Result<()> {
let path = path.as_ref().to_owned();
let path = path.as_ref();
asyncify(move || std::fs::remove_file(path)).await
}
4 changes: 2 additions & 2 deletions tokio/src/fs/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use std::path::Path;
///
/// This is an async version of [`std::fs::rename`](std::fs::rename)
pub async fn rename(from: impl AsRef<Path>, to: impl AsRef<Path>) -> io::Result<()> {
let from = from.as_ref().to_owned();
let to = to.as_ref().to_owned();
let from = from.as_ref();
let to = to.as_ref();

asyncify(move || std::fs::rename(from, to)).await
}
2 changes: 1 addition & 1 deletion tokio/src/fs/set_permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ use std::path::Path;
///
/// [std]: https://doc.rust-lang.org/std/fs/fn.set_permissions.html
pub async fn set_permissions(path: impl AsRef<Path>, perm: Permissions) -> io::Result<()> {
let path = path.as_ref().to_owned();
let path = path.as_ref();
asyncify(|| std::fs::set_permissions(path, perm)).await
}
2 changes: 1 addition & 1 deletion tokio/src/fs/symlink_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ use std::path::Path;
///
/// [std]: https://doc.rust-lang.org/std/fs/fn.symlink_metadata.html
pub async fn symlink_metadata(path: impl AsRef<Path>) -> io::Result<Metadata> {
let path = path.as_ref().to_owned();
let path = path.as_ref();
asyncify(|| std::fs::symlink_metadata(path)).await
}
4 changes: 2 additions & 2 deletions tokio/src/fs/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use std::{io, path::Path};
/// # }
/// ```
pub async fn write<C: AsRef<[u8]> + Unpin>(path: impl AsRef<Path>, contents: C) -> io::Result<()> {
let path = path.as_ref().to_owned();
let contents = contents.as_ref().to_owned();
let path = path.as_ref();
let contents = contents.as_ref();

asyncify(move || std::fs::write(path, contents)).await
}
14 changes: 13 additions & 1 deletion tokio/src/runtime/blocking/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,19 @@ where
})
}

/// Run the provided function on an executor dedicated to blocking operations.
/// Run the provided function on an executor dedicated to blocking operations
/// with a custom lifetime scope.
///
/// # Safety
///
/// You must make sure that the returned `JoinHandle` is either awaited to
/// completion, or that its destructor is run before anything it borrows goes
/// out of scope.
///
/// Among other things, this means that `mem::forget`:ing the Handle is _not_
/// safe.
///
/// This can be used safely through the spawn_scoped! macro.
pub(crate) unsafe fn spawn_scoped<'a, F, R>(func: F) -> JoinHandle<'a, R>
where
F: FnOnce() -> R + Send + Sync + 'a,
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/runtime/thread_pool/tests/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ fn queues_2() -> (queue::Worker<Noop>, queue::Worker<Noop>) {
use std::cell::RefCell;
use std::collections::HashMap;
thread_local! {
static TASKS: RefCell<HashMap<u32, task::JoinHandle<u32>>> = RefCell::new(HashMap::new())
static TASKS: RefCell<HashMap<u32, task::JoinHandle<'static, u32>>> = RefCell::new(HashMap::new())
}

fn val(num: u32) -> Task<Noop> {
Expand Down

0 comments on commit e898bce

Please sign in to comment.