From e995fa8aeaa4bfe92d270ddb9e1dd8b5516eaa25 Mon Sep 17 00:00:00 2001 From: Andy Russell Date: Thu, 28 Mar 2019 20:45:40 -0400 Subject: implement `AsRawFd` for stdio locks --- src/libstd/sys/redox/ext/io.rs | 15 +++++++++++++++ src/libstd/sys/unix/ext/io.rs | 15 +++++++++++++++ src/libstd/sys/windows/ext/io.rs | 21 +++++++++++++++++++++ 3 files changed, 51 insertions(+) (limited to 'src/libstd') diff --git a/src/libstd/sys/redox/ext/io.rs b/src/libstd/sys/redox/ext/io.rs index f431f96c541..c21d216478f 100644 --- a/src/libstd/sys/redox/ext/io.rs +++ b/src/libstd/sys/redox/ext/io.rs @@ -115,6 +115,21 @@ impl AsRawFd for io::Stderr { fn as_raw_fd(&self) -> RawFd { 2 } } +#[stable(feature = "asraw_stdio_locks", since = "1.35.0")] +impl<'a> AsRawFd for io::StdinLock<'a> { + fn as_raw_fd(&self) -> RawFd { 0 } +} + +#[stable(feature = "asraw_stdio_locks", since = "1.35.0")] +impl<'a> AsRawFd for io::StdoutLock<'a> { + fn as_raw_fd(&self) -> RawFd { 1 } +} + +#[stable(feature = "asraw_stdio_locks", since = "1.35.0")] +impl<'a> AsRawFd for io::StderrLock<'a> { + fn as_raw_fd(&self) -> RawFd { 2 } +} + #[stable(feature = "from_raw_os", since = "1.1.0")] impl FromRawFd for net::TcpStream { unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream { diff --git a/src/libstd/sys/unix/ext/io.rs b/src/libstd/sys/unix/ext/io.rs index 1a0b3b8962b..6bcc59495e3 100644 --- a/src/libstd/sys/unix/ext/io.rs +++ b/src/libstd/sys/unix/ext/io.rs @@ -95,3 +95,18 @@ impl AsRawFd for io::Stdout { impl AsRawFd for io::Stderr { fn as_raw_fd(&self) -> RawFd { libc::STDERR_FILENO } } + +#[stable(feature = "asraw_stdio_locks", since = "1.35.0")] +impl<'a> AsRawFd for io::StdinLock<'a> { + fn as_raw_fd(&self) -> RawFd { libc::STDIN_FILENO } +} + +#[stable(feature = "asraw_stdio_locks", since = "1.35.0")] +impl<'a> AsRawFd for io::StdoutLock<'a> { + fn as_raw_fd(&self) -> RawFd { libc::STDOUT_FILENO } +} + +#[stable(feature = "asraw_stdio_locks", since = "1.35.0")] +impl<'a> AsRawFd for io::StderrLock<'a> { + fn as_raw_fd(&self) -> RawFd { libc::STDERR_FILENO } +} diff --git a/src/libstd/sys/windows/ext/io.rs b/src/libstd/sys/windows/ext/io.rs index 1a7d734b89e..ec47c2e9d5a 100644 --- a/src/libstd/sys/windows/ext/io.rs +++ b/src/libstd/sys/windows/ext/io.rs @@ -83,6 +83,27 @@ impl AsRawHandle for io::Stderr { } } +#[stable(feature = "asraw_stdio_locks", since = "1.35.0")] +impl<'a> AsRawHandle for io::StdinLock<'a> { + fn as_raw_handle(&self) -> RawHandle { + unsafe { c::GetStdHandle(c::STD_INPUT_HANDLE) as RawHandle } + } +} + +#[stable(feature = "asraw_stdio_locks", since = "1.35.0")] +impl<'a> AsRawHandle for io::StdoutLock<'a> { + fn as_raw_handle(&self) -> RawHandle { + unsafe { c::GetStdHandle(c::STD_OUTPUT_HANDLE) as RawHandle } + } +} + +#[stable(feature = "asraw_stdio_locks", since = "1.35.0")] +impl<'a> AsRawHandle for io::StderrLock<'a> { + fn as_raw_handle(&self) -> RawHandle { + unsafe { c::GetStdHandle(c::STD_ERROR_HANDLE) as RawHandle } + } +} + #[stable(feature = "from_raw_os", since = "1.1.0")] impl FromRawHandle for fs::File { unsafe fn from_raw_handle(handle: RawHandle) -> fs::File { -- cgit 1.4.1-3-g733a5 From f10e44420a072881cce6d7819d2b2bfb99df90df Mon Sep 17 00:00:00 2001 From: Christian Date: Fri, 29 Mar 2019 15:56:22 +0100 Subject: Edited the dbg! docs stating that dbg! works the same way in release builds. --- src/libstd/macros.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index 9d0eb2e6b1c..ee5a8e6631e 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -231,10 +231,13 @@ macro_rules! eprintln { /// to give up ownership, you can instead borrow with `dbg!(&expr)` /// for some expression `expr`. /// +/// The `dbg!` macro works exactly the same in release builds. This is useful when debugging issues +/// that only occur in release builds or when debugging in release mode is significantly faster. +/// /// Note that the macro is intended as a debugging tool and therefore you /// should avoid having uses of it in version control for longer periods. /// Use cases involving debug output that should be added to version control -/// may be better served by macros such as `debug!` from the `log` crate. +/// are better served by macros such as [`debug!`][debug-log] from the [`log`][log] crate. /// /// # Stability /// @@ -306,6 +309,8 @@ macro_rules! eprintln { /// ``` /// /// [stderr]: https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr) +/// [debug-log]: https://docs.rs/log/*/log/macro.debug.html +/// [log]: https://docs.rs/log/ #[macro_export] #[stable(feature = "dbg_macro", since = "1.32.0")] macro_rules! dbg { -- cgit 1.4.1-3-g733a5 From 9240092fe3bf32803ac9f9a247a098844d6f4780 Mon Sep 17 00:00:00 2001 From: Christian Date: Fri, 29 Mar 2019 16:18:24 +0100 Subject: Adjusted the indentation. --- src/libstd/macros.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index ee5a8e6631e..03d2a307216 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -231,8 +231,9 @@ macro_rules! eprintln { /// to give up ownership, you can instead borrow with `dbg!(&expr)` /// for some expression `expr`. /// -/// The `dbg!` macro works exactly the same in release builds. This is useful when debugging issues -/// that only occur in release builds or when debugging in release mode is significantly faster. +/// The `dbg!` macro works exactly the same in release builds. +/// This is useful when debugging issues that only occur in release builds or when debugging in +/// release mode is significantly faster. /// /// Note that the macro is intended as a debugging tool and therefore you /// should avoid having uses of it in version control for longer periods. -- cgit 1.4.1-3-g733a5 From fe210d0df17b16d3afa3ec5462767bd9f20a4c0c Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Fri, 29 Mar 2019 16:24:13 +0100 Subject: Update src/libstd/macros.rs Wrapped lines earlier such that it is more coherent with the rest of the text. Co-Authored-By: DevQps <46896178+DevQps@users.noreply.github.com> --- src/libstd/macros.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index 03d2a307216..37e88ee6209 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -232,7 +232,8 @@ macro_rules! eprintln { /// for some expression `expr`. /// /// The `dbg!` macro works exactly the same in release builds. -/// This is useful when debugging issues that only occur in release builds or when debugging in +/// This is useful when debugging issues that only occur in release +/// builds or when debugging in release mode is significantly faster. /// release mode is significantly faster. /// /// Note that the macro is intended as a debugging tool and therefore you -- cgit 1.4.1-3-g733a5 From 8705de49e1de83f357fcd6224f9e0a5e266915da Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Fri, 29 Mar 2019 16:25:38 +0100 Subject: Update src/libstd/macros.rs Removed duplicate line. Co-Authored-By: DevQps <46896178+DevQps@users.noreply.github.com> --- src/libstd/macros.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index 37e88ee6209..bc0a97939d8 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -234,7 +234,6 @@ macro_rules! eprintln { /// The `dbg!` macro works exactly the same in release builds. /// This is useful when debugging issues that only occur in release /// builds or when debugging in release mode is significantly faster. -/// release mode is significantly faster. /// /// Note that the macro is intended as a debugging tool and therefore you /// should avoid having uses of it in version control for longer periods. -- cgit 1.4.1-3-g733a5 From b6fb3e34117008f7f97094b0f1521116a4e66473 Mon Sep 17 00:00:00 2001 From: Matt Brubeck Date: Fri, 29 Mar 2019 09:30:08 -0700 Subject: In doc examples, don't ignore read/write results Calling `Read::read` or `Write::write` without checking the returned `usize` value is almost always an error. Example code in the documentation should demonstrate how to use the return value correctly. Otherwise, people might copy the example code thinking that it is okay to "fire and forget" these methods. --- src/libstd/io/mod.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 14c850b6b05..14a16f3fc0f 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -24,9 +24,9 @@ //! let mut buffer = [0; 10]; //! //! // read up to 10 bytes -//! f.read(&mut buffer)?; +//! let n = f.read(&mut buffer)?; //! -//! println!("The bytes: {:?}", buffer); +//! println!("The bytes: {:?}", &buffer[..n]); //! Ok(()) //! } //! ``` @@ -56,9 +56,9 @@ //! f.seek(SeekFrom::End(-10))?; //! //! // read up to 10 bytes -//! f.read(&mut buffer)?; +//! let n = f.read(&mut buffer)?; //! -//! println!("The bytes: {:?}", buffer); +//! println!("The bytes: {:?}", &buffer[..n]); //! Ok(()) //! } //! ``` @@ -537,7 +537,9 @@ pub trait Read { /// let mut buffer = [0; 10]; /// /// // read up to 10 bytes - /// f.read(&mut buffer[..])?; + /// let n = f.read(&mut buffer[..])?; + /// + /// println!("The bytes: {:?}", &buffer[..n]); /// Ok(()) /// } /// ``` @@ -1062,12 +1064,23 @@ impl Initializer { /// use std::fs::File; /// /// fn main() -> std::io::Result<()> { +/// let data = b"some bytes"; +/// +/// let mut pos = 0; /// let mut buffer = File::create("foo.txt")?; /// -/// buffer.write(b"some bytes")?; +/// while pos < data.len() { +/// let bytes_written = buffer.write(&data[pos..])?; +/// pos += bytes_written; +/// } /// Ok(()) /// } /// ``` +/// +/// The trait also provides convenience methods like [`write_all`], which calls +/// `write` in a loop until its entire input has been written. +/// +/// [`write_all`]: #method.write_all #[stable(feature = "rust1", since = "1.0.0")] #[doc(spotlight)] pub trait Write { -- cgit 1.4.1-3-g733a5