Skip to content

Commit 8ac29bd

Browse files
committed
Auto merge of #42697 - Mark-Simulacrum:take-limit, r=aturon
Allow setting the limit on std::io::Take. Fixes #27269.
2 parents d1f1f86 + 7109d03 commit 8ac29bd

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/libstd/io/mod.rs

+29
Original file line numberDiff line numberDiff line change
@@ -1755,6 +1755,35 @@ impl<T> Take<T> {
17551755
#[stable(feature = "rust1", since = "1.0.0")]
17561756
pub fn limit(&self) -> u64 { self.limit }
17571757

1758+
/// Sets the number of bytes that can be read before this instance will
1759+
/// return EOF. This is the same as constructing a new `Take` instance, so
1760+
/// the amount of bytes read and the previous limit value don't matter when
1761+
/// calling this method.
1762+
///
1763+
/// # Examples
1764+
///
1765+
/// ```
1766+
/// #![feature(take_set_limit)]
1767+
/// use std::io;
1768+
/// use std::io::prelude::*;
1769+
/// use std::fs::File;
1770+
///
1771+
/// # fn foo() -> io::Result<()> {
1772+
/// let f = File::open("foo.txt")?;
1773+
///
1774+
/// // read at most five bytes
1775+
/// let mut handle = f.take(5);
1776+
/// handle.set_limit(10);
1777+
///
1778+
/// assert_eq!(handle.limit(), 10);
1779+
/// # Ok(())
1780+
/// # }
1781+
/// ```
1782+
#[unstable(feature = "take_set_limit", issue = "42781")]
1783+
pub fn set_limit(&mut self, limit: u64) {
1784+
self.limit = limit;
1785+
}
1786+
17581787
/// Consumes the `Take`, returning the wrapped reader.
17591788
///
17601789
/// # Examples

0 commit comments

Comments
 (0)