Skip to content

Commit 755f622

Browse files
committed
auto merge of #9279 : erickt/rust/master, r=alexcrichton
`Some(5).or_{default,zero}` can be easily replaced with `Some(Some(5).unwrap_or_default())`.
2 parents 068e042 + 1a90f24 commit 755f622

File tree

3 files changed

+7
-29
lines changed

3 files changed

+7
-29
lines changed

src/libstd/option.rs

+1-19
Original file line numberDiff line numberDiff line change
@@ -467,23 +467,14 @@ impl<T: Default> Option<T> {
467467
None => Default::default()
468468
}
469469
}
470-
471-
/// Returns self or `Some`-wrapped default value
472-
#[inline]
473-
pub fn or_default(self) -> Option<T> {
474-
match self {
475-
None => Some(Default::default()),
476-
x => x,
477-
}
478-
}
479470
}
480471

481472
impl<T> Default for Option<T> {
482473
#[inline]
483474
fn default() -> Option<T> { None }
484475
}
485476

486-
impl<T:Zero> Option<T> {
477+
impl<T: Zero> Option<T> {
487478
/// Returns the contained value or zero (for this type)
488479
#[inline]
489480
pub fn unwrap_or_zero(self) -> T {
@@ -492,15 +483,6 @@ impl<T:Zero> Option<T> {
492483
None => Zero::zero()
493484
}
494485
}
495-
496-
/// Returns self or `Some`-wrapped zero value
497-
#[inline]
498-
pub fn or_zero(self) -> Option<T> {
499-
match self {
500-
None => Some(Zero::zero()),
501-
x => x
502-
}
503-
}
504486
}
505487

506488
/// An iterator that yields either one or zero elements

src/libsyntax/ext/deriving/default.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ fn default_substructure(cx: @ExtCtxt, span: Span, substr: &Substructure) -> @Exp
4747
cx.ident_of("Default"),
4848
cx.ident_of("default")
4949
];
50-
let default_call = || {
51-
cx.expr_call_global(span, default_ident.clone(), ~[])
52-
};
50+
let default_call = cx.expr_call_global(span, default_ident.clone(), ~[]);
5351

5452
return match *substr.fields {
5553
StaticStruct(_, ref summary) => {
@@ -58,13 +56,13 @@ fn default_substructure(cx: @ExtCtxt, span: Span, substr: &Substructure) -> @Exp
5856
if count == 0 {
5957
cx.expr_ident(span, substr.type_ident)
6058
} else {
61-
let exprs = vec::from_fn(count, |_| default_call());
59+
let exprs = vec::from_elem(count, default_call);
6260
cx.expr_call_ident(span, substr.type_ident, exprs)
6361
}
6462
}
6563
Right(ref fields) => {
6664
let default_fields = do fields.map |ident| {
67-
cx.field_imm(span, *ident, default_call())
65+
cx.field_imm(span, *ident, default_call)
6866
};
6967
cx.expr_struct_ident(span, substr.type_ident, default_fields)
7068
}

src/libsyntax/ext/deriving/zero.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ fn zero_substructure(cx: @ExtCtxt, span: Span, substr: &Substructure) -> @Expr {
6262
cx.ident_of("Zero"),
6363
cx.ident_of("zero")
6464
];
65-
let zero_call = || {
66-
cx.expr_call_global(span, zero_ident.clone(), ~[])
67-
};
65+
let zero_call = cx.expr_call_global(span, zero_ident.clone(), ~[]);
6866

6967
return match *substr.fields {
7068
StaticStruct(_, ref summary) => {
@@ -73,13 +71,13 @@ fn zero_substructure(cx: @ExtCtxt, span: Span, substr: &Substructure) -> @Expr {
7371
if count == 0 {
7472
cx.expr_ident(span, substr.type_ident)
7573
} else {
76-
let exprs = vec::from_fn(count, |_| zero_call());
74+
let exprs = vec::from_elem(count, zero_call);
7775
cx.expr_call_ident(span, substr.type_ident, exprs)
7876
}
7977
}
8078
Right(ref fields) => {
8179
let zero_fields = do fields.map |ident| {
82-
cx.field_imm(span, *ident, zero_call())
80+
cx.field_imm(span, *ident, zero_call)
8381
};
8482
cx.expr_struct_ident(span, substr.type_ident, zero_fields)
8583
}

0 commit comments

Comments
 (0)