From 3d52ca8fb39a22b0f76137538169c11f91763354 Mon Sep 17 00:00:00 2001 From: Chance Date: Mon, 1 Jul 2024 11:12:21 -0400 Subject: [PATCH] hopefully fixes std feature by marking serde_json optional --- Cargo.toml | 2 +- src/pointer.rs | 43 ++++++++++++++++++++----------------------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2e29865..3e2a9cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,5 +27,5 @@ delete = ["resolve"] impl = [] json = ["dep:serde_json", "serde"] resolve = [] -std = ["serde/std", "serde_json/std"] +std = ["serde/std", "serde_json?/std"] toml = ["dep:toml", "serde", "std"] diff --git a/src/pointer.rs b/src/pointer.rs index f42cd2f..2919063 100644 --- a/src/pointer.rs +++ b/src/pointer.rs @@ -52,13 +52,21 @@ impl Pointer { /// /// This is a zero-copy constructor fn new + ?Sized>(s: &S) -> &Self { - unsafe { &*(core::ptr::from_ref::(s.as_ref()) as *const Self) } + // unsafe { &*(core::ptr::from_ref::(s.as_ref()) as *const Self) } + #[allow(clippy::ref_as_ptr)] + unsafe { + &*(s.as_ref() as *const str as *const Self) + } } /// Constant reference to a root pointer - + pub const fn root() -> &'static Self { - unsafe { &*(core::ptr::from_ref::("") as *const Self) } + // unsafe { &*(core::ptr::from_ref::("") as *const Self) } + #[allow(clippy::ref_as_ptr)] + unsafe { + &*("" as *const str as *const Self) + } } /// Attempts to parse a string into a `Pointer`. @@ -88,26 +96,27 @@ impl Pointer { /// let bar = data.resolve(POINTER).unwrap(); /// assert_eq!(bar, "baz"); /// ```` - + pub const fn from_static(s: &'static str) -> &'static Self { assert!(validate(s).is_ok(), "invalid json pointer"); - unsafe { &*(core::ptr::from_ref::(s) as *const Self) } + // unsafe { &*(core::ptr::from_ref::(s) as *const Self) } + #[allow(clippy::ref_as_ptr)] + unsafe { + &*(s as *const str as *const Self) + } } /// The encoded string representation of this `Pointer` - pub fn as_str(&self) -> &str { &self.0 } /// Converts into an owned [`PointerBuf`] - pub fn to_buf(&self) -> PointerBuf { PointerBuf(self.0.to_string()) } /// Returns an iterator of `Token`s in the `Pointer`. - pub fn tokens(&self) -> Tokens { let mut s = self.0.split('/'); // skipping the first '/' @@ -116,26 +125,22 @@ impl Pointer { } /// Returns the number of tokens in the `Pointer`. - pub fn count(&self) -> usize { self.tokens().count() } /// Returns `true` if the JSON Pointer equals `""`. - pub fn is_root(&self) -> bool { self.0.is_empty() } /// Returns a `serde_json::Value` representation of this `Pointer` - #[cfg(feature = "json")] pub fn to_json_value(&self) -> serde_json::Value { serde_json::Value::String(self.0.to_string()) } /// Returns the last `Token` in the `Pointer`. - pub fn back(&self) -> Option { self.0 .rsplit_once('/') @@ -145,13 +150,11 @@ impl Pointer { /// Returns the last token in the `Pointer`. /// /// alias for `back` - pub fn last(&self) -> Option { self.back() } /// Returns the first `Token` in the `Pointer`. - pub fn front(&self) -> Option { if self.is_root() { return None; @@ -168,13 +171,11 @@ impl Pointer { /// Returns the first `Token` in the `Pointer`. /// /// alias for `front` - pub fn first(&self) -> Option { self.front() } /// Splits the `Pointer` into the first `Token` and a remainder `Pointer`. - pub fn split_front(&self) -> Option<(Token, &Self)> { if self.is_root() { return None; @@ -215,7 +216,6 @@ impl Pointer { /// assert_eq!(tail, Pointer::from_static("/bar/baz")); /// assert_eq!(ptr.split_at(3), None); /// ``` - pub fn split_at(&self, idx: usize) -> Option<(&Self, &Self)> { if self.0.as_bytes().get(idx).copied() != Some(b'/') { return None; @@ -225,7 +225,6 @@ impl Pointer { } /// Splits the `Pointer` into the parent path and the last `Token`. - pub fn split_back(&self) -> Option<(&Self, Token)> { self.0 .rsplit_once('/') @@ -233,7 +232,6 @@ impl Pointer { } /// A pointer to the parent of the current path. - pub fn parent(&self) -> Option<&Self> { self.0.rsplit_once('/').map(|(front, _)| Self::new(front)) } @@ -258,7 +256,7 @@ impl Pointer { /// let ptr = Pointer::root(); /// assert_eq!(ptr.get(0), None); /// ``` - + pub fn get(&self, index: usize) -> Option { self.tokens().nth(index).clone() } @@ -309,7 +307,6 @@ impl Pointer { } /// Finds the commonality between this and another `Pointer`. - pub fn intersection<'a>(&'a self, other: &Self) -> &'a Self { if self.is_root() || other.is_root() { return Self::root(); @@ -587,7 +584,7 @@ pub struct PointerBuf(String); impl PointerBuf { /// Creates a new `PointerBuf` pointing to a document root. - + pub fn new() -> Self { Self(String::new()) } @@ -614,7 +611,7 @@ impl PointerBuf { } /// Coerces to a Pointer slice. - + pub fn as_ptr(&self) -> &Pointer { self }