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/2] 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/2] 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) }