tvm-ffi 0.1.16

tvm-ffi rust support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
//! [`Array`], an immutable sequence with copy-on-write updates.

use std::fmt::Debug;
use std::marker::PhantomData;

use crate::IntoArg;
use crate::any::TryFromTemp;
use crate::collections::list::seq_cells;
use crate::derive::Object;
use crate::object::ObjectCoreWithExtraItems;
use crate::object::{Object, ObjectArc};
use crate::type_traits::{ContainerElement, RefFromAnyUnchecked, TypeSubsumes};
use crate::{Any, AnyCompatible, AnyView, ObjectRefCore};
use tvm_ffi_sys::TVMFFITypeIndex as TypeIndex;
use tvm_ffi_sys::{TVMFFIAny, TVMFFIObject};

/// The object behind an [`Array`], laid out as the C++ `ArrayObj`.
#[repr(C)]
#[derive(Object)]
#[object(type_key = "ffi.Array", unsafe type_index = TypeIndex::kTVMFFIArray)]
pub struct ArrayObj {
    pub(crate) object: Object,
    /// The element buffer, `capacity` `TVMFFIAny` cells.
    pub data: *mut core::ffi::c_void,
    /// The number of initialized elements.
    pub size: i64,
    /// The number of cells in the buffer.
    pub capacity: i64,
    /// Optional custom deleter for the data pointer.
    pub data_deleter: Option<unsafe extern "C" fn(*mut core::ffi::c_void)>,
}

// A Rust-allocated array holds `capacity` cells inline, of which the first `size`
// are initialized.
unsafe impl ObjectCoreWithExtraItems for ArrayObj {
    type ExtraItem = TVMFFIAny;
    fn extra_items_count(this: &Self) -> usize {
        this.capacity as usize
    }
}

// Runs only for a Rust-allocated array; one allocated in C++ is released by its
// own deleter.
impl Drop for ArrayObj {
    fn drop(&mut self) {
        let cells = self.data as *const TVMFFIAny;
        for i in 0..self.size as usize {
            // SAFETY: the first `size` cells are initialized and owned by the array.
            drop(unsafe { Any::from_raw_ffi_any(*cells.add(i)) });
        }
    }
}

/// A reference-counted sequence of `T` with value semantics, passed across
/// languages as `ffi.Array`.
///
/// Cloning an `Array` is cheap: it shares the same elements. The mutators
/// ([`push`], [`insert`], [`pop`], [`truncate`], [`set`]) are copy-on-write:
/// they copy the elements when the array is shared, and write in place when
/// this handle is the only reference, so other handles never see the change.
/// Use [`List`] for a sequence whose changes all handles see.
///
/// [`List`]: crate::List
/// [`push`]: Array::push
/// [`insert`]: Array::insert
/// [`pop`]: Array::pop
/// [`truncate`]: Array::truncate
/// [`set`]: Array::set
///
/// # Examples
///
/// ```
/// use tvm_ffi::Array;
///
/// let mut array = Array::from_vec(vec![1_i64, 2, 3]);
/// assert_eq!(array.len(), 3);
/// assert_eq!(array.get(1), Some(2));
/// assert_eq!(array.get(3), None);
///
/// let snapshot = array.clone();
/// array.push(4);
/// assert_eq!(array.len(), 4);
/// assert_eq!(snapshot.len(), 3);
///
/// let doubled: Array<i64> = array.iter().map(|x| x * 2).collect();
/// assert_eq!(doubled.iter().copied().collect::<Vec<_>>(), vec![2, 4, 6, 8]);
/// ```
#[repr(C)]
#[derive(Clone)]
pub struct Array<T> {
    data: ObjectArc<ArrayObj>,
    _marker: PhantomData<T>,
}

// SAFETY: a shared `Array` is never mutated (mutators copy unless this handle
// is unique), so sharing it is as safe as sharing its elements.
unsafe impl<T: Send + Sync> Send for Array<T> {}
unsafe impl<T: Send + Sync> Sync for Array<T> {}

impl<T: ContainerElement + Clone> Debug for Array<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let full_name = std::any::type_name::<T>();
        let short_name = full_name.split("::").last().unwrap_or(full_name);
        write!(f, "Array<{}>[{}]", short_name, self.len())
    }
}

/// Formats the array as its `repr`; see [`AnyView`].
impl<T: ContainerElement + Clone> std::fmt::Display for Array<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Display::fmt(&AnyView::from_object(&**ObjectRefCore::data(self)), f)
    }
}

impl<T: ContainerElement + Clone> Default for Array<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: ContainerElement + Clone> From<Vec<T>> for Array<T> {
    fn from(items: Vec<T>) -> Self {
        Self::from_vec(items)
    }
}

impl<T: ContainerElement + Clone, const N: usize> From<[T; N]> for Array<T> {
    fn from(items: [T; N]) -> Self {
        Self::from_vec(items.into())
    }
}

impl<T: ContainerElement + Clone> From<&[T]> for Array<T> {
    fn from(items: &[T]) -> Self {
        Self::from_vec(items.to_vec())
    }
}

/// A reference-count bump, as `From<&T> for T` of an object reference.
impl<T: ContainerElement + Clone> From<&Array<T>> for Array<T> {
    #[inline]
    fn from(value: &Array<T>) -> Self {
        value.clone()
    }
}

impl<T, U: TypeSubsumes<T>> IntoArg<Array<U>> for Array<T> {
    type Output = Array<U>;
    #[inline(always)]
    fn into_arg(self) -> Array<U> {
        self.upcast()
    }
    #[inline(always)]
    fn into_owned(self) -> Array<U> {
        self.upcast()
    }
}

impl<'a, T, U: TypeSubsumes<T> + ContainerElement + Clone + 'a> IntoArg<Array<U>> for &'a Array<T> {
    type Output = &'a Array<U>;
    #[inline(always)]
    fn into_arg(self) -> &'a Array<U> {
        self.upcast_ref()
    }
    #[inline(always)]
    fn into_owned(self) -> Array<U> {
        self.upcast_ref().clone()
    }
}

/// An array upcasts to the root [`ObjectRef`](crate::object::ObjectRef).
impl<T: ContainerElement + Clone> From<Array<T>> for crate::object::ObjectRef {
    #[inline]
    fn from(value: Array<T>) -> Self {
        crate::object::into_root_object_ref(value)
    }
}

/// The strict downcast from the root [`ObjectRef`](crate::object::ObjectRef).
impl<T: ContainerElement + Clone> TryFrom<crate::object::ObjectRef> for Array<T> {
    type Error = crate::error::Error;
    #[inline]
    fn try_from(value: crate::object::ObjectRef) -> crate::error::Result<Self> {
        crate::object::root_try_into(value)
    }
}

impl<T: ContainerElement + Clone> TryFrom<&crate::object::ObjectRef> for Array<T> {
    type Error = crate::error::Error;
    #[inline]
    fn try_from(value: &crate::object::ObjectRef) -> crate::error::Result<Self> {
        crate::object::root_try_into_ref(value)
    }
}

impl<T> Array<T> {
    /// View this array as an `Array<U>` without a copy, as C++
    /// `Array<U>(const Array<T>&)` does for a base `U` of `T`.
    ///
    /// It is a method, not `From`: `From<Array<T>> for Array<U>` would overlap
    /// `From<T> for T` at `T = U` (E0119), and a crate that defines `T` and `U`
    /// cannot implement it for `Array` either (orphan rule, E0117).
    ///
    /// ```
    /// use tvm_ffi::object::{ObjectRef, ObjectRefCore};
    /// use tvm_ffi::{Array, Shape};
    ///
    /// let shapes = Array::from([Shape::from([1_i64, 2])]);
    /// let objects: Array<ObjectRef> = shapes.clone().upcast();
    /// assert!(objects[0].same_as(&shapes[0]));
    /// ```
    #[inline(always)]
    pub fn upcast<U: TypeSubsumes<T>>(self) -> Array<U> {
        Array {
            data: self.data,
            _marker: PhantomData,
        }
    }

    /// Borrow this array as an `Array<U>`, as [`upcast`](Self::upcast) does.
    #[inline(always)]
    pub fn upcast_ref<U: TypeSubsumes<T>>(&self) -> &Array<U> {
        // SAFETY: `Array<T>` and `Array<U>` are both `repr(C)` over one
        // `ObjectArc<ArrayObj>`, and every `T` cell is a valid `U` cell.
        unsafe { &*(self as *const Self as *const Array<U>) }
    }
}

unsafe impl<T: ContainerElement + Clone> ObjectRefCore for Array<T> {
    type ContainerType = ArrayObj;

    fn data(this: &Self) -> &ObjectArc<Self::ContainerType> {
        &this.data
    }

    fn into_data(this: Self) -> ObjectArc<Self::ContainerType> {
        this.data
    }

    unsafe fn from_data(data: ObjectArc<Self::ContainerType>) -> Self {
        Self {
            data,
            _marker: PhantomData,
        }
    }
}

impl<T: ContainerElement + Clone> Array<T> {
    /// Create an empty array.
    ///
    /// # Returns
    ///
    /// The empty array.
    ///
    /// See also [`Array::from_vec`].
    pub fn new() -> Self {
        Self::from_vec(Vec::new())
    }

    /// Create an array holding `items`, like `Array::from(items)`.
    ///
    /// # Arguments
    ///
    /// * `items` - The elements.
    ///
    /// # Returns
    ///
    /// The array.
    ///
    /// # Examples
    ///
    /// ```
    /// use tvm_ffi::Array;
    ///
    /// let array = Array::from_vec(vec![1_i64, 2, 3]);
    /// assert_eq!(array[1], 2);
    /// ```
    pub fn from_vec(items: Vec<T>) -> Self {
        let capacity = items.len();
        Self::new_with_capacity(items, capacity)
    }

    /// Append `value` to the end.
    ///
    /// See also [`List::push`](crate::List::push).
    pub fn push(&mut self, value: T) {
        let size = self.len();
        let container = self.make_mut(size + 1);
        // SAFETY: `make_mut` left room for one more cell past `size`.
        unsafe { write_cell((container.data as *mut TVMFFIAny).add(size), value) };
        container.size += 1;
    }

    /// Insert `value` at `index`, shifting the elements after it, as
    /// [`Vec::insert`].
    ///
    /// # Panics
    ///
    /// Panics if `index > len`.
    ///
    /// See also [`List::insert`](crate::List::insert).
    pub fn insert(&mut self, index: usize, value: T) {
        let size = self.len();
        if index > size {
            panic!("insertion index (is {index}) should be <= len (is {size})");
        }
        let container = self.make_mut(size + 1);
        // SAFETY: `make_mut` left room for one more cell; the tail moves up by one.
        unsafe {
            let cell = (container.data as *mut TVMFFIAny).add(index);
            std::ptr::copy(cell, cell.add(1), size - index);
            write_cell(cell, value);
        }
        container.size += 1;
    }

    /// Remove and return the last element, or `None` if the array is empty,
    /// as [`Vec::pop`].
    ///
    /// See also [`List::pop`](crate::List::pop).
    pub fn pop(&mut self) -> Option<T> {
        let size = self.len();
        if size == 0 {
            return None;
        }
        let container = self.make_mut(size);
        container.size -= 1;
        // SAFETY: the last cell is initialized, holds a `T`, and is no longer
        // counted by `size`, so it is moved out exactly once.
        unsafe {
            let mut cell = std::ptr::read((container.data as *const TVMFFIAny).add(size - 1));
            debug_assert!(T::container_check_any_strict(&cell));
            Some(T::container_move_from_any_after_check(&mut cell))
        }
    }

    /// Shorten the array to `len` elements, dropping the rest; a no-op when it
    /// is already that short.
    ///
    /// See also [`List::truncate`](crate::List::truncate).
    pub fn truncate(&mut self, len: usize) {
        let size = self.len();
        if len >= size {
            return;
        }
        let container = self.make_mut(size);
        // Shrink `size` first, so a panicking element drop leaks rather than
        // double-frees the remaining cells.
        container.size = len as i64;
        let cells = container.data as *const TVMFFIAny;
        for i in len..size {
            // SAFETY: cells `len..size` are initialized and no longer counted.
            drop(unsafe { Any::from_raw_ffi_any(*cells.add(i)) });
        }
    }

    /// Replace the element at `index`, as `vec[index] = value`.
    ///
    /// # Panics
    ///
    /// Panics if `index >= len`.
    ///
    /// See also [`List::set`](crate::List::set).
    pub fn set(&mut self, index: usize, value: T) {
        let size = self.len();
        if index >= size {
            panic!("Index out of bounds: the len is {size} but the index is {index}");
        }
        let container = self.make_mut(size);
        // SAFETY: the cell at `index` is initialized; its old value is dropped
        // after the new one is written.
        unsafe {
            let cell = (container.data as *mut TVMFFIAny).add(index);
            let old = Any::from_raw_ffi_any(std::ptr::read(cell));
            write_cell(cell, value);
            drop(old);
        }
    }

    /// Return the number of elements.
    ///
    /// # Returns
    ///
    /// The number of elements.
    pub fn len(&self) -> usize {
        self.data.size as usize
    }

    /// Return `true` if the array has no elements.
    ///
    /// # Returns
    ///
    /// `true` if the array has no elements.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Return the element at `index`, or `None` if it is out of bounds, as
    /// [`slice::get`]. `array[index]` borrows it instead, and panics when out
    /// of bounds.
    pub fn get(&self, index: usize) -> Option<T> {
        let cell = self.cells().get(index)?;
        // SAFETY: every cell of an `Array<T>` holds a `T`, checked when the
        // array is built.
        Some(unsafe {
            debug_assert!(T::container_check_any_strict(cell));
            T::container_copy_from_any_view_after_check(cell)
        })
    }

    /// An array of `items` with room for `capacity` elements.
    fn new_with_capacity(items: Vec<T>, capacity: usize) -> Self {
        let size = items.len();

        // Allocate with capacity
        // SAFETY: the cells are written below; those past `size` are never read.
        let mut arc = unsafe {
            ObjectArc::<ArrayObj>::new_with_extra_items(ArrayObj {
                object: Object::new(),
                data: core::ptr::null_mut(),
                size: size as i64,
                capacity: capacity as i64,
                data_deleter: None,
            })
        };

        unsafe {
            let container = ObjectArc::as_mut_ptr(&mut arc);
            let base_ptr = ArrayObj::extra_items_ptr(container);
            (*container).data = base_ptr as *mut _;

            for (i, item) in items.into_iter().enumerate() {
                write_cell(base_ptr.add(i), item);
            }
        }
        // SAFETY: `arc` was allocated and initialized above as an `Array<T>`.
        unsafe { Self::from_data(arc) }
    }

    /// Make this handle the only reference to its cells, with room for at least
    /// `capacity` of them, and return the container to edit in place.
    fn make_mut(&mut self, capacity: usize) -> &mut ArrayObj {
        // As C++ `Array::CopyOnWrite(reserve)`.
        let unique = ObjectArc::is_unique(&self.data);
        let current = self.data.capacity as usize;
        if !unique || capacity > current {
            let capacity = if capacity > current {
                (current * 2).max(capacity)
            } else {
                current
            };
            self.copy_to_new_buffer(capacity, unique);
        }
        // SAFETY: this handle is now the only reference, so no one else observes
        // the container while it is mutably borrowed.
        unsafe { &mut *ObjectArc::as_mut_ptr(&mut self.data) }
    }

    /// Replace `self` with a unique array of `capacity` cells holding the same
    /// elements: moved out of a `unique` source, otherwise each re-referenced.
    fn copy_to_new_buffer(&mut self, capacity: usize, unique: bool) {
        let size = self.len();
        let mut grown = Self::new_with_capacity(Vec::new(), capacity);
        let source = self.as_container().data as *const TVMFFIAny;
        // SAFETY: `grown` is fresh with room for `size` cells. A unique source
        // forgets the cells it moved; a shared one keeps its own references.
        unsafe {
            let target = ObjectArc::as_mut_ptr(&mut grown.data);
            let cells = (*target).data as *mut TVMFFIAny;
            if unique {
                std::ptr::copy_nonoverlapping(source, cells, size);
                (*ObjectArc::as_mut_ptr(&mut self.data)).size = 0;
            } else {
                for i in 0..size {
                    let copy = Any::from(AnyView::from_raw_ffi_any(*source.add(i)));
                    std::ptr::write(cells.add(i), Any::into_raw_ffi_any(copy));
                }
            }
            (*target).size = size as i64;
        }
        *self = grown;
    }

    /// The cells of the array, one per element.
    #[inline]
    fn cells(&self) -> &[TVMFFIAny] {
        let container = self.as_container();
        if container.size == 0 {
            return &[];
        }
        // SAFETY: a non-empty array owns `size` initialized cells at `data`,
        // and an `Array` is immutable while it is borrowed.
        unsafe {
            std::slice::from_raw_parts(container.data as *const TVMFFIAny, container.size as usize)
        }
    }

    #[inline]
    fn as_container(&self) -> &ArrayObj {
        // SAFETY: `self.data` keeps the array object alive while it is borrowed.
        unsafe { &*ObjectArc::as_ptr(&self.data) }
    }
}

// -----------------------------------------------------------------------------
// Index
// -----------------------------------------------------------------------------

/// Lends `&T` from the array's cell in place. Every cell of an `Array<T>`
/// holds a `T` (checked when the array is built), so only a debug check runs.
impl<T: ContainerElement + Clone + RefFromAnyUnchecked> std::ops::Index<usize> for Array<T> {
    type Output = T;

    fn index(&self, index: usize) -> &T {
        let container = self.as_container();
        let len = container.size as usize;
        if index >= len {
            panic!(
                "Index out of bounds: the len is {} but the index is {}",
                len, index
            );
        }
        unsafe {
            let cell = &*(container.data as *const TVMFFIAny).add(index);
            debug_assert!(T::container_check_any_strict(cell));
            T::ref_from_any_unchecked(cell)
        }
    }
}

// -----------------------------------------------------------------------------
// Iterators
// -----------------------------------------------------------------------------

impl<T: ContainerElement + Clone + RefFromAnyUnchecked> Array<T> {
    /// Return an iterator that borrows each element in place, without a
    /// reference-count bump, as `array[i]` does.
    ///
    /// Element types stored in another representation, such as `bool` or
    /// `i32` (held as `i64`), iterate by value instead: `for x in array`.
    ///
    /// ```
    /// use tvm_ffi::Array;
    /// let array = Array::from_vec(vec![1_i64, 2, 3]);
    /// assert_eq!(array.iter().sum::<i64>(), 6);
    /// ```
    pub fn iter(&self) -> Iter<'_, T> {
        Iter {
            cells: self.cells().iter(),
            _marker: std::marker::PhantomData,
        }
    }
}

/// An iterator that borrows the elements of an [`Array`] in place.
pub struct Iter<'a, T> {
    cells: std::slice::Iter<'a, TVMFFIAny>,
    _marker: std::marker::PhantomData<&'a T>,
}

impl<'a, T: ContainerElement + RefFromAnyUnchecked> Iterator for Iter<'a, T> {
    type Item = &'a T;

    #[inline]
    fn next(&mut self) -> Option<&'a T> {
        self.cells.next().map(|cell| unsafe {
            // SAFETY: every cell of an `Array<T>` holds a `T`, checked when the
            // array is built.
            debug_assert!(T::container_check_any_strict(cell));
            T::ref_from_any_unchecked(cell)
        })
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.cells.size_hint()
    }
}

impl<T: ContainerElement + RefFromAnyUnchecked> DoubleEndedIterator for Iter<'_, T> {
    #[inline]
    fn next_back(&mut self) -> Option<Self::Item> {
        self.cells.next_back().map(|cell| unsafe {
            debug_assert!(T::container_check_any_strict(cell));
            T::ref_from_any_unchecked(cell)
        })
    }
}

impl<T: ContainerElement + RefFromAnyUnchecked> ExactSizeIterator for Iter<'_, T> {}

impl<'a, T: ContainerElement + Clone + RefFromAnyUnchecked> IntoIterator for &'a Array<T> {
    type Item = &'a T;
    type IntoIter = Iter<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

/// An iterator that yields the elements of an [`Array`] by value.
pub struct IntoIter<T> {
    array: Array<T>,
    index: usize,
}

impl<T: ContainerElement + Clone> Iterator for IntoIter<T> {
    type Item = T;

    #[inline]
    fn next(&mut self) -> Option<T> {
        let cell = self.array.cells().get(self.index)?;
        self.index += 1;
        // SAFETY: every cell of an `Array<T>` holds a `T`, checked when the
        // array is built.
        Some(unsafe {
            debug_assert!(T::container_check_any_strict(cell));
            T::container_copy_from_any_view_after_check(cell)
        })
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.array.len() - self.index;
        (remaining, Some(remaining))
    }
}

impl<T: ContainerElement + Clone> ExactSizeIterator for IntoIter<T> {}

/// Iterate by value, for every element type. Cloning an `Array` first costs one
/// reference-count bump for the whole array.
impl<T: ContainerElement + Clone> IntoIterator for Array<T> {
    type Item = T;
    type IntoIter = IntoIter<T>;

    fn into_iter(self) -> Self::IntoIter {
        IntoIter {
            array: self,
            index: 0,
        }
    }
}

impl<T: ContainerElement + Clone> FromIterator<T> for Array<T> {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let items: Vec<T> = iter.into_iter().collect();
        Self::from_vec(items)
    }
}

// -----------------------------------------------------------------------------
// Any conversions
// -----------------------------------------------------------------------------

unsafe impl<T> AnyCompatible for Array<T>
where
    T: ContainerElement + Clone,
{
    #[inline]
    unsafe fn is_undefined(field: *const Self) -> bool {
        unsafe { ObjectArc::is_null(std::ptr::addr_of!((*field).data)) }
    }

    fn type_str() -> String {
        format!("Array<{}>", T::container_type_str())
    }

    fn type_schema() -> String {
        crate::type_traits::type_schema(
            <ArrayObj as crate::ObjectCore>::TYPE_KEY,
            &[T::container_type_schema()],
        )
    }

    unsafe fn check_any_strict(data: &TVMFFIAny) -> bool {
        if data.type_index != TypeIndex::kTVMFFIArray as i32 {
            return false;
        }

        let container = unsafe { &*(data.data_union.v_obj as *const ArrayObj) };
        let base_ptr = container.data as *const TVMFFIAny;
        for i in 0..container.size {
            let elem_any = unsafe { &*base_ptr.add(i as usize) };
            if unsafe { !T::container_check_any_strict(elem_any) } {
                return false;
            }
        }
        true
    }

    unsafe fn match_constraints(data: &TVMFFIAny) -> bool {
        let container = unsafe { &*(data.data_union.v_obj as *const ArrayObj) };
        let base_ptr = container.data as *const TVMFFIAny;
        for i in 0..container.size {
            let elem_any = unsafe { &*base_ptr.add(i as usize) };
            if unsafe { !T::container_match_constraints(elem_any) } {
                return false;
            }
        }
        true
    }

    unsafe fn copy_to_any_view(src: &Self, data: &mut TVMFFIAny) {
        data.type_index = TypeIndex::kTVMFFIArray as i32;
        data.data_union.v_obj = ObjectArc::as_ptr(Self::data(src)) as *mut TVMFFIObject;
        data.small_str_len = 0;
    }

    unsafe fn move_to_any(src: Self, data: &mut TVMFFIAny) {
        data.type_index = TypeIndex::kTVMFFIArray as i32;
        data.data_union.v_obj = ObjectArc::into_raw(Self::into_data(src)) as *mut TVMFFIObject;
        data.small_str_len = 0;
    }

    unsafe fn copy_from_any_view_after_check(data: &TVMFFIAny) -> Self {
        let ptr = unsafe { data.data_union.v_obj as *const ArrayObj };
        unsafe { crate::object::unsafe_::inc_ref(ptr as *mut TVMFFIObject) };
        unsafe { Self::from_data(ObjectArc::from_raw(ptr)) }
    }

    unsafe fn move_from_any_after_check(data: &mut TVMFFIAny) -> Self {
        let ptr = unsafe { data.data_union.v_obj as *const ArrayObj };
        let obj = unsafe { Self::from_data(ObjectArc::from_raw(ptr)) };

        data.type_index = TypeIndex::kTVMFFINone as i32;
        data.data_union.v_int64 = 0;

        obj
    }

    unsafe fn try_cast_from_any_view(data: &TVMFFIAny) -> Result<Self, ()> {
        // Mirrors C++ `SeqTypeTraitsBase::TryCastFromAnyView`: `ffi.List` is
        // the accepted other sequence type, and is always copied.
        if data.type_index != TypeIndex::kTVMFFIArray as i32
            && data.type_index != TypeIndex::kTVMFFIList as i32
        {
            return Err(());
        }

        // Fast path: an array whose elements all match shares the reference.
        if unsafe { <Self as AnyCompatible>::check_any_strict(data) } {
            return Ok(unsafe { <Self as AnyCompatible>::copy_from_any_view_after_check(data) });
        }

        // Slow path: convert element by element into a new array.
        let cells = unsafe { seq_cells(data) };
        let mut items = Vec::with_capacity(cells.len());
        for cell in cells {
            unsafe { items.push(T::container_try_cast_from_any_view(cell)?) };
        }
        Ok(Array::from_vec(items))
    }
}

// The cell's payload word is the single object pointer of the handle.
unsafe impl<T> crate::type_traits::RefFromAnyUnchecked for Array<T>
where
    T: ContainerElement + Clone,
{
    #[inline(always)]
    unsafe fn ref_from_any_unchecked(cell: &TVMFFIAny) -> &Self {
        unsafe { &*(core::ptr::addr_of!(cell.data_union) as *const Self) }
    }
}

unsafe impl<T> crate::type_traits::DowncastRefFromAny for Array<T>
where
    T: ContainerElement + Clone,
{
    #[inline(always)]
    unsafe fn downcast_ref_from_any(cell: &TVMFFIAny) -> Option<&Self> {
        unsafe { crate::type_traits::downcast_ref_from_any_in_place::<Self>(cell) }
    }
}

unsafe impl<T> crate::type_traits::DowncastRefFromObject for Array<T>
where
    T: ContainerElement + Clone,
{
    #[inline(always)]
    unsafe fn downcast_ref_from_object(place: &*mut TVMFFIObject) -> Option<&Self> {
        unsafe { crate::type_traits::downcast_ref_from_object_in_place::<Self>(place) }
    }
}

impl<'b, T> TryFrom<&'b Any> for Array<T>
where
    T: ContainerElement + Clone,
{
    type Error = crate::error::Error;

    fn try_from(value: &'b Any) -> Result<Self, Self::Error> {
        let temp: TryFromTemp<Self> = TryFromTemp::try_from(value)?;
        Ok(TryFromTemp::into_value(temp))
    }
}

impl<T> TryFrom<Any> for Array<T>
where
    T: ContainerElement + Clone,
{
    type Error = crate::error::Error;

    fn try_from(value: Any) -> Result<Self, Self::Error> {
        let temp: TryFromTemp<Self> = TryFromTemp::try_from(value)?;
        Ok(TryFromTemp::into_value(temp))
    }
}

impl<'a, T> TryFrom<AnyView<'a>> for Array<T>
where
    T: ContainerElement + Clone,
{
    type Error = crate::error::Error;

    fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error> {
        let temp: TryFromTemp<Self> = TryFromTemp::try_from(value)?;
        Ok(TryFromTemp::into_value(temp))
    }
}

// -----------------------------------------------------------------------------
// Helpers
// -----------------------------------------------------------------------------

/// Move `value` into the uninitialized `cell`.
unsafe fn write_cell<T: ContainerElement>(cell: *mut TVMFFIAny, value: T) {
    let mut raw = TVMFFIAny::new();
    unsafe { T::container_move_to_any(value, &mut raw) };
    unsafe { std::ptr::write(cell, raw) };
}