diff options
| author | metamuffin <metamuffin@disroot.org> | 2026-01-06 04:00:51 +0100 |
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2026-01-06 04:00:51 +0100 |
| commit | c04f49adaa2cb0fa3074d6b122d1e11689c4f5de (patch) | |
| tree | c8713e458ba2428c3f4df37181351e56ec7a7c57 /common/object | |
| parent | 1d3fe7b02b2d2ec7cad5c8e3e0fd4eafb60bf0e0 (diff) | |
| download | jellything-c04f49adaa2cb0fa3074d6b122d1e11689c4f5de.tar jellything-c04f49adaa2cb0fa3074d6b122d1e11689c4f5de.tar.bz2 jellything-c04f49adaa2cb0fa3074d6b122d1e11689c4f5de.tar.zst | |
find field + refactor old common types
Diffstat (limited to 'common/object')
| -rw-r--r-- | common/object/src/lib.rs | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/common/object/src/lib.rs b/common/object/src/lib.rs index 97d2650..9f9e0be 100644 --- a/common/object/src/lib.rs +++ b/common/object/src/lib.rs @@ -39,8 +39,15 @@ impl<'a> Object<'a> { values: &buf[1 + nf + nf..], }) } - pub fn get_aligned(&self, tag: Tag) -> Option<&[u32]> { - let index = self.tags.binary_search(&tag.0).ok()?; + fn find_field(&self, tag: Tag) -> Option<usize> { + // using partition as binary search for the first field (instead of regular binary_search that returns any) + let first = self.tags.partition_point(|&x| x >= tag.0); + self.tags + .get(first) + .is_some_and(|&x| x == tag.0) + .then_some(first) + } + fn get_aligned(&self, index: usize) -> Option<&[u32]> { let start_raw = *self.offsets.get(index)?; let end_raw = self .offsets @@ -53,8 +60,7 @@ impl<'a> Object<'a> { Some(&self.values[start as usize..end as usize]) } - pub fn get_unaligned(&self, tag: Tag) -> Option<&[u8]> { - let index = self.tags.binary_search(&tag.0).ok()?; + fn get_unaligned(&self, index: usize) -> Option<&[u8]> { let start_raw = *self.offsets.get(index)?; let end_raw = self .offsets @@ -69,15 +75,12 @@ impl<'a> Object<'a> { let values_u8: &[u8] = bytemuck::cast_slice(self.values); Some(&values_u8[start as usize..end as usize]) } - pub fn get_str(&self, tag: Tag) -> Option<&str> { - self.get_unaligned(tag).and_then(|b| str::from_utf8(b).ok()) - } - pub fn get<'b: 'a, T: Value<'b>>(&'b self, tag: TypedTag<T>) -> Option<T> { + let index = self.find_field(tag.0)?; if T::ALIGNED { - T::load_aligned(self.get_aligned(tag.0)?) + T::load_aligned(self.get_aligned(index)?) } else { - T::load_unaligned(self.get_unaligned(tag.0)?) + T::load_unaligned(self.get_unaligned(index)?) } } } |