vector-basis 0.3.0

Access components of generic vector types
Documentation

Vector Basis Traits

Crates.io Docs License no_std

Low-level traits for generic access to vector basis components (e.g., x, y, z). Builds directly on vector-space.

⚠️ Recommended for interop only (like FFI with render engines, serialization). Prefer vector-space traits for math operations—avoid direct component access in generic code.

Quick Start

use vector_basis::{Basis, Bases};
use vector_space::VectorSpace;

#[derive(Copy, Clone, Debug, PartialEq)]
struct Vec2(f32, f32);

impl VectorSpace for Vec2 {
    type Scalar = f32;
}

impl Basis<0> for Vec2 {
    fn basis(&self) -> f32 { self.0 }
    fn basis_mut(&mut self) -> &mut f32 { &mut self.0 }
}

impl Basis<1> for Vec2 {
    fn basis(&self) -> f32 { self.1 }
    fn basis_mut(&mut self) -> &mut f32 { &mut self.1 }
}

// Convenience via Bases (auto-implemented)
let v = Vec2(1.0, 2.0);
assert_eq!(v.bases::<0>(), 1.0);  // x-component
assert_eq!(v.bases::<1>(), 2.0);  // y-component

let v2 = v.with_bases::<1>(3.0);  // Set y to 3.0
assert_eq!(v2, Vec2(1.0, 3.0));

let unit_x = Vec2::unit_bases::<0>();  // (1.0, 0.0)
let scaled_x = Vec2::bases_of::<0>(5.0);  // (5.0, 0.0)

Features

  • Basis<I>: Const-generic index access (I=0 → x, I=1 → y, etc.) -> unit_basis(), basis_of(mag), basis(&self), basis_mut(&mut self), with_basis(self, mag)
  • Bases: Blanket impl over Basis<I> for convenience (bases::<I>(), etc.)
  • Zero-cost: Inline, no allocations
  • no_std: Full support
  • Interop-focused: Safe for engines like wgpu/OpenGL (pass components directly)
  • Dimension-generic: Both 2D and 3D vectors can implement Basis<0> and Basis<1>, which makes it possible to use both 2D and 3D vectors for a 2D rendering engine using this crate

When to Use

  • Yes: Extracting v.bases::<2>() for GPU buffers or JSON serialization.
  • No: Math ops—use InnerSpace::project() etc. from vector-space instead.