From 9c9aa1f380066e0318b829ec4138d1d1b4ef6949 Mon Sep 17 00:00:00 2001 From: Brian Heylin <3947+bheylin@users.noreply.github.com> Date: Wed, 11 Dec 2024 09:10:27 +0100 Subject: [PATCH 1/8] Add literal 'null', 'true' and 'false' consts to `RawValue` struct. --- src/raw.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/raw.rs b/src/raw.rs index 22d14441e..3b53d150b 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -119,7 +119,14 @@ pub struct RawValue { } impl RawValue { - fn from_borrowed(json: &str) -> &Self { + /// A literal JSON null value as `RawValue`. + pub const NULL: &RawValue = RawValue::from_borrowed("null"); + /// A literal JSON boolean true value as `RawValue`. + pub const TRUE: &RawValue = RawValue::from_borrowed("true"); + /// A literal JSON boolean false value as `RawValue`. + pub const FALSE: &RawValue = RawValue::from_borrowed("false"); + + const fn from_borrowed(json: &str) -> &Self { unsafe { mem::transmute::<&str, &RawValue>(json) } } @@ -148,7 +155,7 @@ impl ToOwned for RawValue { impl Default for Box { fn default() -> Self { - RawValue::from_borrowed("null").to_owned() + RawValue::NULL.to_owned() } } From 4db66fb0b21b3ac1932d35317e9bcdff14b716f3 Mon Sep 17 00:00:00 2001 From: Brian Heylin <3947+bheylin@users.noreply.github.com> Date: Wed, 11 Dec 2024 10:14:15 +0100 Subject: [PATCH 2/8] Add `'static` lifetime to `const`'s --- src/raw.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/raw.rs b/src/raw.rs index 3b53d150b..3d45ff3e7 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -120,11 +120,11 @@ pub struct RawValue { impl RawValue { /// A literal JSON null value as `RawValue`. - pub const NULL: &RawValue = RawValue::from_borrowed("null"); + pub const NULL: &'static RawValue = RawValue::from_borrowed("null"); /// A literal JSON boolean true value as `RawValue`. - pub const TRUE: &RawValue = RawValue::from_borrowed("true"); + pub const TRUE: &'static RawValue = RawValue::from_borrowed("true"); /// A literal JSON boolean false value as `RawValue`. - pub const FALSE: &RawValue = RawValue::from_borrowed("false"); + pub const FALSE: &'static RawValue = RawValue::from_borrowed("false"); const fn from_borrowed(json: &str) -> &Self { unsafe { mem::transmute::<&str, &RawValue>(json) } From f42c7c760b5e38316c6c9090f80a925491889b4b Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 11 Dec 2024 10:58:22 -0800 Subject: [PATCH 3/8] Move RawValue associated constants into same impl block as public functions --- src/raw.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/raw.rs b/src/raw.rs index 3d45ff3e7..0eeb4c46f 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -119,13 +119,6 @@ pub struct RawValue { } impl RawValue { - /// A literal JSON null value as `RawValue`. - pub const NULL: &'static RawValue = RawValue::from_borrowed("null"); - /// A literal JSON boolean true value as `RawValue`. - pub const TRUE: &'static RawValue = RawValue::from_borrowed("true"); - /// A literal JSON boolean false value as `RawValue`. - pub const FALSE: &'static RawValue = RawValue::from_borrowed("false"); - const fn from_borrowed(json: &str) -> &Self { unsafe { mem::transmute::<&str, &RawValue>(json) } } @@ -175,6 +168,13 @@ impl Display for RawValue { } impl RawValue { + /// A literal JSON null value as `RawValue`. + pub const NULL: &'static RawValue = RawValue::from_borrowed("null"); + /// A literal JSON boolean true value as `RawValue`. + pub const TRUE: &'static RawValue = RawValue::from_borrowed("true"); + /// A literal JSON boolean false value as `RawValue`. + pub const FALSE: &'static RawValue = RawValue::from_borrowed("false"); + /// Convert an owned `String` of JSON data to an owned `RawValue`. /// /// This function is equivalent to `serde_json::from_str::>` From 9875785f24c30c990942e3412b3feb8f798bb4dc Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 21 Dec 2024 10:51:05 -0800 Subject: [PATCH 4/8] Tweak wording of NULL/TRUE/FALSE documentation --- src/raw.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/raw.rs b/src/raw.rs index 0eeb4c46f..f81d943a5 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -168,11 +168,11 @@ impl Display for RawValue { } impl RawValue { - /// A literal JSON null value as `RawValue`. + /// A constant RawValue with the JSON value `null`. pub const NULL: &'static RawValue = RawValue::from_borrowed("null"); - /// A literal JSON boolean true value as `RawValue`. + /// A constant RawValue with the JSON value `true`. pub const TRUE: &'static RawValue = RawValue::from_borrowed("true"); - /// A literal JSON boolean false value as `RawValue`. + /// A constant RawValue with the JSON value `false`. pub const FALSE: &'static RawValue = RawValue::from_borrowed("false"); /// Convert an owned `String` of JSON data to an owned `RawValue`. From b2a1415aad0c76684c675d13a5746850d275c178 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 21 Dec 2024 10:54:55 -0800 Subject: [PATCH 5/8] Release 1.0.134 --- Cargo.toml | 2 +- src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4d9e82c7c..a2dda353e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "serde_json" -version = "1.0.133" +version = "1.0.134" authors = ["Erick Tryzelaar ", "David Tolnay "] categories = ["encoding", "parser-implementations", "no-std"] description = "A JSON serialization file format" diff --git a/src/lib.rs b/src/lib.rs index b98cad696..87d040f84 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -299,7 +299,7 @@ //! [macro]: crate::json //! [`serde-json-core`]: https://github.com/rust-embedded-community/serde-json-core -#![doc(html_root_url = "https://docs.rs/serde_json/1.0.133")] +#![doc(html_root_url = "https://docs.rs/serde_json/1.0.134")] // Ignored clippy lints #![allow( clippy::collapsible_else_if, From 1e77cac742aaa12d0c8390bd8d40e279e05a3bca Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 26 Dec 2024 18:30:03 -0800 Subject: [PATCH 6/8] Resolve precedence clippy lint warning: operator precedence can trip the unwary --> src/read.rs:963:18 | 963 | let n = (((n1 - 0xD800) as u32) << 10 | (n2 - 0xDC00) as u32) + 0x1_0000; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider parenthesizing your expression: `(((n1 - 0xD800) as u32) << 10) | (n2 - 0xDC00) as u32` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#precedence = note: `-W clippy::precedence` implied by `-W clippy::all` = help: to override `-W clippy::all` add `#[allow(clippy::precedence)]` warning: operator precedence can trip the unwary --> src/read.rs:991:28 | 991 | ptr.write((n >> 6 & 0b0001_1111) as u8 | 0b1100_0000); | ^^^^^^^^^^^^^^^^^^^^ help: consider parenthesizing your expression: `(n >> 6) & 0b0001_1111` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#precedence warning: operator precedence can trip the unwary --> src/read.rs:995:28 | 995 | ptr.write((n >> 12 & 0b0000_1111) as u8 | 0b1110_0000); | ^^^^^^^^^^^^^^^^^^^^^ help: consider parenthesizing your expression: `(n >> 12) & 0b0000_1111` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#precedence warning: operator precedence can trip the unwary --> src/read.rs:996:35 | 996 | ptr.add(1).write((n >> 6 & 0b0011_1111) as u8 | 0b1000_0000); | ^^^^^^^^^^^^^^^^^^^^ help: consider parenthesizing your expression: `(n >> 6) & 0b0011_1111` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#precedence warning: operator precedence can trip the unwary --> src/read.rs:1000:28 | 1000 | ptr.write((n >> 18 & 0b0000_0111) as u8 | 0b1111_0000); | ^^^^^^^^^^^^^^^^^^^^^ help: consider parenthesizing your expression: `(n >> 18) & 0b0000_0111` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#precedence warning: operator precedence can trip the unwary --> src/read.rs:1002:29 | 1002 | .write((n >> 12 & 0b0011_1111) as u8 | 0b1000_0000); | ^^^^^^^^^^^^^^^^^^^^^ help: consider parenthesizing your expression: `(n >> 12) & 0b0011_1111` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#precedence warning: operator precedence can trip the unwary --> src/read.rs:1003:35 | 1003 | ptr.add(2).write((n >> 6 & 0b0011_1111) as u8 | 0b1000_0000); | ^^^^^^^^^^^^^^^^^^^^ help: consider parenthesizing your expression: `(n >> 6) & 0b0011_1111` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#precedence --- src/read.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/read.rs b/src/read.rs index eaa46996b..b4128467b 100644 --- a/src/read.rs +++ b/src/read.rs @@ -960,7 +960,7 @@ fn parse_unicode_escape<'de, R: Read<'de>>( // This value is in range U+10000..=U+10FFFF, which is always a valid // codepoint. - let n = (((n1 - 0xD800) as u32) << 10 | (n2 - 0xDC00) as u32) + 0x1_0000; + let n = ((((n1 - 0xD800) as u32) << 10) | (n2 - 0xDC00) as u32) + 0x1_0000; push_wtf8_codepoint(n, scratch); return Ok(()); } @@ -988,19 +988,21 @@ fn push_wtf8_codepoint(n: u32, scratch: &mut Vec) { let encoded_len = match n { 0..=0x7F => unreachable!(), 0x80..=0x7FF => { - ptr.write((n >> 6 & 0b0001_1111) as u8 | 0b1100_0000); + ptr.write(((n >> 6) & 0b0001_1111) as u8 | 0b1100_0000); 2 } 0x800..=0xFFFF => { - ptr.write((n >> 12 & 0b0000_1111) as u8 | 0b1110_0000); - ptr.add(1).write((n >> 6 & 0b0011_1111) as u8 | 0b1000_0000); + ptr.write(((n >> 12) & 0b0000_1111) as u8 | 0b1110_0000); + ptr.add(1) + .write(((n >> 6) & 0b0011_1111) as u8 | 0b1000_0000); 3 } 0x1_0000..=0x10_FFFF => { - ptr.write((n >> 18 & 0b0000_0111) as u8 | 0b1111_0000); + ptr.write(((n >> 18) & 0b0000_0111) as u8 | 0b1111_0000); ptr.add(1) - .write((n >> 12 & 0b0011_1111) as u8 | 0b1000_0000); - ptr.add(2).write((n >> 6 & 0b0011_1111) as u8 | 0b1000_0000); + .write(((n >> 12) & 0b0011_1111) as u8 | 0b1000_0000); + ptr.add(2) + .write(((n >> 6) & 0b0011_1111) as u8 | 0b1000_0000); 4 } 0x11_0000.. => unreachable!(), From d48c224d12a18189bdabbfe050bfe8134dbb4bf5 Mon Sep 17 00:00:00 2001 From: tison Date: Tue, 7 Jan 2025 08:10:53 +0800 Subject: [PATCH 7/8] Add Map::into_values method Signed-off-by: tison --- src/map.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/map.rs b/src/map.rs index bc9a9307b..126e8001a 100644 --- a/src/map.rs +++ b/src/map.rs @@ -338,6 +338,14 @@ impl Map { } } + /// Gets an iterator over the values of the map. + #[inline] + pub fn into_values(self) -> IntoValues { + IntoValues { + iter: self.map.into_values(), + } + } + /// Retains only the elements specified by the predicate. /// /// In other words, remove all pairs `(k, v)` such that `f(&k, &mut v)` @@ -1155,3 +1163,17 @@ type ValuesMutImpl<'a> = btree_map::ValuesMut<'a, String, Value>; type ValuesMutImpl<'a> = indexmap::map::ValuesMut<'a, String, Value>; delegate_iterator!((ValuesMut<'a>) => &'a mut Value); + +////////////////////////////////////////////////////////////////////////////// + +/// An owning iterator over a serde_json::Map's values. +pub struct IntoValues { + iter: IntoValuesImpl, +} + +#[cfg(not(feature = "preserve_order"))] +type IntoValuesImpl = btree_map::IntoValues; +#[cfg(feature = "preserve_order")] +type IntoValuesImpl = indexmap::map::IntoValues; + +delegate_iterator!((IntoValues) => Value); From 9802c08d4ef1662cbbf92fabf7d6f4dc6aecfe9e Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 6 Jan 2025 16:21:47 -0800 Subject: [PATCH 8/8] Release 1.0.135 --- Cargo.toml | 2 +- src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a2dda353e..23ad063e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "serde_json" -version = "1.0.134" +version = "1.0.135" authors = ["Erick Tryzelaar ", "David Tolnay "] categories = ["encoding", "parser-implementations", "no-std"] description = "A JSON serialization file format" diff --git a/src/lib.rs b/src/lib.rs index 87d040f84..60a26ca8c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -299,7 +299,7 @@ //! [macro]: crate::json //! [`serde-json-core`]: https://github.com/rust-embedded-community/serde-json-core -#![doc(html_root_url = "https://docs.rs/serde_json/1.0.134")] +#![doc(html_root_url = "https://docs.rs/serde_json/1.0.135")] // Ignored clippy lints #![allow( clippy::collapsible_else_if,