EmbeddingView

Struct EmbeddingView 

Source
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: u16

Embedding dimension

§dtype: EmbeddingType

Embedding data type (F32, F16, etc.)

Implementations§

Source§

impl<'a> EmbeddingView<'a>

Source

pub fn from_bytes(bytes: &'a [u8]) -> Result<Self, String>

Creates a new embedding view from raw bytes.

§Format

Expects bytes in the format: [u16 dim | u8 dtype | u8 reserved | data...]

§Errors

Returns error if:

  • Buffer too small (< 4 bytes header)
  • Invalid dtype byte
  • Data length doesn’t match expected size
Source

pub fn data(&self) -> &'a [u8]

Returns the raw data bytes.

Source

pub fn as_f32_vec(&self) -> Result<Vec<f32>, String>

Returns the embedding as a Vec (safe copy).

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.

Source

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.

Source

pub fn cosine_similarity( &self, other: &EmbeddingView<'_>, ) -> Result<f32, String>

Computes cosine similarity with another embedding view (zero-copy).

§Performance

Uses SIMD instructions when available on x86_64. Typical performance:

  • 256-dim: ~50-100 ns
  • 1024-dim: ~200-400 ns
§Errors

Returns error if dimensions or dtypes don’t match.

Source

pub fn dot_product(&self, other: &EmbeddingView<'_>) -> Result<f32, String>

Source

pub fn euclidean_distance( &self, other: &EmbeddingView<'_>, ) -> Result<f32, String>

Computes Euclidean distance (zero-copy).

Source

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>

Source§

fn clone(&self) -> EmbeddingView<'a>

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for EmbeddingView<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.