From e0855ff4a13ea79daa56d23a25f827fe86e8147b Mon Sep 17 00:00:00 2001 From: Diggory Blake Date: Sun, 24 Dec 2017 17:02:24 +0000 Subject: Implement AsRef for Component --- src/libstd/path.rs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src/libstd') diff --git a/src/libstd/path.rs b/src/libstd/path.rs index eb125a4737a..ff528903b75 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -576,6 +576,13 @@ impl<'a> AsRef for Component<'a> { } } +#[stable(feature = "path_component_asref", since = "1.24.0")] +impl<'a> AsRef for Component<'a> { + fn as_ref(&self) -> &Path { + self.as_os_str().as_ref() + } +} + /// An iterator over the [`Component`]s of a [`Path`]. /// /// This `struct` is created by the [`components`] method on [`Path`]. -- cgit 1.4.1-3-g733a5 From d0d5db65c79ee79dea761be81fd8bd6847366e3d Mon Sep 17 00:00:00 2001 From: Bulat Musin <9249387+bmusin@users.noreply.github.com> Date: Wed, 10 Jan 2018 08:03:10 +0300 Subject: fix typo rwlock.rs Hi. Fixed typo: contained -> content --- src/libstd/sync/rwlock.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/sync/rwlock.rs b/src/libstd/sync/rwlock.rs index 0f3f4e50f7e..2edf02efc47 100644 --- a/src/libstd/sync/rwlock.rs +++ b/src/libstd/sync/rwlock.rs @@ -36,7 +36,7 @@ use sys_common::rwlock as sys; /// required that `T` satisfies [`Send`] to be shared across threads and /// [`Sync`] to allow concurrent access through readers. The RAII guards /// returned from the locking methods implement [`Deref`][] (and [`DerefMut`] -/// for the `write` methods) to allow access to the contained of the lock. +/// for the `write` methods) to allow access to the content of the lock. /// /// # Poisoning /// -- cgit 1.4.1-3-g733a5 From 44912bf77bf1fd2137e5b7d1d31b2e59e2819136 Mon Sep 17 00:00:00 2001 From: Matt Brubeck Date: Wed, 10 Jan 2018 08:06:23 -0800 Subject: Pre-allocate in fs::read and fs::read_string --- src/libstd/fs.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs index f40aed2478a..51cb9609120 100644 --- a/src/libstd/fs.rs +++ b/src/libstd/fs.rs @@ -211,6 +211,14 @@ pub struct DirBuilder { recursive: bool, } +/// How large a buffer to pre-allocate before reading the entire file at `path`. +fn initial_buffer_size>(path: P) -> usize { + // Allocate one extra byte so the buffer doesn't need to grow before the + // final `read` call at the end of the file. Don't worry about `usize` + // overflow because reading will fail regardless in that case. + metadata(path).map(|m| m.len() as usize + 1).unwrap_or(0) +} + /// Read the entire contents of a file into a bytes vector. /// /// This is a convenience function for using [`File::open`] and [`read_to_end`] @@ -246,7 +254,7 @@ pub struct DirBuilder { /// ``` #[unstable(feature = "fs_read_write", issue = "46588")] pub fn read>(path: P) -> io::Result> { - let mut bytes = Vec::new(); + let mut bytes = Vec::with_capacity(initial_buffer_size(&path)); File::open(path)?.read_to_end(&mut bytes)?; Ok(bytes) } @@ -287,7 +295,7 @@ pub fn read>(path: P) -> io::Result> { /// ``` #[unstable(feature = "fs_read_write", issue = "46588")] pub fn read_string>(path: P) -> io::Result { - let mut string = String::new(); + let mut string = String::with_capacity(initial_buffer_size(&path)); File::open(path)?.read_to_string(&mut string)?; Ok(string) } -- cgit 1.4.1-3-g733a5 From 9d9504a27c240f3e04bec95e382f8130282cca36 Mon Sep 17 00:00:00 2001 From: Alexis Hunt Date: Wed, 10 Jan 2018 17:58:00 -0500 Subject: Fix typo. --- src/libstd/ffi/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/ffi/mod.rs b/src/libstd/ffi/mod.rs index a75596351e4..a37a5e8ae82 100644 --- a/src/libstd/ffi/mod.rs +++ b/src/libstd/ffi/mod.rs @@ -53,7 +53,7 @@ //! terminator, so the buffer length is really `len+1` characters. //! Rust strings don't have a nul terminator; their length is always //! stored and does not need to be calculated. While in Rust -//! accessing a string's length is a O(1) operation (becasue the +//! accessing a string's length is a O(1) operation (because the //! length is stored); in C it is an O(length) operation because the //! length needs to be computed by scanning the string for the nul //! terminator. -- cgit 1.4.1-3-g733a5 From fc825f09e87c11413ae1e6a28957df055eb92b96 Mon Sep 17 00:00:00 2001 From: muvlon Date: Thu, 11 Jan 2018 17:20:16 +0100 Subject: Fix confusing documentation for OsStr --- src/libstd/ffi/os_str.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index 109173d31c5..0d07c01a105 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -84,7 +84,7 @@ pub struct OsString { /// This type represents a borrowed reference to a string in the operating system's preferred /// representation. /// -/// `OsStr` is to [`OsString`] as [`String`] is to [`&str`]: the former in each pair are borrowed +/// `OsStr` is to [`OsString`] as [`&str`] is to [`String`]: the former in each pair are borrowed /// references; the latter are owned strings. /// /// See the [module's toplevel documentation about conversions][conversions] for a discussion on -- cgit 1.4.1-3-g733a5 From bac725ecdf31fe8ac5e7ddcd45d2c316d9aff674 Mon Sep 17 00:00:00 2001 From: muvlon Date: Thu, 11 Jan 2018 17:20:30 +0100 Subject: Fix dead links in docs for OsStr --- src/libstd/ffi/os_str.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/libstd') diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index 0d07c01a105..91e3855f536 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -91,6 +91,8 @@ pub struct OsString { /// the traits which `OsStr` implements for conversions from/to native representations. /// /// [`OsString`]: struct.OsString.html +/// [`&str`]: ../primitive.str.html +/// [`String`]: ../string/struct.String.html /// [conversions]: index.html#conversions #[stable(feature = "rust1", since = "1.0.0")] pub struct OsStr { -- cgit 1.4.1-3-g733a5 From 80028760ed111b800decee876af59741f8765cce Mon Sep 17 00:00:00 2001 From: muvlon Date: Thu, 11 Jan 2018 22:01:32 +0100 Subject: s/OsStr/&OsStr in docs to align with &str/String comparison --- src/libstd/ffi/os_str.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index 91e3855f536..3959e8533be 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -36,7 +36,7 @@ use sys_common::{AsInner, IntoInner, FromInner}; /// and platform-native string values, and in particular allowing a Rust string /// to be converted into an "OS" string with no cost if possible. /// -/// `OsString` is to [`OsStr`] as [`String`] is to [`&str`]: the former +/// `OsString` is to [`&OsStr`] as [`String`] is to [`&str`]: the former /// in each pair are owned strings; the latter are borrowed /// references. /// @@ -64,6 +64,7 @@ use sys_common::{AsInner, IntoInner, FromInner}; /// the traits which `OsString` implements for conversions from/to native representations. /// /// [`OsStr`]: struct.OsStr.html +/// [`&OsStr`]: struct.OsStr.html /// [`From`]: ../convert/trait.From.html /// [`String`]: ../string/struct.String.html /// [`&str`]: ../primitive.str.html @@ -84,7 +85,7 @@ pub struct OsString { /// This type represents a borrowed reference to a string in the operating system's preferred /// representation. /// -/// `OsStr` is to [`OsString`] as [`&str`] is to [`String`]: the former in each pair are borrowed +/// `&OsStr` is to [`OsString`] as [`&str`] is to [`String`]: the former in each pair are borrowed /// references; the latter are owned strings. /// /// See the [module's toplevel documentation about conversions][conversions] for a discussion on -- cgit 1.4.1-3-g733a5 From 7cad7f6b3dbbd1a831cf86b149474e083f2727ba Mon Sep 17 00:00:00 2001 From: Neil Shen Date: Fri, 12 Jan 2018 13:38:24 +0800 Subject: Fix Duration::subsec_millis and Duration::subsec_micros examples --- src/libstd/time/duration.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/time/duration.rs b/src/libstd/time/duration.rs index 15ddb62bab5..cb5bfb9176e 100644 --- a/src/libstd/time/duration.rs +++ b/src/libstd/time/duration.rs @@ -206,7 +206,7 @@ impl Duration { /// /// let duration = Duration::from_millis(5432); /// assert_eq!(duration.as_secs(), 5); - /// assert_eq!(duration.subsec_nanos(), 432_000_000); + /// assert_eq!(duration.subsec_millis(), 432); /// ``` #[unstable(feature = "duration_extras", issue = "46507")] #[inline] @@ -226,7 +226,7 @@ impl Duration { /// /// let duration = Duration::from_micros(1_234_567); /// assert_eq!(duration.as_secs(), 1); - /// assert_eq!(duration.subsec_nanos(), 234_567_000); + /// assert_eq!(duration.subsec_micros(), 234_567); /// ``` #[unstable(feature = "duration_extras", issue = "46507")] #[inline] -- cgit 1.4.1-3-g733a5