Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/sats/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ fn product_type_layout<C: FromIterator<ProductTypeElementLayout>>(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();
Expand Down
41 changes: 41 additions & 0 deletions crates/sats/src/product_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ impl Deref for ProductType {
}
}

impl IntoIterator for ProductType {
type Item = ProductTypeElement;
type IntoIter = std::vec::IntoIter<ProductTypeElement>;

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 ")?;
Expand Down Expand Up @@ -358,3 +376,26 @@ where
}

impl<'a, I> ExactSizeIterator for ElementValuesWithType<'a, I> where I: ExactSizeIterator<Item = &'a AlgebraicValue> {}

#[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::<Vec<_>>();
let owned = product
.into_iter()
.map(|element| element.algebraic_type)
.collect::<Vec<_>>();

assert_eq!(borrowed, expected);
assert_eq!(owned, expected);
}
}
3 changes: 1 addition & 2 deletions crates/sats/src/proptest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ pub fn generate_algebraic_value(ty: AlgebraicType) -> impl Strategy<Value = Alge

/// Generates a `ProductValue` typed at `ty`.
pub fn generate_product_value(ty: ProductType) -> impl Strategy<Value = ProductValue> {
Vec::from(ty.elements)
.into_iter()
ty.into_iter()
.map(|elem| generate_algebraic_value(elem.algebraic_type))
.collect::<Vec<_>>()
.prop_map_into()
Expand Down