-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tighten JsonPointer and methods #613
Conversation
Hi, thanks for the contribution it definitely looks like an improvement. However we moved the JSON pointer implementation in recent changes into |
Hey @timothee-haudebourg . |
I've taken a leap and am assuming that #612 felt forced to change the internals of these types from |
Thanks for the effort 😄 It doesn't really matter what internal type is used in the wrapper types, but having the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I've split the verify functions up. I've tried to ensure there is always a path for str/String to become JsonPointers without the utf-8 check being done again.
This means that in general, if you already have a &str
, then from_str_const
is the better method to construct a JsonPointer. And, if you already have a String
, then try_from
is best.
I also changed JsonPointerBuf::deserialize
to use try_into
instead of parse
, as from_str
implied making an extra copy.
The `JsonPointer` type ought to be `#[repr(transparent)]` if `std::mem::transmute` is going to be used to coerce one from a `str`. `ReferenceToken` already did. The `validate` method did not ensure that a string starts with a `/`. This may not have led a direct path to any undefined behavior, because it was ensured that all `~` are followed by a `0` or `1`, but other methods do act as though they assumed a pointer string always starts with a `/`. I think this makes things a bit more clear. The private `token_end` can be mostly replaced by `find("/")`. `as_array_index` can be implemented in terms of the standard library's integer parsing. (Allowing "+000" is an annoying gotcha in seemingly every major language's standard library!). I've added `Deserialize` for `&JsonPointer`. I've made the internals of the new `json_pointer!` macro `const {}` to better ensure that its checks actually happen at compile time. I've adjusted some methods to reduce the total number of `unsafe` blocks necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CI is failing but that's expected and fixed in another PR. Thanks again!
@@ -65,14 +61,36 @@ impl JsonPointer { | |||
/// # Safety | |||
/// | |||
/// The input string *must* be a valid JSON pointer. | |||
pub const unsafe fn new_unchecked(s: &[u8]) -> &Self { | |||
pub const unsafe fn new_unchecked_str(s: &str) -> &Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that was necessary since we can always use s.as_bytes()
even in const
context, but why not.
The
JsonPointer
type ought to be#[repr(transparent)]
ifstd::mem::transmute
is going to be used to coerce one from astr
.ReferenceToken
already did.The
validate
method did not ensure that a string starts with a/
. This may not have led a direct path to any undefined behavior, because it was ensured that all~
are followed by a0
or1
, but other methods do act as though they assumed a pointer string always starts with a/
. I think this makes things a bit more clear.The private
token_end
can be mostly replaced byfind("/")
.as_array_index
can be implemented in terms of the standard library's integer parsing. (Allowing "+000" is an annoying gotcha in seemingly every major language's standard library!).I've added
Deserialize
for&JsonPointer
.I've made the internals of the new
json_pointer!
macroconst {}
to better ensure that its checks actually happen at compile time.I've adjusted some methods to reduce the total number of
unsafe
blocks