diff --git a/crates/sats/src/layout.rs b/crates/sats/src/layout.rs index b9e8f919899..db615924d19 100644 --- a/crates/sats/src/layout.rs +++ b/crates/sats/src/layout.rs @@ -672,7 +672,7 @@ fn product_type_layout>(ty: ProductTyp let mut max_child_align = 1; let mut fixed = true; - let elements = Vec::from(ty.elements) + let elements = ty .into_iter() .map(|elem| { let layout_type: AlgebraicTypeLayout = elem.algebraic_type.into(); diff --git a/crates/sats/src/product_type.rs b/crates/sats/src/product_type.rs index 0131c9365b7..b5758dfd1cb 100644 --- a/crates/sats/src/product_type.rs +++ b/crates/sats/src/product_type.rs @@ -66,6 +66,24 @@ impl Deref for ProductType { } } +impl IntoIterator for ProductType { + type Item = ProductTypeElement; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + Vec::from(self.elements).into_iter() + } +} + +impl<'a> IntoIterator for &'a ProductType { + type Item = &'a ProductTypeElement; + type IntoIter = std::slice::Iter<'a, ProductTypeElement>; + + fn into_iter(self) -> Self::IntoIter { + self.elements.iter() + } +} + impl std::fmt::Debug for ProductType { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str("ProductType ")?; @@ -358,3 +376,26 @@ where } impl<'a, I> ExactSizeIterator for ElementValuesWithType<'a, I> where I: ExactSizeIterator {} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn product_type_into_iterator() { + let product = ProductType::from([AlgebraicType::U8, AlgebraicType::String]); + let expected = vec![AlgebraicType::U8, AlgebraicType::String]; + + let borrowed = (&product) + .into_iter() + .map(|element| element.algebraic_type.clone()) + .collect::>(); + let owned = product + .into_iter() + .map(|element| element.algebraic_type) + .collect::>(); + + assert_eq!(borrowed, expected); + assert_eq!(owned, expected); + } +} diff --git a/crates/sats/src/proptest.rs b/crates/sats/src/proptest.rs index 503d804f6a6..12b02033351 100644 --- a/crates/sats/src/proptest.rs +++ b/crates/sats/src/proptest.rs @@ -156,8 +156,7 @@ pub fn generate_algebraic_value(ty: AlgebraicType) -> impl Strategy impl Strategy { - Vec::from(ty.elements) - .into_iter() + ty.into_iter() .map(|elem| generate_algebraic_value(elem.algebraic_type)) .collect::>() .prop_map_into()