jsonptr
jsonptr copied to clipboard
Slice-like Pointers
Taking a page out of the std::path book, I figured it'd be nice to have most operations using Pointers operate on string slices instead of owned strings. This significantly helps reduce allocations/copies when manipulating this type, and allows for neat things like creating static/const Pointers that can be parsed at compilation time.
The handful of operations requiring &mut have been move to a companion buffer type called PointerBuf, named analogously to std::path::PathBuf. Also to be consistent with PathBuf, it implements Deref<Target=Pointer> so all pointer operations also work on this owned type transparently, and for the most part it can be used where the original Pointer type would.
I did make a few API breaks though. I'm open to iterating on the breaks, and maybe make some compatibility compromises, as not all changes are necessary to support this PR, but I figured this was a good opportunity to more closely align the API to std conventions and make things less surprising / more intuitive.
Since a lot of code was rewritten/simplified to take advantage of immutability and avoid re-allocations, I've also added a few quickcheck tests to generate some confidence that the new implementations work as expected. The original tests have also been preserved; they were just updated to use the new APIs.
Features
- New slice type
Pointerthat enables zero-copy usage patterns. const fn Pointer::from_static- Zero-allocation
Pointer::root - Introduced some Quickcheck tests
Pointer::split_frontandPointer::split_backPointer::parentPointer::strip_suffix
Breaks
- Original
Pointerrenamed toPointerBuf- All of the original methods are still available, but some were changed:
Pointer::rootis now justPointerBuf::new- NOTE: a static method on the slice type
Pointer::rootis also available and encouraged as a replacement where possible (as it doesn't allocate)
- NOTE: a static method on the slice type
Pointer::newis nowPointerBuf::from_tokensPointer:unionis nowPointerBuf::intersection(fixes #28)
- All of the original methods are still available, but some were changed:
- Debug implementation changed to preserve type information (e.g. prints
PathBuf("/foo/bar")instead of"/foo/bar") - Removed
impl Deref<Target=&str>(Pointeris a subtype ofstr, not the other way around!)
Misc
- Fixed some warnings from clippy
- Opportunistic optimizations/simplifications