From 45c62c08f9740dfcdf64c64c8b183acfc22fb3d6 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Mon, 9 Sep 2013 19:57:08 -0700 Subject: std: rename Option::unwrap_or_default() to unwrap_or() --- src/libsyntax/parse/parser.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index b5772a9eede..8725a0426f7 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -802,7 +802,7 @@ impl Parser { */ let opt_abis = self.parse_opt_abis(); - let abis = opt_abis.unwrap_or_default(AbiSet::Rust()); + let abis = opt_abis.unwrap_or(AbiSet::Rust()); let purity = self.parse_unsafety(); self.expect_keyword(keywords::Fn); let (decl, lifetimes) = self.parse_ty_fn_decl(); @@ -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 "" and "". - let bounds = opt_bounds.unwrap_or_default(opt_vec::Empty); + let bounds = opt_bounds.unwrap_or(opt_vec::Empty); ast::TyParam { ident: ident, id: ast::DUMMY_NODE_ID, bounds: bounds } } @@ -4363,7 +4363,7 @@ impl Parser { self.obsolete(*self.last_span, ObsoleteExternVisibility); } - let abis = opt_abis.unwrap_or_default(AbiSet::C()); + let abis = opt_abis.unwrap_or(AbiSet::C()); let (inner, next) = self.parse_inner_attrs_and_next(); let m = self.parse_foreign_mod_items(sort, abis, next); @@ -4640,7 +4640,7 @@ impl Parser { if self.eat_keyword(keywords::Fn) { // EXTERN FUNCTION ITEM - let abis = opt_abis.unwrap_or_default(AbiSet::C()); + let abis = opt_abis.unwrap_or(AbiSet::C()); let (ident, item_, extra_attrs) = self.parse_item_fn(extern_fn, abis); return iovi_item(self.mk_item(lo, self.last_span.hi, ident, -- cgit 1.4.1-3-g733a5 From e6c11313c88574aa2500df2f76c5534fbc2e0512 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Mon, 9 Sep 2013 19:32:32 -0700 Subject: std: Add Option.{result_or_default,or_default} that uses Default --- src/librustc/middle/typeck/check/_match.rs | 4 ++-- src/libstd/option.rs | 20 ++++++++++++++++++++ src/libsyntax/parse/parser.rs | 2 +- 3 files changed, 23 insertions(+), 3 deletions(-) (limited to 'src/libsyntax/parse') 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 Option { } } +impl Option { + /// 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 { + match self { + None => Some(Default::default()), + x => x, + } + } +} + impl Default for Option { fn default() -> Option { 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 "" and "". - 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 } } -- cgit 1.4.1-3-g733a5