Compare commits

..

1 Commits

Author SHA1 Message Date
36a5461de5 Composite is now generic over size in memory.
Previously the Composite trait accepted a Vec<Value> for reading from memory, and returned a Vec<Value> for writing to memory. This meant the caller might provide the wrong size of Vec (e.g. a Point3d requires 3 memory addresses, so the code has to check and handle if the user only passes in 2).

Now those methods accept/return a [Value; N] which is statically guaranteed to be the right size! This means there's no more need for runtime checks that the Vec is the right size -- because the array is guaranteed to be the right size.

This involves removing the SIZE constant and instead changing it into a SIZE const generic.
2023-12-04 12:33:07 -06:00
2 changed files with 7 additions and 7 deletions

View File

@ -12,8 +12,9 @@ pub trait Composite<const SIZE: usize>: Sized {
impl Composite<3> for kittycad::types::Point3D {
fn into_parts(self) -> [Value; 3] {
let points = [self.x, self.y, self.z];
points.map(NumericValue::Float).map(Value::NumericValue)
[self.x, self.y, self.z]
.map(NumericValue::Float)
.map(Value::NumericValue)
}
fn from_parts(values: [Value; 3]) -> Result<Self, ExecutionError> {

View File

@ -6,9 +6,10 @@
//! You can think of it as a domain-specific language for making KittyCAD API calls and using
//! the results to make other API calls.
use std::{collections::HashMap, fmt};
use composite::Composite;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fmt};
mod composite;
#[cfg(test)]
@ -270,8 +271,6 @@ pub enum ExecutionError {
CannotApplyOperation { op: Operation, operands: Vec<Value> },
#[error("Tried to read a '{expected}' from KCEP program memory, found an '{actual}' instead")]
MemoryWrongType { expected: &'static str, actual: String },
#[error("Wrong size of memory trying to read value from KCEP program memory: got {actual} but wanted {expected}")]
MemoryWrongSize { expected: usize, actual: usize },
}
/// Take an array of result and return a result of array.
@ -279,9 +278,9 @@ pub enum ExecutionError {
/// If any member of the array was Err(E), return Err with the first E value.
fn arr_res_to_res_array<T, E, const N: usize>(arr: [Result<T, E>; N]) -> Result<[T; N], E> {
let mut out = core::array::from_fn(|_| None);
for (i, res) in arr.enumerate() {
for (i, res) in arr.into_iter().enumerate() {
out[i] = match res {
Ok(x) => x,
Ok(x) => Some(x),
Err(e) => return Err(e),
};
}