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.
This commit is contained in:
bors 2013-09-14 00:01:04 -07:00
commit 2aa578efd9
55 changed files with 1102 additions and 230 deletions

View file

@ -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 */

View file

@ -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)`")
};
}

View file

@ -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 \

View file

@ -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.

View file

@ -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>>
}

View file

@ -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,