diff options
| author | Erick Tryzelaar <erick.tryzelaar@gmail.com> | 2013-09-09 19:32:32 -0700 |
|---|---|---|
| committer | Erick Tryzelaar <erick.tryzelaar@gmail.com> | 2013-09-12 18:54:12 -0700 |
| commit | e6c11313c88574aa2500df2f76c5534fbc2e0512 (patch) | |
| tree | 11918e2476ef68448fe58d160e91f290d0a82aff | |
| parent | f1374a7044d844fb1f89b651bf4c628ac32ed48a (diff) | |
| download | rust-e6c11313c88574aa2500df2f76c5534fbc2e0512.tar.gz rust-e6c11313c88574aa2500df2f76c5534fbc2e0512.zip | |
std: Add Option.{result_or_default,or_default} that uses Default
| -rw-r--r-- | src/librustc/middle/typeck/check/_match.rs | 4 | ||||
| -rw-r--r-- | src/libstd/option.rs | 20 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 2 |
3 files changed, 23 insertions, 3 deletions
diff --git a/src/librustc/middle/typeck/check/_match.rs b/src/librustc/middle/typeck/check/_match.rs index f08694e4437..061921e60e1 100644 --- a/src/librustc/middle/typeck/check/_match.rs +++ b/src/librustc/middle/typeck/check/_match.rs @@ -173,7 +173,7 @@ pub fn check_pat_variant(pcx: &pat_ctxt, pat: @ast::Pat, path: &ast::Path, fcx.write_error(pat.id); kind_name = "[error]"; arg_types = (*subpats).clone() - .unwrap_or(~[]) + .unwrap_or_default() .map(|_| ty::mk_err()); } } @@ -222,7 +222,7 @@ pub fn check_pat_variant(pcx: &pat_ctxt, pat: @ast::Pat, path: &ast::Path, fcx.write_error(pat.id); kind_name = "[error]"; arg_types = (*subpats).clone() - .unwrap_or(~[]) + .unwrap_or_default() .map(|_| ty::mk_err()); } } diff --git a/src/libstd/option.rs b/src/libstd/option.rs index 1f3a31a403c..84d8a3aa188 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -350,6 +350,26 @@ impl<T> Option<T> { } } +impl<T: Default> Option<T> { + /// Returns the contained value or default (for this type) + #[inline] + pub fn unwrap_or_default(self) -> T { + match self { + Some(x) => x, + None => Default::default() + } + } + + /// Returns self or `Some`-wrapped default value + #[inline] + pub fn or_default(self) -> Option<T> { + match self { + None => Some(Default::default()), + x => x, + } + } +} + impl<T> Default for Option<T> { fn default() -> Option<T> { None } } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 8725a0426f7..6a15641430f 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3461,7 +3461,7 @@ impl Parser { let ident = self.parse_ident(); let opt_bounds = self.parse_optional_ty_param_bounds(); // For typarams we don't care about the difference b/w "<T>" and "<T:>". - let bounds = opt_bounds.unwrap_or(opt_vec::Empty); + let bounds = opt_bounds.unwrap_or_default(); ast::TyParam { ident: ident, id: ast::DUMMY_NODE_ID, bounds: bounds } } |
