Skip to content

Commit aa779c1

Browse files
committed
std: change Decoder::read_option to return a generic type
This allows read_option to be used with a custom option type instead of just core::Option.
1 parent ce9e5ec commit aa779c1

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

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)