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.
This commit is contained in:
Thomas Bahn
2014-12-23 17:29:09 +01:00
parent cca63836d2
commit 0957d5769d
2 changed files with 12 additions and 13 deletions
+6 -6
View File
@@ -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.