mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-23 14:08:06 +00:00
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:
+6
-6
@@ -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.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user