From e03b9665f4736034134664717e38dab43f7e5631 Mon Sep 17 00:00:00 2001 From: Sam Delmerico Date: Tue, 12 Jul 2016 00:40:53 -0400 Subject: [PATCH] added into_* variations for as_object, as_array Allows a the destructuring of a Json type with move semantics. For example, let json_value = Json::from_str("{}").unwrap(); let json_object = (|json: Json| { json.as_object() })(json_value); assert!(json_object.is_some()); if a Json type is passed to a closure, or some other scope block, when trying to destructure it, there is a lifetime conflict. The Json given as a parameter to the closure was moved, but it cannot be moved out of the closure as an Object. If you replace as_object with into_object, the Object value returned is not a reference, so it can be moved out of the scope. --- src/json.rs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/json.rs b/src/json.rs index 44bf6a2..6026273 100644 --- a/src/json.rs +++ b/src/json.rs @@ -1023,7 +1023,7 @@ impl Json { self.as_object().is_some() } - /// If the Json value is an Object, returns the associated BTreeMap. + /// If the Json value is an Object, returns a reference to the associated BTreeMap. /// Returns None otherwise. pub fn as_object<'a>(&'a self) -> Option<&'a Object> { match self { @@ -1032,7 +1032,7 @@ impl Json { } } - /// If the Json value is an Object, returns the associated mutable BTreeMap. + /// If the Json value is an Object, returns a mutable reference to the associated BTreeMap. /// Returns None otherwise. pub fn as_object_mut<'a>(&'a mut self) -> Option<&'a mut Object> { match self { @@ -1041,12 +1041,21 @@ impl Json { } } + /// If the Json value is an Object, returns the associated BTreeMap. + /// Returns None otherwise. + pub fn into_object(self) -> Option { + match self { + Json::Object(map) => Some(map), + _ => None + } + } + /// Returns true if the Json value is an Array. Returns false otherwise. pub fn is_array<'a>(&'a self) -> bool { self.as_array().is_some() } - /// If the Json value is an Array, returns the associated vector. + /// If the Json value is an Array, returns a reference to the associated vector. /// Returns None otherwise. pub fn as_array<'a>(&'a self) -> Option<&'a Array> { match self { @@ -1055,7 +1064,7 @@ impl Json { } } - /// If the Json value is an Array, returns the associated mutable vector. + /// If the Json value is an Array, returns a mutable reference to the associated vector. /// Returns None otherwise. pub fn as_array_mut<'a>(&'a mut self) -> Option<&'a mut Array> { match self { @@ -1064,6 +1073,15 @@ impl Json { } } + /// If the Json value is an Array, returns the associated vector. + /// Returns None otherwise. + pub fn into_array(self) -> Option { + match self { + Json::Array(array) => Some(array), + _ => None + } + } + /// Returns true if the Json value is a String. Returns false otherwise. pub fn is_string<'a>(&'a self) -> bool { self.as_string().is_some() @@ -3426,7 +3444,7 @@ mod tests { _ => {} // it parsed and we are good to go } } - + #[test] fn test_negative_zero() { Json::from_str("{\"test\":-0}").unwrap();