Skip to content

Commit bcbe27c

Browse files
Rollup merge of rust-lang#34828 - seanmonstar:into-opton, r=alexcrichton
core: impl From<T> for Option<T> First, the semantics of this `impl` seem spot on. If I have a value `T`, and I wish to make a `Option<T>`, then `Option::from(val)` should always give `Some(val)`. Second, this allows improvement for several APIs that currently take `Option<T>` as arguments. Consider: ```rust fn set_read_timeout(&mut self, timeout: Option<u32>) { // ... } x.set_read_timeout(Some(30)); x.set_read_timeout(Some(10)); x.set_read_timeout(None); ``` With this `impl`: ```rust fn set_read_timeout<T: Into<Option<u32>>>(&mut self, timeout: T) { let timeout = timeout.into(); // ... } x.set_read_timeout(30); x.set_read_timeout(10); x.set_read_timeout(Some(10)); // backwards compatible x.set_read_timeout(None); ``` The change to those methods aren't included, but could be modified later. r? @sfackler
2 parents e7c822c + fbfee42 commit bcbe27c

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/libcore/option.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
use self::Option::*;
143143

144144
use clone::Clone;
145+
use convert::From;
145146
use default::Default;
146147
use iter::ExactSizeIterator;
147148
use iter::{Iterator, DoubleEndedIterator, FromIterator, IntoIterator};
@@ -754,6 +755,13 @@ impl<'a, T> IntoIterator for &'a mut Option<T> {
754755
}
755756
}
756757

758+
#[stable(since = "1.12.0", feature = "option_from")]
759+
impl<T> From<T> for Option<T> {
760+
fn from(val: T) -> Option<T> {
761+
Some(val)
762+
}
763+
}
764+
757765
/////////////////////////////////////////////////////////////////////////////
758766
// The Option Iterators
759767
/////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)