1
Fork 0

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 ce9e5ecb6c
commit aa779c1240
3 changed files with 17 additions and 11 deletions

View file

@ -411,13 +411,13 @@ pub mod reader {
} }
#[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 @@ pub mod reader {
#[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!(),
} }
} }

View file

@ -980,10 +980,10 @@ impl<'self> serialize::Decoder for Decoder<'self> {
} }
} }
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),
} }
} }
} }

View file

@ -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 @@ impl<S:Encoder,T:Encodable<S>> Encodable<S> for Option<T> {
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
}
}
} }
} }