Skip to main content

Vector

Struct Vector 

Source
pub struct Vector(/* private fields */);
Expand description

An owned dense vector of f32 components.

Construct one with the fallible Vector::new (which rejects empty inputs and non-finite components) or its TryFrom<Vec<f32>> sibling; read its components with as_slice or reclaim the buffer with into_inner.

Validation at this boundary keeps the rest of the spine free of input checks: once a Vector exists, the math never has to defend against empty, NaN, or infinite components.

§Representation

Components are stored in a Box<[f32]>, not a Vec<f32>: a Vector is immutable after construction, so it never needs spare capacity. This makes the value one machine word smaller than a Vec-backed wrapper and guarantees the backing allocation is sized exactly to the data — meaningful when millions of vectors are held resident.

§Examples

use iqdb_types::{IqdbError, Vector};

let v = Vector::new(vec![1.0, 0.0, 0.0]).unwrap();
assert_eq!(v.dim(), 3);
assert_eq!(v.as_slice(), &[1.0, 0.0, 0.0]);

// Empty and non-finite inputs are rejected at construction.
assert_eq!(Vector::new(Vec::new()).unwrap_err(), IqdbError::InvalidVector);
assert_eq!(
    Vector::new(vec![1.0, f32::NAN]).unwrap_err(),
    IqdbError::InvalidVector,
);

Implementations§

Source§

impl Vector

Source

pub fn new(data: Vec<f32>) -> Result<Self>

Builds a Vector from data, validating the contents.

Returns IqdbError::InvalidVector when:

  • data is empty, or
  • any component is not finite (NaN or ±infinity).

Validating at the type boundary keeps the rest of the spine — and every consumer crate — free of input checks. Once a Vector is in hand the math can trust its contents.

The data buffer is shrunk to fit (into_boxed_slice) on success, so the stored allocation carries no spare capacity.

§Examples
use iqdb_types::{IqdbError, Vector};

let v = Vector::new(vec![0.1, 0.2, 0.3]).unwrap();
assert_eq!(v.dim(), 3);

assert_eq!(
    Vector::new(Vec::new()).unwrap_err(),
    IqdbError::InvalidVector,
);
assert_eq!(
    Vector::new(vec![1.0, f32::INFINITY]).unwrap_err(),
    IqdbError::InvalidVector,
);
Source

pub fn new_unchecked(data: Vec<f32>) -> Self

Builds a Vector from data without validating it.

Available only when the crate is built with the testing feature. Production code MUST use Vector::new (or TryFrom); a production build of iqdb-types cannot compile a call to this constructor.

Reserved for tests that deliberately need to construct otherwise- invalid vectors to assert downstream behavior on bad input.

§Examples
use iqdb_types::Vector;

// Constructible only under the `testing` feature.
let v = Vector::new_unchecked(vec![f32::NAN]);
assert_eq!(v.len(), 1);
Source

pub fn as_slice(&self) -> &[f32]

Borrows the components as a slice.

§Examples
use iqdb_types::Vector;

let v = Vector::new(vec![0.5, 0.5]).unwrap();
assert_eq!(v.as_slice(), &[0.5, 0.5]);
Source

pub fn len(&self) -> usize

Returns the number of components.

§Examples
use iqdb_types::Vector;

assert_eq!(Vector::new(vec![1.0, 2.0]).unwrap().len(), 2);
Source

pub fn is_empty(&self) -> bool

Returns true if the vector has no components.

A Vector produced by Vector::new is never empty (empty inputs are rejected at construction); this method is false for every Vector outside the testing-gated Vector::new_unchecked.

§Examples
use iqdb_types::Vector;

assert!(!Vector::new(vec![1.0]).unwrap().is_empty());
Source

pub fn dim(&self) -> usize

Returns the dimensionality of the vector (its component count).

§Examples
use iqdb_types::Vector;

assert_eq!(Vector::new(vec![1.0, 2.0, 3.0]).unwrap().dim(), 3);
Source

pub fn into_inner(self) -> Vec<f32>

Consumes the vector and returns the underlying buffer as a Vec<f32>.

This is allocation-free: the boxed slice is converted back to a Vec with capacity equal to its length (Box<[f32]>::into_vec), no copy.

§Examples
use iqdb_types::Vector;

let v = Vector::new(vec![1.0, 2.0]).unwrap();
assert_eq!(v.into_inner(), vec![1.0, 2.0]);

Trait Implementations§

Source§

impl Clone for Vector

Source§

fn clone(&self) -> Vector

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Vector

Source§

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

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

impl<'de> Deserialize<'de> for Vector

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for Vector

Source§

fn eq(&self, other: &Vector) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Vector

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Vector

Source§

impl TryFrom<Vec<f32>> for Vector

Source§

fn try_from(data: Vec<f32>) -> Result<Self>

Delegates to Vector::new: rejects empty and non-finite inputs with IqdbError::InvalidVector.

§Examples
use iqdb_types::Vector;

let v: Vector = vec![1.0, 0.0].try_into().unwrap();
assert_eq!(v.dim(), 2);
Source§

type Error = IqdbError

The type returned in the event of a conversion error.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

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

Source§

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
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

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

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

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.
Source§

impl<E> WithErrorCode<E> for E

Source§

fn with_code(self, code: impl Into<String>) -> CodedError<E>

Attach an error code to an error