From b5b1244857b6f529b5b079bb1b72f2bd2e10da42 Mon Sep 17 00:00:00 2001 From: Paul Masurel Date: Thu, 7 Oct 2021 18:14:49 +0900 Subject: [PATCH] More functionality in the ownedbytes crate (#1172) --- Cargo.toml | 2 +- common/Cargo.toml | 2 +- ownedbytes/Cargo.toml | 2 +- ownedbytes/src/lib.rs | 52 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d51b4b60fc..2e31f74737 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,7 +37,7 @@ tantivy-query-grammar = { version="0.15.0", path="./query-grammar" } tantivy-bitpacker = { version="0.1", path="./bitpacker" } common = { version = "0.1", path = "./common/", package = "tantivy-common" } fastfield_codecs = { version="0.1", path="./fastfield_codecs", default-features = false } -ownedbytes = { version="0.1", path="./ownedbytes" } +ownedbytes = { version="0.2", path="./ownedbytes" } stable_deref_trait = "1.2" rust-stemmers = "1.2" downcast-rs = "1.2" diff --git a/common/Cargo.toml b/common/Cargo.toml index 1a6703c1ed..708629a0e5 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -10,7 +10,7 @@ description = "common traits and utility functions used by multiple tantivy subc [dependencies] byteorder = "1.4.3" -ownedbytes = { version="0.1", path="../ownedbytes" } +ownedbytes = { version="0.2", path="../ownedbytes" } [dev-dependencies] proptest = "1.0.0" diff --git a/ownedbytes/Cargo.toml b/ownedbytes/Cargo.toml index dd9556c3c1..b73379b659 100644 --- a/ownedbytes/Cargo.toml +++ b/ownedbytes/Cargo.toml @@ -1,7 +1,7 @@ [package] authors = ["Paul Masurel ", "Pascal Seitz "] name = "ownedbytes" -version = "0.1.0" +version = "0.2.0" edition = "2018" description = "Expose data as static slice" license = "MIT" diff --git a/ownedbytes/src/lib.rs b/ownedbytes/src/lib.rs index 90f3af893e..494048b90d 100644 --- a/ownedbytes/src/lib.rs +++ b/ownedbytes/src/lib.rs @@ -76,6 +76,19 @@ impl OwnedBytes { (left, right) } + /// Splits the right part of the `OwnedBytes` at the given offset. + /// + /// `self` is truncated to `split_len`, left with the remaining bytes. + pub fn split_off(&mut self, split_len: usize) -> OwnedBytes { + let right_box_stable_deref = self.box_stable_deref.clone(); + let right_piece = OwnedBytes { + data: &self.data[split_len..], + box_stable_deref: right_box_stable_deref, + }; + self.data = &self.data[..split_len]; + right_piece + } + /// Returns true iff this `OwnedBytes` is empty. #[inline] pub fn is_empty(&self) -> bool { @@ -124,6 +137,35 @@ impl fmt::Debug for OwnedBytes { } } +impl PartialEq for OwnedBytes { + fn eq(&self, other: &OwnedBytes) -> bool { + self.as_slice() == other.as_slice() + } +} + +impl Eq for OwnedBytes {} + +impl PartialEq<[u8]> for OwnedBytes { + fn eq(&self, other: &[u8]) -> bool { + self.as_slice() == other + } +} + +impl PartialEq for OwnedBytes { + fn eq(&self, other: &str) -> bool { + self.as_slice() == other.as_bytes() + } +} + +impl<'a, T: ?Sized> PartialEq<&'a T> for OwnedBytes +where + OwnedBytes: PartialEq, +{ + fn eq(&self, other: &&'a T) -> bool { + *self == **other + } +} + impl Deref for OwnedBytes { type Target = [u8]; @@ -287,4 +329,14 @@ mod tests { assert_eq!(right.as_slice(), b""); } } + + #[test] + fn test_split_off() { + let mut data = OwnedBytes::new(b"abcdef".as_ref()); + assert_eq!(data, "abcdef"); + assert_eq!(data.split_off(2), "cdef"); + assert_eq!(data, "ab"); + assert_eq!(data.split_off(1), "b"); + assert_eq!(data, "a"); + } }