From c9ea7c9a58bc2bcfe77d264cedaeca5a3296634a Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 15 Dec 2014 09:06:06 -0800 Subject: [PATCH] serialize: Change some FnOnce bounds to FnMut Relax some of the bounds on the decoder methods back to FnMut to help accomodate some more flavorful variants of decoders which may need to run the closure more than once when it, for example, attempts to find the first successful enum to decode. This a breaking change due to the bounds for the trait switching, and clients will need to update from `FnOnce` to `FnMut` as well as likely making the local function binding mutable in order to call the function. [breaking-change] --- src/librbml/lib.rs | 14 ++++++++------ src/libserialize/json.rs | 11 ++++++----- src/libserialize/serialize.rs | 6 +++--- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/librbml/lib.rs b/src/librbml/lib.rs index 28d5e1812f090..bbedbc75395d6 100644 --- a/src/librbml/lib.rs +++ b/src/librbml/lib.rs @@ -499,8 +499,9 @@ pub mod reader { Ok(result) } - fn read_enum_variant(&mut self, _: &[&str], f: F) -> DecodeResult where - F: FnOnce(&mut Decoder<'doc>, uint) -> DecodeResult, + fn read_enum_variant(&mut self, _: &[&str], + mut f: F) -> DecodeResult + where F: FnMut(&mut Decoder<'doc>, uint) -> DecodeResult, { debug!("read_enum_variant()"); let idx = try!(self._next_uint(EsEnumVid)); @@ -526,8 +527,9 @@ pub mod reader { f(self) } - fn read_enum_struct_variant(&mut self, _: &[&str], f: F) -> DecodeResult where - F: FnOnce(&mut Decoder<'doc>, uint) -> DecodeResult, + fn read_enum_struct_variant(&mut self, _: &[&str], + mut f: F) -> DecodeResult + where F: FnMut(&mut Decoder<'doc>, uint) -> DecodeResult, { debug!("read_enum_struct_variant()"); let idx = try!(self._next_uint(EsEnumVid)); @@ -610,8 +612,8 @@ pub mod reader { self.read_tuple_arg(idx, f) } - fn read_option(&mut self, f: F) -> DecodeResult where - F: FnOnce(&mut Decoder<'doc>, bool) -> DecodeResult, + fn read_option(&mut self, mut f: F) -> DecodeResult where + F: FnMut(&mut Decoder<'doc>, bool) -> DecodeResult, { debug!("read_option()"); self.read_enum("Option", move |this| { diff --git a/src/libserialize/json.rs b/src/libserialize/json.rs index d34828ccee328..6047c76d093eb 100644 --- a/src/libserialize/json.rs +++ b/src/libserialize/json.rs @@ -2082,8 +2082,9 @@ impl ::Decoder for Decoder { f(self) } - fn read_enum_variant(&mut self, names: &[&str], f: F) -> DecodeResult where - F: FnOnce(&mut Decoder, uint) -> DecodeResult, + fn read_enum_variant(&mut self, names: &[&str], + mut f: F) -> DecodeResult + where F: FnMut(&mut Decoder, uint) -> DecodeResult, { debug!("read_enum_variant(names={})", names); let name = match self.pop() { @@ -2133,7 +2134,7 @@ impl ::Decoder for Decoder { } fn read_enum_struct_variant(&mut self, names: &[&str], f: F) -> DecodeResult where - F: FnOnce(&mut Decoder, uint) -> DecodeResult, + F: FnMut(&mut Decoder, uint) -> DecodeResult, { debug!("read_enum_struct_variant(names={})", names); self.read_enum_variant(names, f) @@ -2230,8 +2231,8 @@ impl ::Decoder for Decoder { self.read_tuple_arg(idx, f) } - fn read_option(&mut self, f: F) -> DecodeResult where - F: FnOnce(&mut Decoder, bool) -> DecodeResult, + fn read_option(&mut self, mut f: F) -> DecodeResult where + F: FnMut(&mut Decoder, bool) -> DecodeResult, { debug!("read_option()"); match self.pop() { diff --git a/src/libserialize/serialize.rs b/src/libserialize/serialize.rs index 98bd2f6bc930f..0e0d3b4115bd7 100644 --- a/src/libserialize/serialize.rs +++ b/src/libserialize/serialize.rs @@ -120,12 +120,12 @@ pub trait Decoder { F: FnOnce(&mut Self) -> Result; fn read_enum_variant(&mut self, names: &[&str], f: F) -> Result where - F: FnOnce(&mut Self, uint) -> Result; + F: FnMut(&mut Self, uint) -> Result; fn read_enum_variant_arg(&mut self, a_idx: uint, f: F) -> Result where F: FnOnce(&mut Self) -> Result; fn read_enum_struct_variant(&mut self, names: &[&str], f: F) -> Result where - F: FnOnce(&mut Self, uint) -> Result; + F: FnMut(&mut Self, uint) -> Result; fn read_enum_struct_variant_field(&mut self, &f_name: &str, f_idx: uint, @@ -154,7 +154,7 @@ pub trait Decoder { // Specialized types: fn read_option(&mut self, f: F) -> Result where - F: FnOnce(&mut Self, bool) -> Result; + F: FnMut(&mut Self, bool) -> Result; fn read_seq(&mut self, f: F) -> Result where F: FnOnce(&mut Self, uint) -> Result;