Skip to content

Commit

Permalink
Merge pull request #1221 from bheylin/add-const-raw-values-for-null-a…
Browse files Browse the repository at this point in the history
…nd-bools

Add literal 'null', 'true' and 'false' consts to `RawValue` struct.
  • Loading branch information
dtolnay authored Dec 11, 2024
2 parents 0903de4 + 4db66fb commit 96576ba
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: &'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) }
}

Expand Down Expand Up @@ -148,7 +155,7 @@ impl ToOwned for RawValue {

impl Default for Box<RawValue> {
fn default() -> Self {
RawValue::from_borrowed("null").to_owned()
RawValue::NULL.to_owned()
}
}

Expand Down

0 comments on commit 96576ba

Please sign in to comment.