jsonptr icon indicating copy to clipboard operation
jsonptr copied to clipboard

Slice-like Pointers

Open asmello opened this issue 1 year ago • 0 comments

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 Pointer that enables zero-copy usage patterns.
  • const fn Pointer::from_static
  • Zero-allocation Pointer::root
  • Introduced some Quickcheck tests
  • Pointer::split_front and Pointer::split_back
  • Pointer::parent
  • Pointer::strip_suffix

Breaks

  • Original Pointer renamed to PointerBuf
    • All of the original methods are still available, but some were changed:
      • Pointer::root is now just PointerBuf::new
        • NOTE: a static method on the slice type Pointer::root is also available and encouraged as a replacement where possible (as it doesn't allocate)
      • Pointer::new is now PointerBuf::from_tokens
      • Pointer:union is now PointerBuf::intersection (fixes #28)
  • Debug implementation changed to preserve type information (e.g. prints PathBuf("/foo/bar") instead of "/foo/bar")
  • Removed impl Deref<Target=&str> (Pointer is a subtype of str, not the other way around!)

Misc

  • Fixed some warnings from clippy
  • Opportunistic optimizations/simplifications

asmello avatar Mar 31 '24 19:03 asmello