Expand description
The whole point of the crate (get it?)
This is basically just a small wrapper around an array with convenience methods for accessing values if it’s dimensions are within 1..=4
Examples
Constructing a Point
No matter how a PointND is constructed, the second generic arg must be filled with the number of dimensions it needs to have
If a point of zero dimensions is constructed, it will panic
use point_nd::PointND;
// Creates a 2D point from values of a given vector or array
let vec: Vec<i32> = vec![0, 1];
let p: PointND<_, 2> = PointND::from(&vec);
// Creates a 3D point with all values set to 5
// When using this function, complete type annotation is necessary
let p: PointND<i32, 3> = PointND::fill(5);
// ERROR: Can't create a point with zero dimensions
// let p: PointND<_, 0> = PointND::fill(9);
// If you don't like writing PointND twice, use this syntax instead
// Note: The second generic must still be specified
let p = PointND::<_, 2>::from(&vec);Accessing Values
It is recommended to use the convenience getters if the dimensions of the point are from 1..=4
use point_nd::PointND;
// A 2D point
let arr: [i32; 2] = [0,1];
let p: PointND<_, 2> = PointND::from(&arr);
// As the point has 2 dimensions, we can access it's values with the x() and y() methods
let x: &i32 = p.x();
let y = p.y();
assert_eq!(*y, arr[1]);
// If the point had 3 dimensions, we could use the above and:
// let z = p.z();
// Or 4:
// ...
// let w = p.w();Otherwise indexing or the get() method can be used
use point_nd::PointND;
let arr: [i32; 2] = [0,1];
let p: PointND<_, 2> = PointND::from(&arr);
// Safely getting
// Returns None if index is out of bounds
let x: Option<&i32> = p.get(0);
assert_eq!(*x.unwrap(), arr[0]);
// Unsafely indexing
// If the index is out of bounds, this will panic
// Note that unlike other accessing methods, this will return a copy of the value
let y: i32 = p[1];
assert_eq!(y, arr[1]);Querying Size
The number of dimensions can be retrieved using the dims() method (short for dimensions)
use point_nd::PointND;
let p: PointND<i32, 2> = PointND::fill(10);
assert_eq!(p.dims(), 2);Implementations
sourceimpl<T, const N: usize> PointND<T, N> where
T: Clone + Copy,
impl<T, const N: usize> PointND<T, N> where
T: Clone + Copy,
sourceimpl<T, const N: usize> PointND<T, N> where
T: Clone + Copy,
impl<T, const N: usize> PointND<T, N> where
T: Clone + Copy,
sourceimpl<T, const N: usize> PointND<T, N> where
T: Clone + Copy,
impl<T, const N: usize> PointND<T, N> where
T: Clone + Copy,
sourcepub fn apply<F>(self, modifier: F) -> Self where
F: Fn(T) -> T,
pub fn apply<F>(self, modifier: F) -> Self where
F: Fn(T) -> T,
Returns a new PointND from the values contained by self after applying the modifier function to them
Examples
use point_nd::PointND;
// Multiplies each item by 10
let p = PointND::<i32, 3>::from(&[0, 1, 2]);
let p = p.apply(|item| item * 10);
assert_eq!(p.as_arr(), [0, 10, 20]);sourcepub fn apply_dims<F>(self, dims: &[usize], modifier: F) -> Self where
F: Fn(T) -> T,
pub fn apply_dims<F>(self, dims: &[usize], modifier: F) -> Self where
F: Fn(T) -> T,
Returns a new PointND from the values at the specified dimensions after applying the modifier function to them
Any values at dimensions that were not specified are passed as is
If any dimensions specified are out of bounds, this method will ignore it
Examples
use point_nd::PointND;
// Multiplies items at indexes 1 and 2 by 2
let p = PointND::<i32, 4>::from(&[0, 1, 2, 3]);
let p = p.apply_dims(&[1, 2], |item| item * 2);
assert_eq!(p.as_arr(), [0, 2, 4, 3]);sourcepub fn apply_with<F>(self, values: [T; N], modifier: F) -> Self where
F: Fn(T, T) -> T,
pub fn apply_with<F>(self, values: [T; N], modifier: F) -> Self where
F: Fn(T, T) -> T,
Returns a new PointND from the values specified and those contained by self after applying the modifier to both
Examples
use point_nd::PointND;
// Adds each item in the PointND with their respective items in the array
let p = PointND::<i32, 3>::from(&[0, 1, 2]);
let p = p.apply_with([1, 2, 3], |a, b| a + b);
assert_eq!(p.as_arr(), [1, 3, 5]);sourceimpl<T> PointND<T, 1> where
T: Clone + Copy,
impl<T> PointND<T, 1> where
T: Clone + Copy,
Function for safely getting and setting the first value contained by a 1D PointND
sourceimpl<T> PointND<T, 2> where
T: Clone + Copy,
impl<T> PointND<T, 2> where
T: Clone + Copy,
Functions for safely getting and setting the first and second values contained by a 2D PointND
sourceimpl<T> PointND<T, 3> where
T: Clone + Copy,
impl<T> PointND<T, 3> where
T: Clone + Copy,
Functions for safely getting and setting the first, second and third values contained by a 3D PointND
sourceimpl<T> PointND<T, 4> where
T: Clone + Copy,
impl<T> PointND<T, 4> where
T: Clone + Copy,
Functions for safely getting and setting the first, second, third and fourth values contained by a 4D PointND
Trait Implementations
sourceimpl<T, const N: usize> Add<PointND<T, N>> for PointND<T, N> where
T: Add<Output = T> + Clone + Copy,
impl<T, const N: usize> Add<PointND<T, N>> for PointND<T, N> where
T: Add<Output = T> + Clone + Copy,
sourceimpl<T, const N: usize> Div<PointND<T, N>> for PointND<T, N> where
T: Div<Output = T> + Clone + Copy,
impl<T, const N: usize> Div<PointND<T, N>> for PointND<T, N> where
T: Div<Output = T> + Clone + Copy,
sourceimpl<I, T, const N: usize> Index<I> for PointND<T, N> where
T: Clone + Copy,
I: Sized + SliceIndex<[T], Output = T>,
impl<I, T, const N: usize> Index<I> for PointND<T, N> where
T: Clone + Copy,
I: Sized + SliceIndex<[T], Output = T>,
sourceimpl<I, T, const N: usize> IndexMut<I> for PointND<T, N> where
T: Clone + Copy,
I: Sized + SliceIndex<[T], Output = T>,
impl<I, T, const N: usize> IndexMut<I> for PointND<T, N> where
T: Clone + Copy,
I: Sized + SliceIndex<[T], Output = T>,
sourceimpl<T, const N: usize> Mul<PointND<T, N>> for PointND<T, N> where
T: Mul<Output = T> + Clone + Copy,
impl<T, const N: usize> Mul<PointND<T, N>> for PointND<T, N> where
T: Mul<Output = T> + Clone + Copy,
sourceimpl<T: PartialEq, const N: usize> PartialEq<PointND<T, N>> for PointND<T, N> where
T: Clone + Copy,
impl<T: PartialEq, const N: usize> PartialEq<PointND<T, N>> for PointND<T, N> where
T: Clone + Copy,
sourceimpl<T, const N: usize> Sub<PointND<T, N>> for PointND<T, N> where
T: Sub<Output = T> + Clone + Copy,
impl<T, const N: usize> Sub<PointND<T, N>> for PointND<T, N> where
T: Sub<Output = T> + Clone + Copy,
impl<T: Copy, const N: usize> Copy for PointND<T, N> where
T: Clone + Copy,
impl<T: Eq, const N: usize> Eq for PointND<T, N> where
T: Clone + Copy,
impl<T, const N: usize> StructuralEq for PointND<T, N> where
T: Clone + Copy,
impl<T, const N: usize> StructuralPartialEq for PointND<T, N> where
T: Clone + Copy,
Auto Trait Implementations
impl<T, const N: usize> RefUnwindSafe for PointND<T, N> where
T: RefUnwindSafe,
impl<T, const N: usize> Send for PointND<T, N> where
T: Send,
impl<T, const N: usize> Sync for PointND<T, N> where
T: Sync,
impl<T, const N: usize> Unpin for PointND<T, N> where
T: Unpin,
impl<T, const N: usize> UnwindSafe for PointND<T, N> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into)Uses borrowed data to replace owned data, usually by cloning. Read more