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
/*
 * 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.
 */
//! [`Any`] and [`AnyView`]: owned and borrowed type-erased FFI values.

use crate::error::Error;
use crate::object;
use crate::type_traits::AnyCompatible;
use tvm_ffi_sys::TVMFFITypeIndex as TypeIndex;
use tvm_ffi_sys::{TVMFFIAny, TVMFFIAnyViewToOwnedAny};

/// A borrowed, type-erased FFI value.
///
/// `AnyView` holds any FFI value, such as an integer, a string or an object,
/// without owning it: it does not retain objects, and its lifetime `'a` keeps
/// it from outliving the value it borrows. It is `Copy` and is the argument
/// type of FFI calls. Convert it into an [`Any`] to own the value.
///
/// # Examples
///
/// ```
/// use tvm_ffi::{Any, AnyView, Array};
///
/// let array = Array::from_vec(vec![1_i64, 2, 3]);
/// let view = AnyView::from(&array);
/// assert_eq!(view.downcast_ref::<Array<i64>>().unwrap().len(), 3);
///
/// let owned = Any::from(view);
/// assert_eq!(Array::<i64>::try_from(owned)?.len(), 3);
/// # Ok::<(), tvm_ffi::Error>(())
/// ```
#[derive(Copy, Clone)]
#[repr(C)]
pub struct AnyView<'a> {
    data: TVMFFIAny,
    /// needs to explicit mark lifetime to avoid lifetime mismatch
    _phantom: std::marker::PhantomData<&'a ()>,
}

/// An owned, type-erased FFI value.
///
/// `Any` holds any FFI value, such as an integer, a string or an object, and
/// owns it: cloning an `Any` that holds an object retains the object, and
/// dropping it releases the object. Any [`AnyCompatible`] value converts into
/// it with `From`; `TryFrom` (converting), [`downcast_ref`](Self::downcast_ref)
/// (borrowing) and [`match_any!`](crate::match_any) read it back. Borrow it as an [`AnyView`] to pass it without retaining.
///
/// The one value it does not own is a `DLTensor*` converted from an
/// [`AnyView`], as in C++: that `Any` borrows the tensor and must not outlive
/// it.
///
/// # Examples
///
/// ```
/// use tvm_ffi::Any;
///
/// let value = Any::from(42_i64);
/// assert_eq!(i64::try_from(&value)?, 42);
/// assert!(tvm_ffi::String::try_from(&value).is_err());
///
/// let text = Any::from(tvm_ffi::String::from("hello"));
/// assert_eq!(text.downcast_ref::<tvm_ffi::String>().unwrap().as_str(), "hello");
/// # Ok::<(), tvm_ffi::Error>(())
/// ```
#[repr(C)]
pub struct Any {
    data: TVMFFIAny,
}

//---------------------
// AnyView
//---------------------
impl<'a> AnyView<'a> {
    /// Create a view of `None`, the empty FFI value.
    ///
    /// # Returns
    ///
    /// A view holding `None`.
    pub fn new() -> Self {
        Self {
            data: TVMFFIAny::new(),
            _phantom: std::marker::PhantomData,
        }
    }

    /// Borrow a live managed object without acquiring ownership.
    #[doc(hidden)]
    #[inline(always)]
    pub fn from_object<T: object::ObjectCore>(value: &'a T) -> Self {
        let pointer = std::ptr::from_ref(value).cast::<tvm_ffi_sys::TVMFFIObject>();
        // ObjectCore guarantees the prefix and stable managed allocation. A
        // builder or a moved-out destructor value is not a live managed object.
        let header = unsafe { &*pointer };
        assert!(
            header.type_index >= TypeIndex::kTVMFFIStaticObjectBegin as i32
                && header.deleter.is_some()
                && header
                    .combined_ref_count
                    .load(std::sync::atomic::Ordering::Relaxed)
                    & tvm_ffi_sys::COMBINED_REF_COUNT_MASK_U32
                    != 0,
            "cannot borrow an unmanaged or dead object as AnyView"
        );
        let mut data = TVMFFIAny::new();
        data.type_index = header.type_index;
        data.data_union.v_obj = pointer.cast_mut();
        // The source borrow bounds the view; the dynamic type comes from the
        // allocation even when value borrows a base-class prefix.
        unsafe { Self::from_raw_ffi_any(data) }
    }

    /// Return the runtime type index of the viewed value.
    ///
    /// Indices below `TypeIndex::kTVMFFIStaticObjectBegin` are the built-in
    /// non-object types; the others are object types.
    ///
    /// # Returns
    ///
    /// The type index.
    #[inline(always)]
    pub fn type_index(&self) -> i32 {
        self.data.type_index
    }

    /// The registered type key of the viewed value, such as `"int"` or
    /// `"ffi.Array"`. See also [`Any::type_key`].
    #[inline]
    pub fn type_key(&self) -> &'static str {
        object::registered_type_key(self.data.type_index)
    }

    /// Whether the value is None.
    #[inline(always)]
    pub fn is_none(&self) -> bool {
        self.data.type_index == TypeIndex::kTVMFFINone as i32
    }

    /// Borrow the raw ABI representation.
    /// Copying it does not retain referenced data or extend its lifetime.
    #[inline(always)]
    pub fn as_raw_ffi_any(&self) -> &TVMFFIAny {
        &self.data
    }

    /// Construct a borrowed view from its ABI representation.
    ///
    /// # Safety
    ///
    /// `data.type_index` must describe the payload, and the caller must keep
    /// every resource it references alive for the view's complete lifetime.
    ///
    /// `kTVMFFIObjectRValueRef` carries a further obligation: `v_ptr` must
    /// point to a writable slot the caller uniquely owns, holding one strong
    /// reference. Owning the view takes that reference and writes null back
    /// through the pointer, so the view must be converted at most once and no
    /// other access to the slot may overlap the conversion.
    #[inline(always)]
    pub unsafe fn from_raw_ffi_any(data: TVMFFIAny) -> Self {
        Self {
            data,
            _phantom: std::marker::PhantomData,
        }
    }

    /// Borrow the viewed value as `T` when it holds one, without copying it.
    ///
    /// `T` is an object type such as `ArrayObj`, a managed reference such as
    /// `Array<i64>`, or a value stored in place such as `String` or `i64`. The
    /// check is strict and never converts, unlike `TryFrom`.
    /// The borrow does not change any reference count and is tied to this view,
    /// which holds its own copy of the value's cell.
    ///
    /// # Returns
    ///
    /// The borrowed value, or `None`.
    ///
    /// See also [`Any::downcast_ref`].
    #[inline(always)]
    pub fn downcast_ref<T: crate::type_traits::DowncastRefFromAny>(&self) -> Option<&T> {
        // SAFETY: the view holds a valid cell for as long as it is borrowed.
        unsafe { T::downcast_ref_from_any(&self.data) }
    }

    /// Whether the viewed value is a `T` as is, by the strict check of
    /// [`downcast_ref`](Self::downcast_ref), without conversion.
    /// See also [`Any::is`].
    #[inline(always)]
    pub fn is<T: AnyCompatible>(&self) -> bool {
        // SAFETY: the view holds a valid cell.
        unsafe { T::check_any_strict(&self.data) }
    }

    /// The strong reference count of the held object, for tests and debugging;
    /// `None` if the value is not an object.
    #[doc(hidden)]
    pub fn debug_strong_count(&self) -> Option<usize> {
        unsafe {
            if self.data.type_index >= TypeIndex::kTVMFFIStaticObjectBegin as i32 {
                Some(object::unsafe_::strong_count(self.data.data_union.v_obj))
            } else {
                None
            }
        }
    }
}

impl<'a> From<&'a object::Object> for AnyView<'a> {
    #[inline(always)]
    fn from(value: &'a object::Object) -> Self {
        Self::from_object(value)
    }
}

impl<'a, T: AnyCompatible> From<&'a T> for AnyView<'a> {
    #[inline(always)]
    fn from(value: &'a T) -> Self {
        unsafe {
            let mut data = TVMFFIAny::new();
            T::copy_to_any_view(value, &mut data);
            Self {
                data,
                _phantom: std::marker::PhantomData,
            }
        }
    }
}

impl<'a> From<&AnyView<'a>> for AnyView<'a> {
    #[inline(always)]
    fn from(value: &AnyView<'a>) -> Self {
        *value
    }
}

impl Default for AnyView<'_> {
    fn default() -> Self {
        Self::new()
    }
}

/// Holder for Any value
///
/// This is used to define try_from rule while conforming to orphan rule
/// Users should not use this directly
#[doc(hidden)]
pub struct TryFromTemp<T> {
    value: T,
}

impl<T> TryFromTemp<T> {
    /// Create a new holder for the value
    #[inline(always)]
    pub fn new(value: T) -> Self {
        Self { value }
    }

    /// Move the value out of the holder
    #[inline(always)]
    pub fn into_value(this: Self) -> T {
        this.value
    }
}

//---------------------
// Any
//---------------------
impl Any {
    /// Create an `Any` holding `None`, the empty FFI value.
    ///
    /// # Returns
    ///
    /// An `Any` holding `None`.
    #[inline(always)]
    pub fn new() -> Self {
        Self {
            data: TVMFFIAny::new(),
        }
    }

    /// Return the runtime type index of the stored value.
    ///
    /// Indices below `TypeIndex::kTVMFFIStaticObjectBegin` are the built-in
    /// non-object types; the others are object types.
    ///
    /// # Returns
    ///
    /// The type index.
    #[inline(always)]
    pub fn type_index(&self) -> i32 {
        self.data.type_index
    }

    /// The registered type key of the stored value, such as `"int"` or
    /// `"ffi.Array"`, as C++ `Any::GetTypeKey`.
    ///
    /// ```
    /// use tvm_ffi::{Any, Array};
    /// assert_eq!(Any::from(1_i64).type_key(), "int");
    /// assert_eq!(Any::from(Array::from_vec(vec![1_i64])).type_key(), "ffi.Array");
    /// ```
    #[inline]
    pub fn type_key(&self) -> &'static str {
        object::registered_type_key(self.data.type_index)
    }

    /// Whether the value is None.
    #[inline(always)]
    pub fn is_none(&self) -> bool {
        self.data.type_index == TypeIndex::kTVMFFINone as i32
    }

    /// Borrow the raw ABI representation, which this value owns.
    /// Copying it does not retain referenced data or extend its lifetime.
    #[inline(always)]
    pub fn as_raw_ffi_any(&self) -> &TVMFFIAny {
        &self.data
    }
    /// Borrow the stored value as `T` when it holds one, without copying it.
    ///
    /// `T` is an object type such as `ArrayObj`, a managed reference such as
    /// `Array<i64>`, or a value stored in place such as `String` or `i64`. The
    /// check is strict and never converts, unlike `TryFrom`.
    /// This neither consumes `self` nor changes any reference count, and the
    /// returned reference cannot outlive `self`.
    ///
    /// # Returns
    ///
    /// The borrowed object, or `None`.
    ///
    /// ```
    /// use tvm_ffi::{collections::array::ArrayObj, Any, Array};
    /// let value = Any::from(Array::from_vec(vec![1_i64, 2]));
    /// assert_eq!(value.downcast_ref::<ArrayObj>().unwrap().size, 2);
    /// assert_eq!(value.downcast_ref::<Array<i64>>().unwrap()[1], 2);
    /// assert!(value.downcast_ref::<i64>().is_none());
    /// ```
    ///
    /// See also [`match_any!`](crate::match_any).
    #[inline]
    pub fn downcast_ref<T: crate::type_traits::DowncastRefFromAny>(&self) -> Option<&T> {
        // SAFETY: the owned cell stays valid for as long as `self` is borrowed.
        unsafe { T::downcast_ref_from_any(&self.data) }
    }

    /// Whether the stored value is a `T` as is, by the strict check of
    /// [`downcast_ref`](Self::downcast_ref), without conversion, as std
    /// `Any::is`. It covers every [`AnyCompatible`] type, such as `bool`.
    ///
    /// ```
    /// use tvm_ffi::{Any, Array};
    /// let value = Any::from(Array::from_vec(vec![1_i64, 2]));
    /// assert!(value.is::<Array<i64>>());
    /// assert!(!value.is::<Array<f64>>());
    /// assert!(Any::from(true).is::<bool>());
    /// assert!(!Any::from(1_i64).is::<f64>());
    /// ```
    #[inline(always)]
    pub fn is<T: AnyCompatible>(&self) -> bool {
        // SAFETY: the owned cell is valid.
        unsafe { T::check_any_strict(&self.data) }
    }

    /// Return a pointer to the raw ABI representation, for a runtime call that
    /// writes an owned result into it as an out-parameter.
    ///
    /// Writing through the pointer is unsafe: the cell must afterwards hold a
    /// value this `Any` may own and release, and the previous value must have
    /// been released or moved out.
    #[inline(always)]
    pub fn as_raw_ffi_any_mut(&mut self) -> *mut TVMFFIAny {
        &mut self.data
    }

    /// Give up ownership of the value as its raw ABI representation, as
    /// `Box::into_raw` does. Rebuild it with [`from_raw_ffi_any`](Self::from_raw_ffi_any)
    /// to release it.
    #[inline(always)]
    pub fn into_raw_ffi_any(self) -> TVMFFIAny {
        let this = std::mem::ManuallyDrop::new(self);
        this.data
    }

    /// Take ownership of a raw ABI representation.
    ///
    /// # Safety
    ///
    /// `data.type_index` must describe the payload, and the caller must hand
    /// over the ownership `data` carries, such as one strong reference for an
    /// object, so it is released exactly once, by this `Any`.
    #[inline(always)]
    pub unsafe fn from_raw_ffi_any(data: TVMFFIAny) -> Self {
        Self { data }
    }

    /// The strong reference count of the held object, for tests and debugging;
    /// `None` if the value is not an object.
    #[doc(hidden)]
    pub fn debug_strong_count(&self) -> Option<usize> {
        unsafe {
            if self.data.type_index >= TypeIndex::kTVMFFIStaticObjectBegin as i32 {
                Some(object::unsafe_::strong_count(self.data.data_union.v_obj))
            } else {
                None
            }
        }
    }
}

impl Default for Any {
    #[inline(always)]
    fn default() -> Self {
        Self::new()
    }
}

impl Clone for Any {
    #[inline]
    fn clone(&self) -> Self {
        if self.data.type_index >= TypeIndex::kTVMFFIStaticObjectBegin as i32 {
            unsafe { object::unsafe_::inc_ref(self.data.data_union.v_obj) }
        }
        Self { data: self.data }
    }
}

impl Drop for Any {
    #[inline(always)]
    fn drop(&mut self) {
        if self.data.type_index >= TypeIndex::kTVMFFIStaticObjectBegin as i32 {
            unsafe { object::unsafe_::dec_ref(self.data.data_union.v_obj) }
        }
    }
}

// convert Any ref to AnyView
impl<'a> From<&'a Any> for AnyView<'a> {
    #[inline(always)]
    fn from(value: &'a Any) -> Self {
        Self {
            data: value.data,
            _phantom: std::marker::PhantomData,
        }
    }
}

/// Formats the value as its `repr`, the text the runtime printer gives in
/// every language, such as `(1, 2)` for an array or `"hi"` for a string.
/// Object references format the same way.
///
/// # Examples
///
/// ```
/// use tvm_ffi::{AnyView, Array};
///
/// let xs = Array::from_vec(vec![1_i64, 2]);
/// assert_eq!(AnyView::from(&xs).to_string(), "(1, 2)");
/// assert_eq!(xs.to_string(), "(1, 2)");
/// ```
impl std::fmt::Display for AnyView<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let repr = crate::cached_global_func!("ffi.ReprPrint")
            .call_packed(&[*self])
            .and_then(crate::String::try_from);
        match repr {
            Ok(text) => f.pad(text.as_str()),
            // Formatting cannot fail, so a printer error falls back to the type key.
            Err(_) => f.pad(&object::type_key_of_index(self.type_index())),
        }
    }
}

/// Formats the value as its `repr`; see [`AnyView`].
impl std::fmt::Display for Any {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Display::fmt(&AnyView::from(self), f)
    }
}

/// Formats the type key and the `repr`, such as `AnyView(int: 1)`.
impl std::fmt::Debug for AnyView<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let key = object::type_key_of_index(self.type_index());
        write!(f, "AnyView({key}: {self})")
    }
}

/// Formats the type key and the `repr`, such as `Any(int: 1)`.
impl std::fmt::Debug for Any {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let view = AnyView::from(self);
        let key = object::type_key_of_index(view.type_index());
        write!(f, "Any({key}: {view})")
    }
}

/// Whether a `TVMFFIAny` cell owns everything it holds, so its owning form is a
/// bitwise copy.
#[inline]
pub(crate) fn is_plain_inline(type_index: i32) -> bool {
    type_index < TypeIndex::kTVMFFIRawStr as i32
        || type_index == TypeIndex::kTVMFFISmallStr as i32
        || type_index == TypeIndex::kTVMFFISmallBytes as i32
        || type_index == TypeIndex::kTVMFFIUnchanged as i32
}

/// Takes ownership of the viewed value: objects gain a reference, borrowed
/// strings and bytes are copied, and an [`RValueRef`](crate::rvalue_ref::RValueRef)
/// gives up its object.
///
/// A `DLTensor*` has no reference count to take, so it is kept as is, as in
/// C++: the `Any` borrows the tensor and must not outlive it. Convert it to a
/// [`Tensor`](crate::Tensor) to own the tensor.
///
/// # Panics
///
/// Panics if the view holds an `RValueRef` whose object an earlier conversion
/// already took.
impl From<AnyView<'_>> for Any {
    #[inline]
    fn from(value: AnyView<'_>) -> Self {
        let data = value.data;
        // Owning a borrowed object is the same incref `Any::clone` does below.
        if data.type_index >= TypeIndex::kTVMFFIStaticObjectBegin as i32 {
            unsafe { object::unsafe_::inc_ref(data.data_union.v_obj) };
            return Self { data };
        }
        if is_plain_inline(data.type_index) {
            return Self { data };
        }
        // What is left borrows foreign storage and needs the runtime.
        any_view_to_owned_via_runtime(data)
    }
}

/// Out of line so the inlined conversions above stay leaf code, without the
/// stack frame and unwind path this call needs.
#[cold]
#[inline(never)]
fn any_view_to_owned_via_runtime(view: TVMFFIAny) -> Any {
    unsafe {
        let mut data = TVMFFIAny::new();
        crate::check_safe_call!(TVMFFIAnyViewToOwnedAny(&view, &mut data)).unwrap();
        Any { data }
    }
}

impl<T: AnyCompatible> From<T> for Any {
    #[inline(always)]
    fn from(value: T) -> Self {
        unsafe {
            let mut data = TVMFFIAny::new();
            T::move_to_any(value, &mut data);
            Self { data }
        }
    }
}

impl<'a, T: AnyCompatible> TryFrom<AnyView<'a>> for TryFromTemp<T> {
    type Error = crate::error::Error;
    #[inline]
    fn try_from(value: AnyView<'a>) -> Result<Self, Self::Error> {
        unsafe {
            if T::check_any_strict(&value.data) {
                Ok(TryFromTemp::new(T::copy_from_any_view_after_check(
                    &value.data,
                )))
            } else {
                T::try_cast_from_any_view(&value.data)
                    .map_err(|_| {
                        let msg = format!(
                            "Cannot convert from type `{}` to `{}`",
                            T::get_mismatch_type_info(&value.data),
                            T::type_str()
                        );
                        crate::error::Error::new(crate::error::TYPE_ERROR, &msg)
                    })
                    .map(TryFromTemp::new)
            }
        }
    }
}

impl<'a, T: AnyCompatible> TryFrom<&'a Any> for TryFromTemp<T> {
    type Error = crate::error::Error;
    #[inline]
    fn try_from(value: &'a Any) -> Result<Self, Self::Error> {
        TryFromTemp::try_from(AnyView::from(value))
    }
}

impl<T: AnyCompatible> TryFrom<Any> for TryFromTemp<T> {
    type Error = crate::error::Error;
    #[inline]
    fn try_from(value: Any) -> Result<Self, Self::Error> {
        unsafe {
            if T::check_any_strict(&value.data) {
                let mut value = std::mem::ManuallyDrop::new(value);
                Ok(TryFromTemp::new(T::move_from_any_after_check(
                    &mut value.data,
                )))
            } else {
                T::try_cast_from_any_view(&value.data)
                    .map_err(|_| {
                        let msg = format!(
                            "Cannot convert from type `{}` to `{}`",
                            T::get_mismatch_type_info(&value.data),
                            T::type_str()
                        );
                        crate::error::Error::new(crate::error::TYPE_ERROR, &msg)
                    })
                    .map(TryFromTemp::new)
            }
        }
    }
}

crate::impl_any_compatible_conversions!(
    (),
    bool,
    i8,
    i16,
    i32,
    i64,
    isize,
    u8,
    u16,
    u32,
    u64,
    usize,
    f32,
    f64,
    crate::string::String,
    crate::string::Bytes,
    crate::big_int::BigInt,
    tvm_ffi_sys::dlpack::DLDataType,
    tvm_ffi_sys::dlpack::DLDevice,
);
// Only out of `Any`: a raw pointer and the root reference are not call arguments.
crate::impl_try_from_any!(*mut core::ffi::c_void, crate::object::ObjectRef);
crate::impl_into_arg!(Any, *mut core::ffi::c_void, crate::object::ObjectRef);

crate::impl_try_from_any_for_parametric!(Option<T>);
crate::impl_into_arg!(impl[T: AnyCompatible + Clone] Option<T>);

/// The conversion of a typed function argument, whose error names the
/// argument's index.
pub(crate) trait ArgTryFromAnyView: Sized + crate::type_traits::TypeSchema {
    fn try_from_any_view(value: &AnyView, arg_index: usize) -> Result<Self, Error>;
}

impl<T: AnyCompatible> ArgTryFromAnyView for T {
    fn try_from_any_view(value: &AnyView, arg_index: usize) -> Result<Self, Error> {
        unsafe {
            if T::check_any_strict(&value.data) {
                Ok(T::copy_from_any_view_after_check(&value.data))
            } else {
                T::try_cast_from_any_view(&value.data).map_err(|_| {
                    let msg = format!(
                        "Argument #{}: Cannot convert from type `{}` to `{}`",
                        arg_index,
                        T::get_mismatch_type_info(&value.data),
                        T::type_str()
                    );
                    crate::error::Error::new(crate::error::TYPE_ERROR, &msg)
                })
            }
        }
    }
}