Skip to content

Commit a765dca

Browse files
committed
Add internal accessor methods to io::{Chain, Take}.
Resolves #29067.
1 parent 63c7721 commit a765dca

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

src/libstd/io/mod.rs

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,83 @@ pub struct Chain<T, U> {
14941494
done_first: bool,
14951495
}
14961496

1497+
impl<T, U> Chain<T, U> {
1498+
/// Consumes the `Chain`, returning the wrapped readers.
1499+
///
1500+
/// # Examples
1501+
///
1502+
/// ```
1503+
/// #![feature(more_io_inner_methods)]
1504+
///
1505+
/// # use std::io;
1506+
/// use std::io::prelude::*;
1507+
/// use std::fs::File;
1508+
///
1509+
/// # fn foo() -> io::Result<()> {
1510+
/// let mut foo_file = File::open("foo.txt")?;
1511+
/// let mut bar_file = File::open("bar.txt")?;
1512+
///
1513+
/// let chain = foo_file.chain(bar_file);
1514+
/// let (foo_file, bar_file) = chain.into_inner();
1515+
/// # Ok(())
1516+
/// # }
1517+
/// ```
1518+
#[unstable(feature = "more_io_inner_methods", issue="0")]
1519+
pub fn into_inner(self) -> (T, U) {
1520+
(self.first, self.second)
1521+
}
1522+
1523+
/// Gets references to the underlying readers in this `Chain`.
1524+
///
1525+
/// # Examples
1526+
///
1527+
/// ```
1528+
/// #![feature(more_io_inner_methods)]
1529+
///
1530+
/// # use std::io;
1531+
/// use std::io::prelude::*;
1532+
/// use std::fs::File;
1533+
///
1534+
/// # fn foo() -> io::Result<()> {
1535+
/// let mut foo_file = File::open("foo.txt")?;
1536+
/// let mut bar_file = File::open("bar.txt")?;
1537+
///
1538+
/// let chain = foo_file.chain(bar_file);
1539+
/// let (foo_file, bar_file) = chain.get_ref();
1540+
/// # Ok(())
1541+
/// # }
1542+
/// ```
1543+
#[unstable(feature = "more_io_inner_methods", issue="0")]
1544+
pub fn get_ref(&self) -> (&T, &U) {
1545+
(&self.first, &self.second)
1546+
}
1547+
1548+
/// Gets mutable references to the underlying readers in this `Chain`.
1549+
///
1550+
/// # Examples
1551+
///
1552+
/// ```
1553+
/// #![feature(more_io_inner_methods)]
1554+
///
1555+
/// # use std::io;
1556+
/// use std::io::prelude::*;
1557+
/// use std::fs::File;
1558+
///
1559+
/// # fn foo() -> io::Result<()> {
1560+
/// let mut foo_file = File::open("foo.txt")?;
1561+
/// let mut bar_file = File::open("bar.txt")?;
1562+
///
1563+
/// let mut chain = foo_file.chain(bar_file);
1564+
/// let (foo_file, bar_file) = chain.get_mut();
1565+
/// # Ok(())
1566+
/// # }
1567+
/// ```
1568+
#[unstable(feature = "more_io_inner_methods", issue="0")]
1569+
pub fn get_mut(&mut self) -> (&mut T, &mut U) {
1570+
(&mut self.first, &mut self.second)
1571+
}
1572+
}
1573+
14971574
#[stable(feature = "std_debug", since = "1.16.0")]
14981575
impl<T: fmt::Debug, U: fmt::Debug> fmt::Debug for Chain<T, U> {
14991576
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -1606,6 +1683,60 @@ impl<T> Take<T> {
16061683
pub fn into_inner(self) -> T {
16071684
self.inner
16081685
}
1686+
1687+
/// Gets a reference to the underlying reader.
1688+
///
1689+
/// # Examples
1690+
///
1691+
/// ```
1692+
/// #![feature(more_io_inner_methods)]
1693+
///
1694+
/// use std::io;
1695+
/// use std::io::prelude::*;
1696+
/// use std::fs::File;
1697+
///
1698+
/// # fn foo() -> io::Result<()> {
1699+
/// let mut file = File::open("foo.txt")?;
1700+
///
1701+
/// let mut buffer = [0; 5];
1702+
/// let mut handle = file.take(5);
1703+
/// handle.read(&mut buffer)?;
1704+
///
1705+
/// let file = handle.get_ref();
1706+
/// # Ok(())
1707+
/// # }
1708+
/// ```
1709+
#[unstable(feature = "more_io_inner_methods", issue="0")]
1710+
pub fn get_ref(&self) -> &T {
1711+
&self.inner
1712+
}
1713+
1714+
/// Gets a mutable reference to the underlying reader.
1715+
///
1716+
/// # Examples
1717+
///
1718+
/// ```
1719+
/// #![feature(more_io_inner_methods)]
1720+
///
1721+
/// use std::io;
1722+
/// use std::io::prelude::*;
1723+
/// use std::fs::File;
1724+
///
1725+
/// # fn foo() -> io::Result<()> {
1726+
/// let mut file = File::open("foo.txt")?;
1727+
///
1728+
/// let mut buffer = [0; 5];
1729+
/// let mut handle = file.take(5);
1730+
/// handle.read(&mut buffer)?;
1731+
///
1732+
/// let file = handle.get_mut();
1733+
/// # Ok(())
1734+
/// # }
1735+
/// ```
1736+
#[unstable(feature = "more_io_inner_methods", issue="0")]
1737+
pub fn get_mut(&mut self) -> &mut T {
1738+
&mut self.inner
1739+
}
16091740
}
16101741

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

0 commit comments

Comments
 (0)