fixed-vectors

Build Crates.io

Library implementing fixed-length Vectors meant for representing dimensional values. Tested heavily to ensure safety during use.


Testing

```bash $ cd fixed-vectors $ cargo test ...

If things go well during the tests you should see ok as the test result.

```


Examples

There are various examples implemented within the examples directory to look at for getting started. If you would like to run an example within the examples directory you can run the following commands. bash $ cd fixed-vectors $ cargo run --example [EXAMPLE_NAME]

Written Examples

Voxel Example below shows how a Voxel might be implemented using a Vector3.

```rust use fixed_vectors::Vector3; use core::ops::Index; use std::vec::Vec;

[derive(Debug, PartialEq, Eq)]

struct Voxel { pub position: Vector3, }

[derive(Debug, PartialEq, Eq)]

struct VoxelChunk { voxels: Vec, size: Vector3, }

impl VoxelChunk { pub fn new(size: Vector3) -> Self { let mut voxels = Vec::with_capacity((size.x * size.y * size.z) as usize);

    for x in 0 .. size.x {
        for y in 0 .. size.y {
            for z in 0 .. size.z {
                let new_voxel = Voxel { position: Vector3::new(x, y, z) };
                voxels.push(new_voxel);
            }
        }
    }

    return Self {
        voxels,
        size,
    };
}

}

impl Index> for VoxelChunk { type Output = Voxel;

fn index(&self, index: Vector3<u32>) -> &Self::Output {
    return &self.voxels[position_as_index(self.size, index)];
}

}

/* NOTE: Because of the order a [VoxelChunk] is constructed, we can use this formula to estimate an index of a [Voxel] within the voxels [Vec]. */ const fn positionasindex(size: Vector3, position: Vector3) -> usize { return (position.z + (position.y * size.z) + (position.x * size.z * size.y)) as usize; }

fn main() { let chunk = VoxelChunk::new(Vector3::new(8, 16, 8)); let position = Vector3::::new(4, 8, 4); assert_eq!(chunk[position].position, position); } ```

Custom Vector Example below shows how you would create a custom Vector Struct.

```rust use fixedvectors::{Vector, implvector};

struct Vector5 { x: T, y: T, z: T, w: T, v: T, }

impl_vector!(Vector5 { x, y, z, w, v }, 5);

fn main() { println!("Vector5 Name: {}", Vector5::<()>::NAME); println!("Vector5 Length: {}", Vector5::<()>::LEN); println!("Vector5 Size: {}", Vector5::::SIZE);

let vector = Vector5::new(1, 2, 3, 4, 5);

println!("Vector: {}", vector);
println!("Vector Debug: {:?}", vector);
println!("Vector as Array: {:?}", vector.to_array());
println!("Vector as Vec: {:?}", vector.to_vec());

let mut sum = 0;
for i in vector { sum += i; }
println!("Vector Sum: {}", sum);

} ```

Tuplable Vector Example below shows how you would implemented the TuplableVector Trait in a Vector, as it's not currently implemented automatically with the impl_vector! Macro.

```rust use fixedvectors::{TuplableVector, implvector};

struct Vector2 { x: T, y: T, }

impl_vector!(Vector2 { x, y }, 2);

impl TuplableVector { type Output = (T, T);

fn to_tuple(self) -> Self::Output {
    return (self.x, self.y);
}

}

fn main() { let tuple = Vector2::new("Vector", "2").totuple(); println!("Vector as Tuple: {:?}", tuple); asserteq!(tuple, ("Vector", "2")); } ```


License

MIT