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
impl Vector
Sourcepub fn new(data: Vec<f32>) -> Result<Self>
pub fn new(data: Vec<f32>) -> Result<Self>
Builds a Vector from data, validating the contents.
Returns IqdbError::InvalidVector when:
datais 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,
);Sourcepub fn new_unchecked(data: Vec<f32>) -> Self
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);Sourcepub fn as_slice(&self) -> &[f32]
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]);Sourcepub fn len(&self) -> usize
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);Sourcepub fn is_empty(&self) -> bool
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());Sourcepub fn dim(&self) -> usize
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);Sourcepub fn into_inner(self) -> Vec<f32>
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]);