From 0957d5769d4deae4cb1a984194fed38fda1688a7 Mon Sep 17 00:00:00 2001 From: Thomas Bahn Date: Tue, 23 Dec 2014 17:29:09 +0100 Subject: [PATCH] Replace StrAllocating with String type in function parameters The key parameter of the insert functions are of type String now. As insert requires ownership of its parameter `key` and StrAllocating being gone in rust these functions now communicate that ownership in their signature. As a result &str must be converted into a String at the caller instead of the callee. The Trait ToOwned isn't an alternative as it's a generalized Clone. StrAllocating simply has hidden an allocation or move. --- src/json/builder.rs | 13 ++++++------- src/json/mod.rs | 12 ++++++------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/json/builder.rs b/src/json/builder.rs index 12081372..3bba0ab8 100644 --- a/src/json/builder.rs +++ b/src/json/builder.rs @@ -9,7 +9,6 @@ // except according to those terms. use std::collections::BTreeMap; -use std::str::StrAllocating; use json::value::{ToJson, Value}; @@ -56,20 +55,20 @@ impl ObjectBuilder { Value::Object(self.object) } - pub fn insert(self, key: K, value: V) -> ObjectBuilder { + pub fn insert(self, key: String, value: V) -> ObjectBuilder { let mut builder = self; - builder.object.insert(key.into_string(), value.to_json()); + builder.object.insert(key, value.to_json()); builder } - pub fn insert_array(self, key: S, f: |ArrayBuilder| -> ArrayBuilder) -> ObjectBuilder { + pub fn insert_array(self, key: String, f: |ArrayBuilder| -> ArrayBuilder) -> ObjectBuilder { let builder = ArrayBuilder::new(); - self.insert(key.into_string(), f(builder).unwrap()) + self.insert(key, f(builder).unwrap()) } - pub fn insert_object(self, key: S, f: |ObjectBuilder| -> ObjectBuilder) -> ObjectBuilder { + pub fn insert_object(self, key: String, f: |ObjectBuilder| -> ObjectBuilder) -> ObjectBuilder { let builder = ObjectBuilder::new(); - self.insert(key.into_string(), f(builder).unwrap()) + self.insert(key, f(builder).unwrap()) } } diff --git a/src/json/mod.rs b/src/json/mod.rs index d9d4da2c..feca55f6 100644 --- a/src/json/mod.rs +++ b/src/json/mod.rs @@ -157,8 +157,8 @@ pub struct MyStruct { impl ToJson for MyStruct { fn to_json( &self ) -> Value { ObjectBuilder::new() - .insert("attr1", &self.attr1) - .insert("attr2", &self.attr2) + .insert("attr1".to_string(), &self.attr1) + .insert("attr2".to_string(), &self.attr2) .unwrap() } } @@ -265,9 +265,9 @@ pub struct TestStruct1 { impl ToJson for TestStruct1 { fn to_json( &self ) -> json::Value { json::builder::ObjectBuilder::new() - .insert("data_int", &self.data_int) - .insert("data_str", &self.data_str) - .insert("data_vector", &self.data_vector) + .insert("data_int".to_string(), &self.data_int) + .insert("data_str".to_string(), &self.data_str) + .insert("data_vector".to_string(), &self.data_vector) .unwrap() } } @@ -281,7 +281,7 @@ fn main() { data_vector: vec![2,3,4,5], }; let json: json::Value = test.to_json(); - let json_str: String = json.to_string().into_string(); + let json_str: String = json.to_string(); // Deserialize like before.