pub struct PointND<T, const N: usize> where
    T: Clone + Copy
{ /* private fields */ }
Expand description

The whole point of the crate (get it?)

It really is just a small wrapper around an array with convenience methods for accessing values if it’s dimensions are 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 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);

// Creates a 2D point from values of a given vector or array
let vec = vec![0, 1];
let p: PointND<_, 2> = PointND::from(&vec);

// ERROR:
// let p: PointND<_, 0> = PointND::fill(9);

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();

// If the point had 3 dimensions, we could use the above and:
// let z = p.z();

// Or 4:
// ...
// let w = p.w();

assert_eq!(y, arr[1]);

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
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;

// A 2D point
let p: PointND<i32, 2> = PointND::fill(10);
assert_eq!(p.dims(), 2);

Implementations

Returns a new PointND with values from the specified array or vector

Panics

If the length of the slice is zero

Returns a new PointND with all values set as specified

Panics

If the dimensions of the point being constructed is zero

Returns the number of dimensions of the point (a 2D point will return 2, a 3D point 3, etc)

Returns the Some(value) at the specified dimension or None if the dimension is out of bounds

The value of the first dimension is indexed at 0 for easier interoperability with standard indexing

Returns an array of all the values contained by the point

Returns a vector of all the values contained by the point

Function for safely returning the first value contained by a 1D PointND

Functions for safely returning the first and second values contained by a 2D PointND

Functions for safely returning the first, second and third values contained by a 3D PointND

Functions for safely returning the first, second, third and fourth values contained by a 4D PointND

Trait Implementations

The resulting type after applying the + operator.

Performs the + operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

The resulting type after applying the - operator.

Performs the - operation. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.