use std::borrow::Cow;
use std::ops::Deref;
pub trait VectorRef {
fn as_slice(&self) -> &[f32];
fn dimension(&self) -> usize {
self.as_slice().len()
}
fn is_empty(&self) -> bool {
self.as_slice().is_empty()
}
}
impl VectorRef for [f32] {
#[inline]
fn as_slice(&self) -> &[f32] {
self
}
}
impl VectorRef for Vec<f32> {
#[inline]
fn as_slice(&self) -> &[f32] {
self
}
}
impl VectorRef for &[f32] {
#[inline]
fn as_slice(&self) -> &[f32] {
self
}
}
impl VectorRef for Cow<'_, [f32]> {
#[inline]
fn as_slice(&self) -> &[f32] {
self
}
}
#[derive(Debug, Clone, Copy)]
pub struct BorrowedVector<'a> {
data: &'a [f32],
}
impl<'a> BorrowedVector<'a> {
#[inline]
#[must_use]
pub const fn new(data: &'a [f32]) -> Self {
Self { data }
}
#[inline]
#[must_use]
pub const fn data(&self) -> &'a [f32] {
self.data
}
}
impl VectorRef for BorrowedVector<'_> {
#[inline]
fn as_slice(&self) -> &[f32] {
self.data
}
}
impl Deref for BorrowedVector<'_> {
type Target = [f32];
#[inline]
fn deref(&self) -> &Self::Target {
self.data
}
}
impl AsRef<[f32]> for BorrowedVector<'_> {
#[inline]
fn as_ref(&self) -> &[f32] {
self.data
}
}
pub struct VectorGuard<'a, G> {
_guard: G,
data: &'a [f32],
}
impl<'a, G> VectorGuard<'a, G> {
#[must_use]
pub const fn new(guard: G, data: &'a [f32]) -> Self {
Self {
_guard: guard,
data,
}
}
}
impl<G> VectorRef for VectorGuard<'_, G> {
#[inline]
fn as_slice(&self) -> &[f32] {
self.data
}
}
impl<G> Deref for VectorGuard<'_, G> {
type Target = [f32];
#[inline]
fn deref(&self) -> &Self::Target {
self.data
}
}
impl<G> AsRef<[f32]> for VectorGuard<'_, G> {
#[inline]
fn as_ref(&self) -> &[f32] {
self.data
}
}