use serde::{Deserialize, Serialize};
use std::ops::{Index, IndexMut};
use std::slice::SliceIndex;
use tinyvec::TinyVec;
#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Position(TinyVec<[f64; 2]>);
impl Position {
pub fn as_slice(&self) -> &[f64] {
&self.0
}
pub fn as_slice_mut(&mut self) -> &mut [f64] {
&mut self.0
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl<I: SliceIndex<[f64]>> Index<I> for Position {
type Output = <I as SliceIndex<[f64]>>::Output;
#[inline(always)]
fn index(&self, index: I) -> &Self::Output {
&self.0[index]
}
}
impl<I: SliceIndex<[f64]>> IndexMut<I> for Position {
#[inline(always)]
fn index_mut(&mut self, index: I) -> &mut Self::Output {
&mut self.0[index]
}
}
impl From<TinyVec<[f64; 2]>> for Position {
fn from(value: TinyVec<[f64; 2]>) -> Self {
Self(value)
}
}
impl From<Vec<f64>> for Position {
fn from(value: Vec<f64>) -> Self {
Self(TinyVec::Heap(value))
}
}
impl From<[f64; 2]> for Position {
fn from(value: [f64; 2]) -> Self {
Self(TinyVec::Inline(value.into()))
}
}
impl From<(f64, f64)> for Position {
fn from(value: (f64, f64)) -> Self {
Self::from([value.0, value.1])
}
}
impl From<[f64; 3]> for Position {
fn from(value: [f64; 3]) -> Self {
Self(TinyVec::Heap(value.into()))
}
}
impl From<(f64, f64, f64)> for Position {
fn from(value: (f64, f64, f64)) -> Self {
Self::from([value.0, value.1, value.2])
}
}
impl From<[f64; 4]> for Position {
fn from(value: [f64; 4]) -> Self {
Self(TinyVec::Heap(value.into()))
}
}
impl From<(f64, f64, f64, f64)> for Position {
fn from(value: (f64, f64, f64, f64)) -> Self {
Self::from([value.0, value.1, value.2, value.3])
}
}