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.
This commit is contained in:
Erick Tryzelaar 2013-03-28 13:11:14 -07:00
parent bdf81e1184
commit b05e148dc9
3 changed files with 17 additions and 11 deletions

View File

@ -411,13 +411,13 @@ fn read_tup_elt<T>(&self, idx: uint, f: &fn() -> T) -> T {
}
#[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()");
do self.read_enum("Option") || {
do self.read_enum_variant |idx| {
match idx {
0 => None,
1 => Some(f()),
0 => f(false),
1 => f(true),
_ => fail!(),
}
}
@ -427,13 +427,13 @@ fn read_option<T>(&self, f: &fn() -> T) -> Option<T> {
#[cfg(stage1)]
#[cfg(stage2)]
#[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()");
do self.read_enum("Option") || {
do self.read_enum_variant(["None", "Some"]) |idx| {
match idx {
0 => None,
1 => Some(f()),
0 => f(false),
1 => f(true),
_ => fail!(),
}
}

View File

@ -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() {
Null => { self.pop(); None }
_ => Some(f()),
Null => { self.pop(); f(false) }
_ => f(true),
}
}
}

View File

@ -118,7 +118,7 @@ pub trait Decoder {
fn read_tup_elt<T>(&self, idx: uint, f: &fn() -> T) -> T;
// 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> {
@ -395,7 +395,13 @@ fn encode(&self, s: &S) {
impl<D:Decoder,T:Decodable<D>> Decodable<D> for 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
}
}
}
}