提交 7a6cd2b2 编写于 作者: B bors

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.
...@@ -64,6 +64,7 @@ pub unsafe fn fail_borrowed() { ...@@ -64,6 +64,7 @@ pub unsafe fn fail_borrowed() {
// FIXME #4942: Make these signatures agree with exchange_alloc's signatures // FIXME #4942: Make these signatures agree with exchange_alloc's signatures
#[lang="exchange_malloc"] #[lang="exchange_malloc"]
#[inline(always)]
pub unsafe fn exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char { pub unsafe fn exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char {
transmute(exchange_alloc::malloc(transmute(td), transmute(size))) transmute(exchange_alloc::malloc(transmute(td), transmute(size)))
} }
...@@ -72,11 +73,13 @@ pub unsafe fn exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char { ...@@ -72,11 +73,13 @@ pub unsafe fn exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char {
// inside a landing pad may corrupt the state of the exception handler. If a // inside a landing pad may corrupt the state of the exception handler. If a
// problem occurs, call exit instead. // problem occurs, call exit instead.
#[lang="exchange_free"] #[lang="exchange_free"]
#[inline(always)]
pub unsafe fn exchange_free(ptr: *c_char) { pub unsafe fn exchange_free(ptr: *c_char) {
exchange_alloc::free(transmute(ptr)) exchange_alloc::free(transmute(ptr))
} }
#[lang="malloc"] #[lang="malloc"]
#[inline(always)]
pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char { pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char {
return rustrt::rust_upcall_malloc(td, size); return rustrt::rust_upcall_malloc(td, size);
} }
...@@ -85,6 +88,7 @@ pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char { ...@@ -85,6 +88,7 @@ pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char {
// inside a landing pad may corrupt the state of the exception handler. If a // inside a landing pad may corrupt the state of the exception handler. If a
// problem occurs, call exit instead. // problem occurs, call exit instead.
#[lang="free"] #[lang="free"]
#[inline(always)]
pub unsafe fn local_free(ptr: *c_char) { pub unsafe fn local_free(ptr: *c_char) {
rustrt::rust_upcall_free(ptr); rustrt::rust_upcall_free(ptr);
} }
...@@ -117,6 +121,7 @@ pub unsafe fn check_not_borrowed(a: *u8) { ...@@ -117,6 +121,7 @@ pub unsafe fn check_not_borrowed(a: *u8) {
} }
#[lang="strdup_uniq"] #[lang="strdup_uniq"]
#[inline(always)]
pub unsafe fn strdup_uniq(ptr: *c_uchar, len: uint) -> ~str { pub unsafe fn strdup_uniq(ptr: *c_uchar, len: uint) -> ~str {
str::raw::from_buf_len(ptr, len) str::raw::from_buf_len(ptr, len)
} }
......
...@@ -411,13 +411,13 @@ fn read_tup_elt<T>(&self, idx: uint, f: &fn() -> T) -> T { ...@@ -411,13 +411,13 @@ fn read_tup_elt<T>(&self, idx: uint, f: &fn() -> T) -> T {
} }
#[cfg(stage0)] #[cfg(stage0)]
fn read_option<T>(&self, f: &fn() -> T) -> Option<T> { fn read_option<T>(&self, f: &fn(bool) -> T) -> T {
debug!("read_option()"); debug!("read_option()");
do self.read_enum("Option") || { do self.read_enum("Option") || {
do self.read_enum_variant |idx| { do self.read_enum_variant |idx| {
match idx { match idx {
0 => None, 0 => f(false),
1 => Some(f()), 1 => f(true),
_ => fail!(), _ => fail!(),
} }
} }
...@@ -427,13 +427,13 @@ fn read_option<T>(&self, f: &fn() -> T) -> Option<T> { ...@@ -427,13 +427,13 @@ fn read_option<T>(&self, f: &fn() -> T) -> Option<T> {
#[cfg(stage1)] #[cfg(stage1)]
#[cfg(stage2)] #[cfg(stage2)]
#[cfg(stage3)] #[cfg(stage3)]
fn read_option<T>(&self, f: &fn() -> T) -> Option<T> { fn read_option<T>(&self, f: &fn(bool) -> T) -> T {
debug!("read_option()"); debug!("read_option()");
do self.read_enum("Option") || { do self.read_enum("Option") || {
do self.read_enum_variant(["None", "Some"]) |idx| { do self.read_enum_variant(["None", "Some"]) |idx| {
match idx { match idx {
0 => None, 0 => f(false),
1 => Some(f()), 1 => f(true),
_ => fail!(), _ => fail!(),
} }
} }
......
...@@ -980,10 +980,10 @@ fn read_tup_elt<T>(&self, idx: uint, f: &fn() -> T) -> T { ...@@ -980,10 +980,10 @@ fn read_tup_elt<T>(&self, idx: uint, f: &fn() -> T) -> T {
} }
} }
fn read_option<T>(&self, f: &fn() -> T) -> Option<T> { fn read_option<T>(&self, f: &fn(bool) -> T) -> T {
match *self.peek() { match *self.peek() {
Null => { self.pop(); None } Null => { self.pop(); f(false) }
_ => Some(f()), _ => f(true),
} }
} }
} }
......
...@@ -118,7 +118,7 @@ pub trait Decoder { ...@@ -118,7 +118,7 @@ pub trait Decoder {
fn read_tup_elt<T>(&self, idx: uint, f: &fn() -> T) -> T; fn read_tup_elt<T>(&self, idx: uint, f: &fn() -> T) -> T;
// Specialized types: // Specialized types:
fn read_option<T>(&self, f: &fn() -> T) -> Option<T>; fn read_option<T>(&self, f: &fn(bool) -> T) -> T;
} }
pub trait Encodable<S:Encoder> { pub trait Encodable<S:Encoder> {
...@@ -395,7 +395,13 @@ fn encode(&self, s: &S) { ...@@ -395,7 +395,13 @@ fn encode(&self, s: &S) {
impl<D:Decoder,T:Decodable<D>> Decodable<D> for Option<T> { impl<D:Decoder,T:Decodable<D>> Decodable<D> for Option<T> {
fn decode(d: &D) -> Option<T> { fn decode(d: &D) -> Option<T> {
d.read_option(|| Decodable::decode(d)) do d.read_option |b| {
if b {
Some(Decodable::decode(d))
} else {
None
}
}
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册