Skip to content

Commit 5f62b27

Browse files
authored
Rollup merge of rust-lang#41463 - SergioBenitez:master, r=alexcrichton
Add internal accessor methods to io::{Chain, Take}. Resolves rust-lang#29067.
2 parents 2a320c3 + c168d8b commit 5f62b27

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

src/doc/unstable-book/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@
160160
- [linked_list_extras](library-features/linked-list-extras.md)
161161
- [lookup_host](library-features/lookup-host.md)
162162
- [manually_drop](library-features/manually-drop.md)
163+
- [more_io_inner_methods](library-features/more-io-inner-methods.md)
163164
- [mpsc_select](library-features/mpsc-select.md)
164165
- [n16](library-features/n16.md)
165166
- [never_type_impls](library-features/never-type-impls.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# `more_io_inner_methods`
2+
3+
The tracking issue for this feature is: [#41519]
4+
5+
[#41519]: https://github.com/rust-lang/rust/issues/41519
6+
7+
------------------------
8+
9+
This feature enables several internal accessor methods on structures in
10+
`std::io` including `Take::{get_ref, get_mut}` and `Chain::{into_inner, get_ref,
11+
get_mut}`.

src/libstd/io/mod.rs

+139
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,87 @@ pub struct Chain<T, U> {
15041504
done_first: bool,
15051505
}
15061506

1507+
impl<T, U> Chain<T, U> {
1508+
/// Consumes the `Chain`, returning the wrapped readers.
1509+
///
1510+
/// # Examples
1511+
///
1512+
/// ```
1513+
/// #![feature(more_io_inner_methods)]
1514+
///
1515+
/// # use std::io;
1516+
/// use std::io::prelude::*;
1517+
/// use std::fs::File;
1518+
///
1519+
/// # fn foo() -> io::Result<()> {
1520+
/// let mut foo_file = File::open("foo.txt")?;
1521+
/// let mut bar_file = File::open("bar.txt")?;
1522+
///
1523+
/// let chain = foo_file.chain(bar_file);
1524+
/// let (foo_file, bar_file) = chain.into_inner();
1525+
/// # Ok(())
1526+
/// # }
1527+
/// ```
1528+
#[unstable(feature = "more_io_inner_methods", issue="41519")]
1529+
pub fn into_inner(self) -> (T, U) {
1530+
(self.first, self.second)
1531+
}
1532+
1533+
/// Gets references to the underlying readers in this `Chain`.
1534+
///
1535+
/// # Examples
1536+
///
1537+
/// ```
1538+
/// #![feature(more_io_inner_methods)]
1539+
///
1540+
/// # use std::io;
1541+
/// use std::io::prelude::*;
1542+
/// use std::fs::File;
1543+
///
1544+
/// # fn foo() -> io::Result<()> {
1545+
/// let mut foo_file = File::open("foo.txt")?;
1546+
/// let mut bar_file = File::open("bar.txt")?;
1547+
///
1548+
/// let chain = foo_file.chain(bar_file);
1549+
/// let (foo_file, bar_file) = chain.get_ref();
1550+
/// # Ok(())
1551+
/// # }
1552+
/// ```
1553+
#[unstable(feature = "more_io_inner_methods", issue="41519")]
1554+
pub fn get_ref(&self) -> (&T, &U) {
1555+
(&self.first, &self.second)
1556+
}
1557+
1558+
/// Gets mutable references to the underlying readers in this `Chain`.
1559+
///
1560+
/// Care should be taken to avoid modifying the internal I/O state of the
1561+
/// underlying readers as doing so may corrupt the internal state of this
1562+
/// `Chain`.
1563+
///
1564+
/// # Examples
1565+
///
1566+
/// ```
1567+
/// #![feature(more_io_inner_methods)]
1568+
///
1569+
/// # use std::io;
1570+
/// use std::io::prelude::*;
1571+
/// use std::fs::File;
1572+
///
1573+
/// # fn foo() -> io::Result<()> {
1574+
/// let mut foo_file = File::open("foo.txt")?;
1575+
/// let mut bar_file = File::open("bar.txt")?;
1576+
///
1577+
/// let mut chain = foo_file.chain(bar_file);
1578+
/// let (foo_file, bar_file) = chain.get_mut();
1579+
/// # Ok(())
1580+
/// # }
1581+
/// ```
1582+
#[unstable(feature = "more_io_inner_methods", issue="41519")]
1583+
pub fn get_mut(&mut self) -> (&mut T, &mut U) {
1584+
(&mut self.first, &mut self.second)
1585+
}
1586+
}
1587+
15071588
#[stable(feature = "std_debug", since = "1.16.0")]
15081589
impl<T: fmt::Debug, U: fmt::Debug> fmt::Debug for Chain<T, U> {
15091590
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -1616,6 +1697,64 @@ impl<T> Take<T> {
16161697
pub fn into_inner(self) -> T {
16171698
self.inner
16181699
}
1700+
1701+
/// Gets a reference to the underlying reader.
1702+
///
1703+
/// # Examples
1704+
///
1705+
/// ```
1706+
/// #![feature(more_io_inner_methods)]
1707+
///
1708+
/// use std::io;
1709+
/// use std::io::prelude::*;
1710+
/// use std::fs::File;
1711+
///
1712+
/// # fn foo() -> io::Result<()> {
1713+
/// let mut file = File::open("foo.txt")?;
1714+
///
1715+
/// let mut buffer = [0; 5];
1716+
/// let mut handle = file.take(5);
1717+
/// handle.read(&mut buffer)?;
1718+
///
1719+
/// let file = handle.get_ref();
1720+
/// # Ok(())
1721+
/// # }
1722+
/// ```
1723+
#[unstable(feature = "more_io_inner_methods", issue="41519")]
1724+
pub fn get_ref(&self) -> &T {
1725+
&self.inner
1726+
}
1727+
1728+
/// Gets a mutable reference to the underlying reader.
1729+
///
1730+
/// Care should be taken to avoid modifying the internal I/O state of the
1731+
/// underlying reader as doing so may corrupt the internal limit of this
1732+
/// `Take`.
1733+
///
1734+
/// # Examples
1735+
///
1736+
/// ```
1737+
/// #![feature(more_io_inner_methods)]
1738+
///
1739+
/// use std::io;
1740+
/// use std::io::prelude::*;
1741+
/// use std::fs::File;
1742+
///
1743+
/// # fn foo() -> io::Result<()> {
1744+
/// let mut file = File::open("foo.txt")?;
1745+
///
1746+
/// let mut buffer = [0; 5];
1747+
/// let mut handle = file.take(5);
1748+
/// handle.read(&mut buffer)?;
1749+
///
1750+
/// let file = handle.get_mut();
1751+
/// # Ok(())
1752+
/// # }
1753+
/// ```
1754+
#[unstable(feature = "more_io_inner_methods", issue="41519")]
1755+
pub fn get_mut(&mut self) -> &mut T {
1756+
&mut self.inner
1757+
}
16191758
}
16201759

16211760
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)