Skip to content

Commit 7a6cd2b

Browse files
committed
auto merge of #5608 : erickt/rust/incoming, r=catamorphism
@nikomatsakis pointed out that `fn read_option<T>(&self, f: &fn() -> T) -> Option<T>` should have this syntax so it can work with custom option types: `fn read_option<T>(&self, f: &fn(bool) -> T) -> T`. Also, this also includes some `#[inline(always)]` on the memory functions in `src/libcore/unstable/lang.rs` to reduce one level of indirection when allocating memory.
2 parents f814592 + aa779c1 commit 7a6cd2b

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

Diff for: src/libcore/unstable/lang.rs

+5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ pub unsafe fn fail_borrowed() {
6464

6565
// FIXME #4942: Make these signatures agree with exchange_alloc's signatures
6666
#[lang="exchange_malloc"]
67+
#[inline(always)]
6768
pub unsafe fn exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char {
6869
transmute(exchange_alloc::malloc(transmute(td), transmute(size)))
6970
}
@@ -72,11 +73,13 @@ pub unsafe fn exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char {
7273
// inside a landing pad may corrupt the state of the exception handler. If a
7374
// problem occurs, call exit instead.
7475
#[lang="exchange_free"]
76+
#[inline(always)]
7577
pub unsafe fn exchange_free(ptr: *c_char) {
7678
exchange_alloc::free(transmute(ptr))
7779
}
7880

7981
#[lang="malloc"]
82+
#[inline(always)]
8083
pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char {
8184
return rustrt::rust_upcall_malloc(td, size);
8285
}
@@ -85,6 +88,7 @@ pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char {
8588
// inside a landing pad may corrupt the state of the exception handler. If a
8689
// problem occurs, call exit instead.
8790
#[lang="free"]
91+
#[inline(always)]
8892
pub unsafe fn local_free(ptr: *c_char) {
8993
rustrt::rust_upcall_free(ptr);
9094
}
@@ -117,6 +121,7 @@ pub unsafe fn check_not_borrowed(a: *u8) {
117121
}
118122

119123
#[lang="strdup_uniq"]
124+
#[inline(always)]
120125
pub unsafe fn strdup_uniq(ptr: *c_uchar, len: uint) -> ~str {
121126
str::raw::from_buf_len(ptr, len)
122127
}

Diff for: src/libstd/ebml.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -411,13 +411,13 @@ pub mod reader {
411411
}
412412
413413
#[cfg(stage0)]
414-
fn read_option<T>(&self, f: &fn() -> T) -> Option<T> {
414+
fn read_option<T>(&self, f: &fn(bool) -> T) -> T {
415415
debug!("read_option()");
416416
do self.read_enum("Option") || {
417417
do self.read_enum_variant |idx| {
418418
match idx {
419-
0 => None,
420-
1 => Some(f()),
419+
0 => f(false),
420+
1 => f(true),
421421
_ => fail!(),
422422
}
423423
}
@@ -427,13 +427,13 @@ pub mod reader {
427427
#[cfg(stage1)]
428428
#[cfg(stage2)]
429429
#[cfg(stage3)]
430-
fn read_option<T>(&self, f: &fn() -> T) -> Option<T> {
430+
fn read_option<T>(&self, f: &fn(bool) -> T) -> T {
431431
debug!("read_option()");
432432
do self.read_enum("Option") || {
433433
do self.read_enum_variant(["None", "Some"]) |idx| {
434434
match idx {
435-
0 => None,
436-
1 => Some(f()),
435+
0 => f(false),
436+
1 => f(true),
437437
_ => fail!(),
438438
}
439439
}

Diff for: src/libstd/json.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -980,10 +980,10 @@ impl<'self> serialize::Decoder for Decoder<'self> {
980980
}
981981
}
982982
983-
fn read_option<T>(&self, f: &fn() -> T) -> Option<T> {
983+
fn read_option<T>(&self, f: &fn(bool) -> T) -> T {
984984
match *self.peek() {
985-
Null => { self.pop(); None }
986-
_ => Some(f()),
985+
Null => { self.pop(); f(false) }
986+
_ => f(true),
987987
}
988988
}
989989
}

Diff for: src/libstd/serialize.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub trait Decoder {
118118
fn read_tup_elt<T>(&self, idx: uint, f: &fn() -> T) -> T;
119119

120120
// Specialized types:
121-
fn read_option<T>(&self, f: &fn() -> T) -> Option<T>;
121+
fn read_option<T>(&self, f: &fn(bool) -> T) -> T;
122122
}
123123

124124
pub trait Encodable<S:Encoder> {
@@ -395,7 +395,13 @@ impl<S:Encoder,T:Encodable<S>> Encodable<S> for Option<T> {
395395

396396
impl<D:Decoder,T:Decodable<D>> Decodable<D> for Option<T> {
397397
fn decode(d: &D) -> Option<T> {
398-
d.read_option(|| Decodable::decode(d))
398+
do d.read_option |b| {
399+
if b {
400+
Some(Decodable::decode(d))
401+
} else {
402+
None
403+
}
404+
}
399405
}
400406
}
401407

0 commit comments

Comments
 (0)