pub struct EmbeddingView<'a> {
pub dim: u16,
pub dtype: EmbeddingType,
/* private fields */
}Expand description
Zero-copy view into an embedding stored in a binary buffer.
This struct provides direct access to embedding data without allocation. All similarity computations can be performed on borrowed data.
§Layout
The binary format is:
[u16 dim | u8 dtype | u8 similarity | vector data...]Fields§
§dim: u16Embedding dimension
dtype: EmbeddingTypeEmbedding data type (F32, F16, etc.)
Implementations§
Source§impl<'a> EmbeddingView<'a>
impl<'a> EmbeddingView<'a>
Sourcepub fn from_bytes(bytes: &'a [u8]) -> Result<Self, String>
pub fn from_bytes(bytes: &'a [u8]) -> Result<Self, String>
Sourcepub fn as_f32_vec(&self) -> Result<Vec<f32>, String>
pub fn as_f32_vec(&self) -> Result<Vec<f32>, String>
Returns the embedding as a Vec
This method performs a safe copy of the embedding data, converting from bytes to f32 values. While it allocates memory, it’s guaranteed to work regardless of memory alignment.
§Performance
For 256-dim embedding: ~100-200ns allocation + copy overhead Still much faster than full record decode for large records.
§Errors
Returns error if dtype is not F32.
Sourcepub fn as_f32_slice(&self) -> Result<&'a [f32], String>
pub fn as_f32_slice(&self) -> Result<&'a [f32], String>
Returns the embedding as an f32 slice (zero-copy cast - EXPERIMENTAL).
⚠️ WARNING: This method is experimental and may fail with alignment errors.
Memory alignment is not guaranteed for slices from arbitrary buffers.
Use as_f32_vec() for a safe, reliable alternative.
§Safety
Uses bytemuck for casting, which requires 4-byte alignment.
Will panic if the underlying buffer is not properly aligned.
§Errors
Returns error if dtype is not F32 or bytemuck feature is not enabled.
Sourcepub fn cosine_similarity(
&self,
other: &EmbeddingView<'_>,
) -> Result<f32, String>
pub fn cosine_similarity( &self, other: &EmbeddingView<'_>, ) -> Result<f32, String>
pub fn dot_product(&self, other: &EmbeddingView<'_>) -> Result<f32, String>
Sourcepub fn euclidean_distance(
&self,
other: &EmbeddingView<'_>,
) -> Result<f32, String>
pub fn euclidean_distance( &self, other: &EmbeddingView<'_>, ) -> Result<f32, String>
Computes Euclidean distance (zero-copy).
Sourcepub fn similarity(
&self,
other: &EmbeddingView<'_>,
metric: SimilarityMetric,
) -> Result<f32, String>
pub fn similarity( &self, other: &EmbeddingView<'_>, metric: SimilarityMetric, ) -> Result<f32, String>
Generic similarity with metric selection.
Trait Implementations§
Source§impl<'a> Clone for EmbeddingView<'a>
impl<'a> Clone for EmbeddingView<'a>
Source§fn clone(&self) -> EmbeddingView<'a>
fn clone(&self) -> EmbeddingView<'a>
1.0.0§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<'a> Debug for EmbeddingView<'a>
impl<'a> Debug for EmbeddingView<'a>
impl<'a> Copy for EmbeddingView<'a>
Auto Trait Implementations§
impl<'a> Freeze for EmbeddingView<'a>
impl<'a> RefUnwindSafe for EmbeddingView<'a>
impl<'a> Send for EmbeddingView<'a>
impl<'a> Sync for EmbeddingView<'a>
impl<'a> Unpin for EmbeddingView<'a>
impl<'a> UnwindSafe for EmbeddingView<'a>
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)