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:
parent
bdf81e1184
commit
b05e148dc9
3 changed files with 17 additions and 11 deletions
|
@ -411,13 +411,13 @@ pub mod reader {
|
|||
}
|
||||
|
||||
#[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 @@ pub mod reader {
|
|||
#[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!(),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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() {
|
||||
Null => { self.pop(); None }
|
||||
_ => Some(f()),
|
||||
Null => { self.pop(); f(false) }
|
||||
_ => f(true),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 @@ impl<S:Encoder,T:Encodable<S>> Encodable<S> for Option<T> {
|
|||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue