Skip to content

Commit

Permalink
Relax Vol::from_elements() bound.
Browse files Browse the repository at this point in the history
This allows it to be constructed from slice references.
  • Loading branch information
kpreid committed Oct 20, 2023
1 parent b82c9ef commit 03170ea
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions all-is-cubes/src/math/vol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,33 @@ pub struct Vol<C, O = ZMaj> {
contents: C,
}

/// Constructors from linear containers.
impl<C, O: Default, V> Vol<C, O>
where
C: Deref<Target = [V]>,
{
/// Constructs a `Vol<C>` containing the provided elements, which must be in the
/// ordering specified by `O`.
///
/// Returns a [`VolLengthError`] if the number of elements does not match
/// [`bounds.volume()`](GridAab::volume).
pub fn from_elements(bounds: GridAab, elements: impl Into<C>) -> Result<Self, VolLengthError> {
let elements = elements.into();
if elements.len() == bounds.volume() {
Ok(Vol {
bounds,
ordering: O::default(),
contents: elements,
})
} else {
Err(VolLengthError {
input_length: elements.len(),
bounds,
})
}
}
}

/// Constructors from elements.
impl<C, O: Default, V> Vol<C, O>
where
Expand Down Expand Up @@ -89,27 +116,6 @@ where
Self::from_elements(GridAab::ORIGIN_CUBE, core::iter::once(value).collect::<C>()).unwrap()
}

/// Constructs a `Vol<C>` containing the provided elements, which must be in the
/// ordering specified by `O`.
///
/// Returns a [`VolLengthError`] if the number of elements does not match
/// [`bounds.volume()`](GridAab::volume).
pub fn from_elements(bounds: GridAab, elements: impl Into<C>) -> Result<Self, VolLengthError> {
let elements = elements.into();
if elements.len() == bounds.volume() {
Ok(Vol {
bounds,
ordering: O::default(),
contents: elements,
})
} else {
Err(VolLengthError {
input_length: elements.len(),
bounds,
})
}
}

/// Constructs a [`Vol<Box<[V]>>`] from nested Rust arrays in [Z][Y][X] order with the Y axis
/// mirrored. The result's bounds's lower bounds are zero.
///
Expand Down

0 comments on commit 03170ea

Please sign in to comment.