Skip to content

Commit

Permalink
Use pass-by-reference when ownership is not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
rooney committed May 1, 2024
1 parent a3f62bb commit 0ff9352
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ where
/// "location": "Menlo Park, CA"
/// });
///
/// let u: User = serde_json::from_value(j).unwrap();
/// let u: User = serde_json::from_value(&j).unwrap();
/// println!("{:#?}", u);
/// }
/// ```
Expand All @@ -1001,7 +1001,7 @@ where
/// is wrong with the data, for example required struct fields are missing from
/// the JSON map or some number is too big to fit in the expected primitive
/// type.
pub fn from_value<T>(value: Value) -> Result<T, Error>
pub fn from_value<T>(value: &Value) -> Result<T, Error>
where
T: DeserializeOwned,
{
Expand Down
2 changes: 1 addition & 1 deletion tests/regression/issue520.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ enum E {
fn test() {
let e = E::Float(159.1);
let v = serde_json::to_value(e).unwrap();
let e = serde_json::from_value::<E>(v).unwrap();
let e = serde_json::from_value::<E>(&v).unwrap();

match e {
E::Float(f) => assert_eq!(f, 159.1),
Expand Down
2 changes: 1 addition & 1 deletion tests/regression/issue795.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ fn test() {
assert!(serde_json::from_str::<Enum>(s).is_err());

let j = json!({"Variant":{"x":0,"y":0}});
assert!(serde_json::from_value::<Enum>(j).is_err());
assert!(serde_json::from_value::<Enum>(&j).is_err());
}
16 changes: 8 additions & 8 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,11 @@ where
assert_eq!(v, value);

// Make sure we can deserialize from a `Value`.
let v: T = from_value(json_value.clone()).unwrap();
let v: T = from_value(&json_value).unwrap();
assert_eq!(v, value);

// Make sure we can round trip back to `Value`.
let json_value2: Value = from_value(json_value.clone()).unwrap();
let json_value2: Value = from_value(&json_value).unwrap();
assert_eq!(json_value2, json_value);

// Make sure we can fully ignore.
Expand Down Expand Up @@ -1413,10 +1413,10 @@ fn test_missing_option_field() {
let value: Foo = from_str("{\"x\": 5}").unwrap();
assert_eq!(value, Foo { x: Some(5) });

let value: Foo = from_value(json!({})).unwrap();
let value: Foo = from_value(&json!({})).unwrap();
assert_eq!(value, Foo { x: None });

let value: Foo = from_value(json!({"x": 5})).unwrap();
let value: Foo = from_value(&json!({"x": 5})).unwrap();
assert_eq!(value, Foo { x: Some(5) });
}

Expand Down Expand Up @@ -1444,10 +1444,10 @@ fn test_missing_renamed_field() {
let value: Foo = from_str("{\"y\": 5}").unwrap();
assert_eq!(value, Foo { x: Some(5) });

let value: Foo = from_value(json!({})).unwrap();
let value: Foo = from_value(&json!({})).unwrap();
assert_eq!(value, Foo { x: None });

let value: Foo = from_value(json!({"y": 5})).unwrap();
let value: Foo = from_value(&json!({"y": 5})).unwrap();
assert_eq!(value, Foo { x: Some(5) });
}

Expand Down Expand Up @@ -1899,13 +1899,13 @@ fn test_integer_key() {
(r#"{"123 ":null}"#, "expected `\"` at line 1 column 6"),
]);

let err = from_value::<BTreeMap<i32, ()>>(json!({" 123":null})).unwrap_err();
let err = from_value::<BTreeMap<i32, ()>>(&json!({" 123":null})).unwrap_err();
assert_eq!(
err.to_string(),
"invalid value: expected key to be a number in quotes",
);

let err = from_value::<BTreeMap<i32, ()>>(json!({"123 ":null})).unwrap_err();
let err = from_value::<BTreeMap<i32, ()>>(&json!({"123 ":null})).unwrap_err();
assert_eq!(
err.to_string(),
"invalid value: expected key to be a number in quotes",
Expand Down

0 comments on commit 0ff9352

Please sign in to comment.