diff options
| author | bors <bors@rust-lang.org> | 2013-09-14 00:01:04 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-09-14 00:01:04 -0700 |
| commit | 2aa578efd9834e37ad52879ff10ee2c2aa938389 (patch) | |
| tree | aa459cb42c65433dfbddca28040db544aea89f59 /src/libsyntax | |
| parent | 4ac10f8f6e8e07c70fadb676170c5402442e2243 (diff) | |
| parent | 93683ae6da3a47f1cd0644a093cb4b1b0bee7faa (diff) | |
| download | rust-2aa578efd9834e37ad52879ff10ee2c2aa938389.tar.gz rust-2aa578efd9834e37ad52879ff10ee2c2aa938389.zip | |
auto merge of #9115 : erickt/rust/master, r=erickt
This is a series of patches to modernize option and result. The highlights are: * rename `.unwrap_or_default(value)` and etc to `.unwrap_or(value)` * add `.unwrap_or_default()` that uses the `Default` trait * add `Default` implementations for vecs, HashMap, Option * add `Option.and(T) -> Option<T>`, `Option.and_then(&fn() -> Option<T>) -> Option<T>`, `Option.or(T) -> Option<T>`, and `Option.or_else(&fn() -> Option<T>) -> Option<T>` * add `option::ToOption`, `option::IntoOption`, `option::AsOption`, `result::ToResult`, `result::IntoResult`, `result::AsResult`, `either::ToEither`, and `either::IntoEither`, `either::AsEither` * renamed `Option::chain*` and `Result::chain*` to `and_then` and `or_else` to avoid the eventual collision with `Iterator.chain`. * Added a bunch of impls of `Default` * Added a `#[deriving(Default)]` syntax extension * Removed impls of `Zero` for `Option<T>` and vecs.
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/attr.rs | 4 | ||||
| -rw-r--r-- | src/libsyntax/ext/deriving/default.rs | 79 | ||||
| -rw-r--r-- | src/libsyntax/ext/deriving/mod.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/ext/expand.rs | 17 | ||||
| -rw-r--r-- | src/libsyntax/opt_vec.rs | 4 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 8 |
6 files changed, 103 insertions, 11 deletions
diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs index fd0887de722..295485d6f6e 100644 --- a/src/libsyntax/attr.rs +++ b/src/libsyntax/attr.rs @@ -187,12 +187,12 @@ pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: &str) -> Option<@str> { attrs.iter() .find(|at| name == at.name()) - .chain(|at| at.value_str()) + .and_then(|at| at.value_str()) } pub fn last_meta_item_value_str_by_name(items: &[@MetaItem], name: &str) -> Option<@str> { - items.rev_iter().find(|mi| name == mi.name()).chain(|i| i.value_str()) + items.rev_iter().find(|mi| name == mi.name()).and_then(|i| i.value_str()) } /* Higher-level applications */ diff --git a/src/libsyntax/ext/deriving/default.rs b/src/libsyntax/ext/deriving/default.rs new file mode 100644 index 00000000000..0c7bbefc690 --- /dev/null +++ b/src/libsyntax/ext/deriving/default.rs @@ -0,0 +1,79 @@ +// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use ast::{MetaItem, item, Expr}; +use codemap::Span; +use ext::base::ExtCtxt; +use ext::build::AstBuilder; +use ext::deriving::generic::*; + +use std::vec; + +pub fn expand_deriving_default(cx: @ExtCtxt, + span: Span, + mitem: @MetaItem, + in_items: ~[@item]) + -> ~[@item] { + let trait_def = TraitDef { + path: Path::new(~["std", "default", "Default"]), + additional_bounds: ~[], + generics: LifetimeBounds::empty(), + methods: ~[ + MethodDef { + name: "default", + generics: LifetimeBounds::empty(), + explicit_self: None, + args: ~[], + ret_ty: Self, + const_nonmatching: false, + combine_substructure: default_substructure + }, + ] + }; + trait_def.expand(cx, span, mitem, in_items) +} + +fn default_substructure(cx: @ExtCtxt, span: Span, substr: &Substructure) -> @Expr { + let default_ident = ~[ + cx.ident_of("std"), + cx.ident_of("default"), + cx.ident_of("Default"), + cx.ident_of("default") + ]; + let default_call = || { + cx.expr_call_global(span, default_ident.clone(), ~[]) + }; + + return match *substr.fields { + StaticStruct(_, ref summary) => { + match *summary { + Left(count) => { + if count == 0 { + cx.expr_ident(span, substr.type_ident) + } else { + let exprs = vec::from_fn(count, |_| default_call()); + cx.expr_call_ident(span, substr.type_ident, exprs) + } + } + Right(ref fields) => { + let default_fields = do fields.map |ident| { + cx.field_imm(span, *ident, default_call()) + }; + cx.expr_struct_ident(span, substr.type_ident, default_fields) + } + } + } + StaticEnum(*) => { + cx.span_fatal(span, "`Default` cannot be derived for enums, \ + only structs") + } + _ => cx.bug("Non-static method in `deriving(Default)`") + }; +} diff --git a/src/libsyntax/ext/deriving/mod.rs b/src/libsyntax/ext/deriving/mod.rs index 7fd44bfad01..dfd4f79cd9e 100644 --- a/src/libsyntax/ext/deriving/mod.rs +++ b/src/libsyntax/ext/deriving/mod.rs @@ -31,6 +31,7 @@ pub mod decodable; pub mod rand; pub mod to_str; pub mod zero; +pub mod default; #[path="cmp/eq.rs"] pub mod eq; @@ -97,6 +98,7 @@ pub fn expand_meta_deriving(cx: @ExtCtxt, "ToStr" => expand!(to_str::expand_deriving_to_str), "Zero" => expand!(zero::expand_deriving_zero), + "Default" => expand!(default::expand_deriving_default), ref tname => { cx.span_err(titem.span, fmt!("unknown \ diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 2cebae550fd..4d62285cf6e 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -331,11 +331,18 @@ pub fn expand_item_mac(extsbox: @mut SyntaxEnv, }; let maybe_it = match expanded { - MRItem(it) => mark_item(it,fm).chain(|i| {fld.fold_item(i)}), - MRExpr(_) => cx.span_fatal(pth.span, - fmt!("expr macro in item position: %s", extnamestr)), - MRAny(_, item_maker, _) => item_maker().chain(|i| {mark_item(i,fm)}) - .chain(|i| {fld.fold_item(i)}), + MRItem(it) => { + mark_item(it,fm) + .and_then(|i| fld.fold_item(i)) + } + MRExpr(_) => { + cx.span_fatal(pth.span, fmt!("expr macro in item position: %s", extnamestr)) + } + MRAny(_, item_maker, _) => { + item_maker() + .and_then(|i| mark_item(i,fm)) + .and_then(|i| fld.fold_item(i)) + } MRDef(ref mdef) => { // yikes... no idea how to apply the mark to this. I'm afraid // we're going to have to wait-and-see on this one. diff --git a/src/libsyntax/opt_vec.rs b/src/libsyntax/opt_vec.rs index 2d7801a22de..46940d2d4da 100644 --- a/src/libsyntax/opt_vec.rs +++ b/src/libsyntax/opt_vec.rs @@ -140,6 +140,10 @@ impl<A:Eq> Eq for OptVec<A> { } } +impl<T> Default for OptVec<T> { + fn default() -> OptVec<T> { Empty } +} + pub struct OptVecIterator<'self, T> { priv iter: Option<VecIterator<'self, T>> } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index b5772a9eede..6a15641430f 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 "<T>" and "<T:>". - let bounds = opt_bounds.unwrap_or_default(opt_vec::Empty); + let bounds = opt_bounds.unwrap_or_default(); 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, |
