Skip to content

Commit 8918b9d

Browse files
committed
feat: Add Extend and FromIterator traits to ZyntaxArray
- Implement Extend<T> for ZyntaxArray<T> to append from iterators - Implement FromIterator<T> for ZyntaxArray<T> for collect() support - Fix async_runtime_tests.rs for PromiseState::Cancelled variant - Update INTEGRATION_PLAN.md to mark collection interop as complete
1 parent b2ce815 commit 8918b9d

3 files changed

Lines changed: 46 additions & 4 deletions

File tree

crates/zyntax_embed/src/array.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,25 @@ impl<'a, T: Copy> IntoIterator for &'a mut ZyntaxArray<T> {
380380
unsafe impl<T: Copy + Send> Send for ZyntaxArray<T> {}
381381
unsafe impl<T: Copy + Sync> Sync for ZyntaxArray<T> {}
382382

383+
impl<T: Copy> Extend<T> for ZyntaxArray<T> {
384+
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
385+
for item in iter {
386+
self.push(item);
387+
}
388+
}
389+
}
390+
391+
impl<T: Copy> std::iter::FromIterator<T> for ZyntaxArray<T> {
392+
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
393+
let iter = iter.into_iter();
394+
let (lower, upper) = iter.size_hint();
395+
let capacity = upper.unwrap_or(lower).max(8);
396+
let mut arr = Self::with_capacity(capacity);
397+
arr.extend(iter);
398+
arr
399+
}
400+
}
401+
383402
#[cfg(test)]
384403
mod tests {
385404
use super::*;
@@ -459,4 +478,24 @@ mod tests {
459478
arr[1] = 42;
460479
assert_eq!(arr.as_slice(), &[1, 42, 3]);
461480
}
481+
482+
#[test]
483+
fn test_extend() {
484+
let mut arr: ZyntaxArray<i32> = [1, 2].into();
485+
arr.extend([3, 4, 5]);
486+
assert_eq!(arr.as_slice(), &[1, 2, 3, 4, 5]);
487+
}
488+
489+
#[test]
490+
fn test_from_iterator() {
491+
let arr: ZyntaxArray<i32> = (0..5).collect();
492+
assert_eq!(arr.as_slice(), &[0, 1, 2, 3, 4]);
493+
}
494+
495+
#[test]
496+
fn test_collect_after_map() {
497+
let arr: ZyntaxArray<i32> = [1, 2, 3].into();
498+
let doubled: ZyntaxArray<i32> = arr.as_slice().iter().map(|x| x * 2).collect();
499+
assert_eq!(doubled.as_slice(), &[2, 4, 6]);
500+
}
462501
}

crates/zyntax_embed/tests/async_runtime_tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,9 @@ async fn compute_value(x: i32) i32 {
10951095
PromiseState::Failed(err) => {
10961096
panic!("Async function failed: {}", err);
10971097
}
1098+
PromiseState::Cancelled => {
1099+
panic!("Async function was cancelled");
1100+
}
10981101
PromiseState::Pending => {
10991102
if polls > 10 {
11001103
panic!("Too many polls");

docs/INTEGRATION_PLAN.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,12 @@ fn test_array_push() {
265265
|------|--------|--------|----------|--------|
266266
| Type tags in zrtl_macros | High | Medium | P0 | ✅ Done (in zrtl crate) |
267267
| DynamicBox in zrtl_macros | High | Medium | P0 | ✅ Done (in zrtl crate) |
268-
| TypeRegistry in embed | High | Low | P0 | 🔲 Pending |
268+
| TypeRegistry in embed | High | Low | P0 | ✅ Done (re-exported) |
269269
| ZRTL plugin loading | High | Medium | P1 | ✅ Done |
270270
| Variadic function calls | High | High | P1 | ✅ Done (0-8 args) |
271271
| GenericBox support | Medium | Medium | P1 | ✅ Done (in zrtl crate) |
272272
| Iterator traits | Medium | Medium | P2 | ✅ Done |
273-
| Collection interop | Medium | Low | P2 | 🔲 Pending |
273+
| Collection interop | Medium | Low | P2 | ✅ Done |
274274
| Async state machine | Medium | High | P2 | ✅ Done |
275275
| Cancellation | Low | Medium | P3 | ✅ Done |
276276
| Promise combinators | Low | Low | P3 | ✅ Done |
@@ -286,7 +286,7 @@ fn test_array_push() {
286286
1. ✅ Add `TypeCategory`, `TypeFlags`, `TypeTag` to `zrtl` crate
287287
2. ✅ Add `DynamicBox` struct and accessors to `zrtl` crate
288288
3. ✅ Add `#[derive(ZrtlType)]` macro to `zrtl_macros`
289-
4. 🔲 Re-export `TypeRegistry` in `zyntax_embed`
289+
4. Re-export `TypeRegistry` in `zyntax_embed`
290290

291291
### Sprint 2: Runtime Integration ✅ COMPLETE
292292

@@ -298,7 +298,7 @@ fn test_array_push() {
298298

299299
1. ✅ Add `ZrtlIterable` and `ZrtlIterator` traits
300300
2. ✅ Implement for `ZyntaxArray` and `ZyntaxString`
301-
3. 🔲 Add collection conversion helpers (partial - basic From/Into impls exist)
301+
3. Add collection conversion helpers (Extend, FromIterator, From, Into)
302302

303303
### Sprint 4: Async & Polish ✅ COMPLETE
304304

0 commit comments

Comments
 (0)