mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-14 00:11:00 +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
-7
@@ -9,7 +9,6 @@
|
|||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::str::StrAllocating;
|
|
||||||
|
|
||||||
use json::value::{ToJson, Value};
|
use json::value::{ToJson, Value};
|
||||||
|
|
||||||
@@ -56,20 +55,20 @@ impl ObjectBuilder {
|
|||||||
Value::Object(self.object)
|
Value::Object(self.object)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert<K: StrAllocating, V: ToJson>(self, key: K, value: V) -> ObjectBuilder {
|
pub fn insert<V: ToJson>(self, key: String, value: V) -> ObjectBuilder {
|
||||||
let mut builder = self;
|
let mut builder = self;
|
||||||
builder.object.insert(key.into_string(), value.to_json());
|
builder.object.insert(key, value.to_json());
|
||||||
builder
|
builder
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_array<S: StrAllocating>(self, key: S, f: |ArrayBuilder| -> ArrayBuilder) -> ObjectBuilder {
|
pub fn insert_array(self, key: String, f: |ArrayBuilder| -> ArrayBuilder) -> ObjectBuilder {
|
||||||
let builder = ArrayBuilder::new();
|
let builder = ArrayBuilder::new();
|
||||||
self.insert(key.into_string(), f(builder).unwrap())
|
self.insert(key, f(builder).unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_object<S: StrAllocating>(self, key: S, f: |ObjectBuilder| -> ObjectBuilder) -> ObjectBuilder {
|
pub fn insert_object(self, key: String, f: |ObjectBuilder| -> ObjectBuilder) -> ObjectBuilder {
|
||||||
let builder = ObjectBuilder::new();
|
let builder = ObjectBuilder::new();
|
||||||
self.insert(key.into_string(), f(builder).unwrap())
|
self.insert(key, f(builder).unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+6
-6
@@ -157,8 +157,8 @@ pub struct MyStruct {
|
|||||||
impl ToJson for MyStruct {
|
impl ToJson for MyStruct {
|
||||||
fn to_json( &self ) -> Value {
|
fn to_json( &self ) -> Value {
|
||||||
ObjectBuilder::new()
|
ObjectBuilder::new()
|
||||||
.insert("attr1", &self.attr1)
|
.insert("attr1".to_string(), &self.attr1)
|
||||||
.insert("attr2", &self.attr2)
|
.insert("attr2".to_string(), &self.attr2)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -265,9 +265,9 @@ pub struct TestStruct1 {
|
|||||||
impl ToJson for TestStruct1 {
|
impl ToJson for TestStruct1 {
|
||||||
fn to_json( &self ) -> json::Value {
|
fn to_json( &self ) -> json::Value {
|
||||||
json::builder::ObjectBuilder::new()
|
json::builder::ObjectBuilder::new()
|
||||||
.insert("data_int", &self.data_int)
|
.insert("data_int".to_string(), &self.data_int)
|
||||||
.insert("data_str", &self.data_str)
|
.insert("data_str".to_string(), &self.data_str)
|
||||||
.insert("data_vector", &self.data_vector)
|
.insert("data_vector".to_string(), &self.data_vector)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -281,7 +281,7 @@ fn main() {
|
|||||||
data_vector: vec![2,3,4,5],
|
data_vector: vec![2,3,4,5],
|
||||||
};
|
};
|
||||||
let json: json::Value = test.to_json();
|
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.
|
// Deserialize like before.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user