From a765dcaf5378cbd6cfa71f3fdeede82eeca4fc26 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Sat, 22 Apr 2017 01:14:18 -0700 Subject: [PATCH 1/4] Add internal accessor methods to io::{Chain, Take}. Resolves #29067. --- src/libstd/io/mod.rs | 131 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index cd096c115ba..cf548346b17 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -1494,6 +1494,83 @@ pub struct Chain { done_first: bool, } +impl Chain { + /// Consumes the `Chain`, returning the wrapped readers. + /// + /// # Examples + /// + /// ``` + /// #![feature(more_io_inner_methods)] + /// + /// # use std::io; + /// use std::io::prelude::*; + /// use std::fs::File; + /// + /// # fn foo() -> io::Result<()> { + /// let mut foo_file = File::open("foo.txt")?; + /// let mut bar_file = File::open("bar.txt")?; + /// + /// let chain = foo_file.chain(bar_file); + /// let (foo_file, bar_file) = chain.into_inner(); + /// # Ok(()) + /// # } + /// ``` + #[unstable(feature = "more_io_inner_methods", issue="0")] + pub fn into_inner(self) -> (T, U) { + (self.first, self.second) + } + + /// Gets references to the underlying readers in this `Chain`. + /// + /// # Examples + /// + /// ``` + /// #![feature(more_io_inner_methods)] + /// + /// # use std::io; + /// use std::io::prelude::*; + /// use std::fs::File; + /// + /// # fn foo() -> io::Result<()> { + /// let mut foo_file = File::open("foo.txt")?; + /// let mut bar_file = File::open("bar.txt")?; + /// + /// let chain = foo_file.chain(bar_file); + /// let (foo_file, bar_file) = chain.get_ref(); + /// # Ok(()) + /// # } + /// ``` + #[unstable(feature = "more_io_inner_methods", issue="0")] + pub fn get_ref(&self) -> (&T, &U) { + (&self.first, &self.second) + } + + /// Gets mutable references to the underlying readers in this `Chain`. + /// + /// # Examples + /// + /// ``` + /// #![feature(more_io_inner_methods)] + /// + /// # use std::io; + /// use std::io::prelude::*; + /// use std::fs::File; + /// + /// # fn foo() -> io::Result<()> { + /// let mut foo_file = File::open("foo.txt")?; + /// let mut bar_file = File::open("bar.txt")?; + /// + /// let mut chain = foo_file.chain(bar_file); + /// let (foo_file, bar_file) = chain.get_mut(); + /// # Ok(()) + /// # } + /// ``` + #[unstable(feature = "more_io_inner_methods", issue="0")] + pub fn get_mut(&mut self) -> (&mut T, &mut U) { + (&mut self.first, &mut self.second) + } +} + #[stable(feature = "std_debug", since = "1.16.0")] impl fmt::Debug for Chain { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -1606,6 +1683,60 @@ impl Take { pub fn into_inner(self) -> T { self.inner } + + /// Gets a reference to the underlying reader. + /// + /// # Examples + /// + /// ``` + /// #![feature(more_io_inner_methods)] + /// + /// use std::io; + /// use std::io::prelude::*; + /// use std::fs::File; + /// + /// # fn foo() -> io::Result<()> { + /// let mut file = File::open("foo.txt")?; + /// + /// let mut buffer = [0; 5]; + /// let mut handle = file.take(5); + /// handle.read(&mut buffer)?; + /// + /// let file = handle.get_ref(); + /// # Ok(()) + /// # } + /// ``` + #[unstable(feature = "more_io_inner_methods", issue="0")] + pub fn get_ref(&self) -> &T { + &self.inner + } + + /// Gets a mutable reference to the underlying reader. + /// + /// # Examples + /// + /// ``` + /// #![feature(more_io_inner_methods)] + /// + /// use std::io; + /// use std::io::prelude::*; + /// use std::fs::File; + /// + /// # fn foo() -> io::Result<()> { + /// let mut file = File::open("foo.txt")?; + /// + /// let mut buffer = [0; 5]; + /// let mut handle = file.take(5); + /// handle.read(&mut buffer)?; + /// + /// let file = handle.get_mut(); + /// # Ok(()) + /// # } + /// ``` + #[unstable(feature = "more_io_inner_methods", issue="0")] + pub fn get_mut(&mut self) -> &mut T { + &mut self.inner + } } #[stable(feature = "rust1", since = "1.0.0")] From aab87e3e9e71696d475a0b2c57f0dee7f702fc27 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Sat, 22 Apr 2017 03:35:53 -0700 Subject: [PATCH 2/4] Add more_io_inner_methods feature to unstable book. --- src/doc/unstable-book/src/SUMMARY.md | 1 + .../src/library-features/more-io-inner-methods.md | 9 +++++++++ 2 files changed, 10 insertions(+) create mode 100644 src/doc/unstable-book/src/library-features/more-io-inner-methods.md diff --git a/src/doc/unstable-book/src/SUMMARY.md b/src/doc/unstable-book/src/SUMMARY.md index 61347573041..9b77d99403f 100644 --- a/src/doc/unstable-book/src/SUMMARY.md +++ b/src/doc/unstable-book/src/SUMMARY.md @@ -160,6 +160,7 @@ - [linked_list_extras](library-features/linked-list-extras.md) - [lookup_host](library-features/lookup-host.md) - [manually_drop](library-features/manually-drop.md) + - [more_io_inner_methods](library-features/more-io-inner-methods.md) - [mpsc_select](library-features/mpsc-select.md) - [n16](library-features/n16.md) - [never_type_impls](library-features/never-type-impls.md) diff --git a/src/doc/unstable-book/src/library-features/more-io-inner-methods.md b/src/doc/unstable-book/src/library-features/more-io-inner-methods.md new file mode 100644 index 00000000000..1b56a8b395c --- /dev/null +++ b/src/doc/unstable-book/src/library-features/more-io-inner-methods.md @@ -0,0 +1,9 @@ +# `more_io_inner_methods` + +The tracking issue for this feature is: None. + +------------------------ + +This feature enables several internal accessor methods on structures in +`std::io` including `Take::{get_ref, get_mut}` and `Chain::{into_inner, get_ref, +get_mut}`. From 76397aea50a506ac177b74d352a0ebbe99d4cdc6 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Mon, 24 Apr 2017 16:46:05 -0700 Subject: [PATCH 3/4] Reference tracking issue for more_io_inner_methods. --- .../src/library-features/more-io-inner-methods.md | 4 +++- src/libstd/io/mod.rs | 10 +++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/doc/unstable-book/src/library-features/more-io-inner-methods.md b/src/doc/unstable-book/src/library-features/more-io-inner-methods.md index 1b56a8b395c..c84f40e7ee5 100644 --- a/src/doc/unstable-book/src/library-features/more-io-inner-methods.md +++ b/src/doc/unstable-book/src/library-features/more-io-inner-methods.md @@ -1,6 +1,8 @@ # `more_io_inner_methods` -The tracking issue for this feature is: None. +The tracking issue for this feature is: [#41519] + +[#41519]: https://github.com/rust-lang/rust/issues/41519 ------------------------ diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index cf548346b17..a30246ae88b 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -1515,7 +1515,7 @@ impl Chain { /// # Ok(()) /// # } /// ``` - #[unstable(feature = "more_io_inner_methods", issue="0")] + #[unstable(feature = "more_io_inner_methods", issue="41519")] pub fn into_inner(self) -> (T, U) { (self.first, self.second) } @@ -1540,7 +1540,7 @@ impl Chain { /// # Ok(()) /// # } /// ``` - #[unstable(feature = "more_io_inner_methods", issue="0")] + #[unstable(feature = "more_io_inner_methods", issue="41519")] pub fn get_ref(&self) -> (&T, &U) { (&self.first, &self.second) } @@ -1565,7 +1565,7 @@ impl Chain { /// # Ok(()) /// # } /// ``` - #[unstable(feature = "more_io_inner_methods", issue="0")] + #[unstable(feature = "more_io_inner_methods", issue="41519")] pub fn get_mut(&mut self) -> (&mut T, &mut U) { (&mut self.first, &mut self.second) } @@ -1706,7 +1706,7 @@ impl Take { /// # Ok(()) /// # } /// ``` - #[unstable(feature = "more_io_inner_methods", issue="0")] + #[unstable(feature = "more_io_inner_methods", issue="41519")] pub fn get_ref(&self) -> &T { &self.inner } @@ -1733,7 +1733,7 @@ impl Take { /// # Ok(()) /// # } /// ``` - #[unstable(feature = "more_io_inner_methods", issue="0")] + #[unstable(feature = "more_io_inner_methods", issue="41519")] pub fn get_mut(&mut self) -> &mut T { &mut self.inner } From c168d8bb07392ca6c5e30c2cde1458c9e32bf03b Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Mon, 24 Apr 2017 16:46:21 -0700 Subject: [PATCH 4/4] Add cautions to io::get_mut method documentation. --- src/libstd/io/mod.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index a30246ae88b..d08ba591de0 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -1547,6 +1547,10 @@ impl Chain { /// Gets mutable references to the underlying readers in this `Chain`. /// + /// Care should be taken to avoid modifying the internal I/O state of the + /// underlying readers as doing so may corrupt the internal state of this + /// `Chain`. + /// /// # Examples /// /// ``` @@ -1713,6 +1717,10 @@ impl Take { /// Gets a mutable reference to the underlying reader. /// + /// Care should be taken to avoid modifying the internal I/O state of the + /// underlying reader as doing so may corrupt the internal limit of this + /// `Take`. + /// /// # Examples /// /// ```