use crate::any::{Any, AnyView};
use std::borrow::Borrow;
use tvm_ffi_sys::TVMFFITypeIndex as TypeIndex;
use tvm_ffi_sys::{TVMFFIAny, TVMFFIGetTypeInfo, TVMFFIObject};
pub unsafe trait AnyCompatible: Sized {
#[doc(hidden)]
const MATCH_ANY_EXACT: bool = false;
#[doc(hidden)]
fn match_any_exact_type_index() -> i32 {
unreachable!("MATCH_ANY_EXACT is false")
}
#[inline]
unsafe fn is_undefined(field: *const Self) -> bool {
let _ = field;
false
}
#[doc(hidden)]
fn has_field_layout() -> bool {
true
}
unsafe fn copy_to_any_view(src: &Self, data: &mut TVMFFIAny);
unsafe fn move_to_any(src: Self, data: &mut TVMFFIAny);
unsafe fn check_any_strict(data: &TVMFFIAny) -> bool;
#[doc(hidden)]
unsafe fn match_constraints(_data: &TVMFFIAny) -> bool {
true
}
unsafe fn copy_from_any_view_after_check(data: &TVMFFIAny) -> Self;
unsafe fn move_from_any_after_check(data: &mut TVMFFIAny) -> Self;
#[allow(clippy::result_unit_err)]
unsafe fn try_cast_from_any_view(data: &TVMFFIAny) -> Result<Self, ()>;
fn get_mismatch_type_info(data: &TVMFFIAny) -> String {
runtime_type_key(data.type_index)
}
fn type_str() -> String;
fn type_schema() -> String {
type_schema(&Self::type_str(), &[])
}
#[inline]
fn to_any(&self) -> Any {
AnyView::from(self).into()
}
}
pub trait ContainerElement: container_element_ops::Ops {}
impl<T: AnyCompatible> ContainerElement for T {}
impl ContainerElement for Any {}
pub unsafe trait TypeSubsumes<Source: ?Sized> {}
unsafe impl<T: ?Sized> TypeSubsumes<T> for T {}
pub trait IntoArg<T> {
type Output: Borrow<T>;
fn into_arg(self) -> Self::Output;
fn into_owned(self) -> T;
}
impl IntoArg<crate::String> for &str {
type Output = crate::String;
#[inline]
fn into_arg(self) -> crate::String {
crate::String::from(self)
}
#[inline]
fn into_owned(self) -> crate::String {
crate::String::from(self)
}
}
impl IntoArg<crate::string::Bytes> for &[u8] {
type Output = crate::string::Bytes;
#[inline]
fn into_arg(self) -> crate::string::Bytes {
crate::string::Bytes::from(self)
}
#[inline]
fn into_owned(self) -> crate::string::Bytes {
crate::string::Bytes::from(self)
}
}
#[doc(hidden)]
pub unsafe trait RefFromAnyUnchecked: ContainerElement {
unsafe fn ref_from_any_unchecked(cell: &TVMFFIAny) -> &Self;
}
unsafe impl RefFromAnyUnchecked for Any {
#[inline(always)]
unsafe fn ref_from_any_unchecked(cell: &TVMFFIAny) -> &Self {
unsafe { &*(cell as *const TVMFFIAny as *const Any) }
}
}
unsafe impl RefFromAnyUnchecked for crate::big_int::BigInt {
#[inline(always)]
unsafe fn ref_from_any_unchecked(cell: &TVMFFIAny) -> &Self {
const {
assert!(
std::mem::size_of::<crate::big_int::BigInt>() == std::mem::size_of::<TVMFFIAny>()
);
assert!(
std::mem::align_of::<crate::big_int::BigInt>() == std::mem::align_of::<TVMFFIAny>()
);
};
unsafe { &*(cell as *const TVMFFIAny as *const crate::big_int::BigInt) }
}
}
unsafe impl RefFromAnyUnchecked for crate::String {
#[inline(always)]
unsafe fn ref_from_any_unchecked(cell: &TVMFFIAny) -> &Self {
unsafe { &*(cell as *const TVMFFIAny as *const crate::String) }
}
}
unsafe impl RefFromAnyUnchecked for crate::string::Bytes {
#[inline(always)]
unsafe fn ref_from_any_unchecked(cell: &TVMFFIAny) -> &Self {
unsafe { &*(cell as *const TVMFFIAny as *const crate::string::Bytes) }
}
}
unsafe impl RefFromAnyUnchecked for i64 {
#[inline(always)]
unsafe fn ref_from_any_unchecked(cell: &TVMFFIAny) -> &Self {
unsafe { &cell.data_union.v_int64 }
}
}
unsafe impl RefFromAnyUnchecked for f64 {
#[inline(always)]
unsafe fn ref_from_any_unchecked(cell: &TVMFFIAny) -> &Self {
unsafe { &cell.data_union.v_float64 }
}
}
unsafe impl RefFromAnyUnchecked for tvm_ffi_sys::dlpack::DLDataType {
#[inline(always)]
unsafe fn ref_from_any_unchecked(cell: &TVMFFIAny) -> &Self {
unsafe { &cell.data_union.v_dtype }
}
}
unsafe impl RefFromAnyUnchecked for tvm_ffi_sys::dlpack::DLDevice {
#[inline(always)]
unsafe fn ref_from_any_unchecked(cell: &TVMFFIAny) -> &Self {
unsafe { &cell.data_union.v_device }
}
}
unsafe impl<T: crate::object::ObjectRefCore> RefFromAnyUnchecked for Option<T> {
#[inline(always)]
unsafe fn ref_from_any_unchecked(cell: &TVMFFIAny) -> &Self {
const {
assert!(std::mem::size_of::<Option<T>>() == std::mem::size_of::<*mut TVMFFIObject>());
assert!(std::mem::align_of::<Option<T>>() == std::mem::align_of::<*mut TVMFFIObject>());
};
if cell.type_index == TypeIndex::kTVMFFINone as i32 {
return &None;
}
unsafe { &*(&cell.data_union.v_obj as *const *mut TVMFFIObject as *const Option<T>) }
}
}
pub unsafe trait DowncastRefFromAny {
unsafe fn downcast_ref_from_any(cell: &TVMFFIAny) -> Option<&Self>;
}
unsafe impl<T: crate::object::ObjectCore> DowncastRefFromAny for T {
#[inline(always)]
unsafe fn downcast_ref_from_any(cell: &TVMFFIAny) -> Option<&Self> {
if cell.type_index < TypeIndex::kTVMFFIStaticObjectBegin as i32
|| !crate::object::is_instance_of::<T>(cell.type_index)
{
return None;
}
Some(unsafe { &*cell.data_union.v_obj.cast::<T>() })
}
}
#[doc(hidden)]
#[inline(always)]
pub unsafe fn downcast_ref_from_any_in_place<T: RefFromAnyUnchecked>(
cell: &TVMFFIAny,
) -> Option<&T> {
if unsafe { T::container_check_any_strict(cell) } {
Some(unsafe { T::ref_from_any_unchecked(cell) })
} else {
None
}
}
unsafe impl<T: crate::object::ObjectRefCore> DowncastRefFromAny for Option<T> {
#[inline(always)]
unsafe fn downcast_ref_from_any(cell: &TVMFFIAny) -> Option<&Self> {
unsafe { downcast_ref_from_any_in_place::<Self>(cell) }
}
}
macro_rules! impl_downcast_ref_from_any {
($($t:ty),* $(,)?) => {
$(unsafe impl DowncastRefFromAny for $t {
#[inline(always)]
unsafe fn downcast_ref_from_any(cell: &TVMFFIAny) -> Option<&Self> {
unsafe { downcast_ref_from_any_in_place::<Self>(cell) }
}
})*
};
}
unsafe impl DowncastRefFromAny for Any {
#[inline(always)]
unsafe fn downcast_ref_from_any(cell: &TVMFFIAny) -> Option<&Self> {
let index = cell.type_index;
let borrowed = index == TypeIndex::kTVMFFIRawStr as i32
|| index == TypeIndex::kTVMFFIByteArrayPtr as i32
|| index == TypeIndex::kTVMFFIDLTensorPtr as i32
|| index == TypeIndex::kTVMFFIObjectRValueRef as i32;
if borrowed {
None
} else {
Some(unsafe { Any::ref_from_any_unchecked(cell) })
}
}
}
impl_downcast_ref_from_any!(
crate::String,
crate::string::Bytes,
i64,
f64,
crate::big_int::BigInt,
tvm_ffi_sys::dlpack::DLDataType,
tvm_ffi_sys::dlpack::DLDevice,
);
#[doc(hidden)]
pub unsafe trait DowncastRefFromObject {
unsafe fn downcast_ref_from_object(place: &*mut TVMFFIObject) -> Option<&Self>;
#[inline(always)]
fn as_object(&self) -> &crate::object::Object {
unsafe { &**std::ptr::from_ref(self).cast::<*const crate::object::Object>() }
}
}
unsafe impl<T: crate::object::ObjectCore> DowncastRefFromObject for T {
#[inline(always)]
fn as_object(&self) -> &crate::object::Object {
unsafe { &*std::ptr::from_ref(self).cast::<crate::object::Object>() }
}
#[inline(always)]
unsafe fn downcast_ref_from_object(place: &*mut TVMFFIObject) -> Option<&Self> {
let object = *place;
if unsafe { !crate::object::is_instance_of::<T>((*object).type_index) } {
return None;
}
Some(unsafe { &*object.cast::<T>() })
}
}
#[doc(hidden)]
#[inline(always)]
pub unsafe fn downcast_ref_from_object_in_place<T: RefFromAnyUnchecked>(
place: &*mut TVMFFIObject,
) -> Option<&T> {
const {
assert!(
std::mem::size_of::<T>() == std::mem::size_of::<*mut TVMFFIObject>(),
"a reference downcast target must be exactly one object pointer"
)
};
let object = *place;
unsafe {
core::hint::assert_unchecked(
(*object).type_index >= TypeIndex::kTVMFFIStaticObjectBegin as i32,
)
};
let mut cell = TVMFFIAny::new();
cell.type_index = unsafe { (*object).type_index };
cell.data_union.v_obj = object;
if unsafe { T::container_check_any_strict(&cell) } {
Some(unsafe { &*(place as *const *mut TVMFFIObject as *const T) })
} else {
None
}
}
unsafe impl AnyCompatible for bool {
unsafe fn copy_to_any_view(src: &Self, data: &mut TVMFFIAny) {
data.type_index = TypeIndex::kTVMFFIBool as i32;
data.small_str_len = 0;
data.data_union.v_int64 = *src as i64;
}
unsafe fn check_any_strict(data: &TVMFFIAny) -> bool {
data.type_index == TypeIndex::kTVMFFIBool as i32
}
unsafe fn copy_from_any_view_after_check(data: &TVMFFIAny) -> Self {
unsafe { data.data_union.v_int64 != 0 }
}
unsafe fn move_to_any(src: Self, data: &mut TVMFFIAny) {
data.type_index = TypeIndex::kTVMFFIBool as i32;
data.small_str_len = 0;
data.data_union.v_int64 = src as i64;
}
unsafe fn move_from_any_after_check(data: &mut TVMFFIAny) -> Self {
unsafe { data.data_union.v_int64 != 0 }
}
unsafe fn try_cast_from_any_view(data: &TVMFFIAny) -> Result<Self, ()> {
if data.type_index == TypeIndex::kTVMFFIBool as i32
|| data.type_index == TypeIndex::kTVMFFIInt as i32
{
unsafe { Ok(data.data_union.v_int64 != 0) }
} else {
Err(())
}
}
fn type_str() -> String {
"bool".to_string()
}
}
macro_rules! impl_any_compatible_for_int {
($($int_type:ty),* $(,)?) => {
$(
unsafe impl AnyCompatible for $int_type {
fn type_str() -> String {
"int".to_string()
}
unsafe fn copy_to_any_view(src: &Self, data: &mut TVMFFIAny) {
data.type_index = TypeIndex::kTVMFFIInt as i32;
data.small_str_len = 0;
data.data_union.v_int64 = *src as i64;
}
unsafe fn check_any_strict(data: &TVMFFIAny) -> bool {
data.type_index == TypeIndex::kTVMFFIInt as i32
}
unsafe fn match_constraints(data: &TVMFFIAny) -> bool {
<$int_type>::try_from(unsafe { data.data_union.v_int64 }).is_ok()
}
unsafe fn copy_from_any_view_after_check(data: &TVMFFIAny) -> Self {
unsafe { data.data_union.v_int64 as $int_type }
}
unsafe fn move_to_any(src: Self, data: &mut TVMFFIAny) {
data.type_index = TypeIndex::kTVMFFIInt as i32;
data.small_str_len = 0;
data.data_union.v_int64 = src as i64;
}
unsafe fn move_from_any_after_check(data: &mut TVMFFIAny) -> Self {
unsafe { data.data_union.v_int64 as $int_type }
}
unsafe fn try_cast_from_any_view(data: &TVMFFIAny) -> Result<Self, ()> {
if data.type_index == TypeIndex::kTVMFFIInt as i32 ||
data.type_index == TypeIndex::kTVMFFIBool as i32 {
Ok(unsafe { data.data_union.v_int64 } as $int_type)
} else {
Err(())
}
}
}
)*
};
}
impl_any_compatible_for_int!(i8, i16, i32, i64, isize, u8, u16, u32, u64, usize);
macro_rules! impl_any_compatible_for_float {
($($float_type:ty),* $(,)?) => {
$(
unsafe impl AnyCompatible for $float_type {
fn type_str() -> String {
"float".to_string()
}
unsafe fn copy_to_any_view(src: &Self, data: &mut TVMFFIAny) {
data.type_index = TypeIndex::kTVMFFIFloat as i32;
data.small_str_len = 0;
data.data_union.v_float64 = *src as f64;
}
unsafe fn check_any_strict(data: &TVMFFIAny) -> bool {
data.type_index == TypeIndex::kTVMFFIFloat as i32
}
unsafe fn match_constraints(data: &TVMFFIAny) -> bool {
let value = unsafe { data.data_union.v_float64 };
((value as $float_type) as f64).to_bits() == value.to_bits()
}
unsafe fn copy_from_any_view_after_check(data: &TVMFFIAny) -> Self {
unsafe { data.data_union.v_float64 as $float_type }
}
unsafe fn move_to_any(src: Self, data: &mut TVMFFIAny) {
data.type_index = TypeIndex::kTVMFFIFloat as i32;
data.small_str_len = 0;
data.data_union.v_float64 = src as f64;
}
unsafe fn move_from_any_after_check(data: &mut TVMFFIAny) -> Self {
unsafe { data.data_union.v_float64 as $float_type }
}
unsafe fn try_cast_from_any_view(data: &TVMFFIAny) -> Result<Self, ()> {
if data.type_index == TypeIndex::kTVMFFIFloat as i32 {
Ok(unsafe { data.data_union.v_float64 } as $float_type)
} else if data.type_index == TypeIndex::kTVMFFIInt as i32 ||
data.type_index == TypeIndex::kTVMFFIBool as i32 {
Ok(unsafe { data.data_union.v_int64 } as $float_type)
} else {
Err(())
}
}
}
)*
};
}
impl_any_compatible_for_float!(f32, f64);
unsafe impl<T: AnyCompatible> AnyCompatible for Option<T> {
fn type_str() -> String {
"Optional<".to_string() + T::type_str().as_str() + ">"
}
fn type_schema() -> String {
type_schema("Optional", &[<T as AnyCompatible>::type_schema()])
}
fn has_field_layout() -> bool {
crate::reflection::field_static_type_index(&T::type_str())
>= TypeIndex::kTVMFFIStaticObjectBegin as i32
}
unsafe fn is_undefined(field: *const Self) -> bool {
if std::mem::size_of::<Self>() != std::mem::size_of::<T>() {
return false;
}
let none = std::mem::MaybeUninit::new(None::<T>);
unsafe { T::is_undefined(field.cast::<T>()) && !T::is_undefined(none.as_ptr().cast::<T>()) }
}
unsafe fn copy_to_any_view(src: &Self, data: &mut TVMFFIAny) {
if let Some(value) = src {
unsafe { T::copy_to_any_view(value, data) };
} else {
data.type_index = TypeIndex::kTVMFFINone as i32;
data.small_str_len = 0;
data.data_union.v_int64 = 0;
}
}
unsafe fn move_to_any(src: Self, data: &mut TVMFFIAny) {
if let Some(value) = src {
unsafe { T::move_to_any(value, data) };
} else {
data.type_index = TypeIndex::kTVMFFINone as i32;
data.small_str_len = 0;
data.data_union.v_int64 = 0;
}
}
unsafe fn check_any_strict(data: &TVMFFIAny) -> bool {
unsafe { T::check_any_strict(data) || data.type_index == TypeIndex::kTVMFFINone as i32 }
}
unsafe fn match_constraints(data: &TVMFFIAny) -> bool {
unsafe { data.type_index == TypeIndex::kTVMFFINone as i32 || T::match_constraints(data) }
}
unsafe fn copy_from_any_view_after_check(data: &TVMFFIAny) -> Self {
if data.type_index == TypeIndex::kTVMFFINone as i32 {
None
} else {
Some(unsafe { T::copy_from_any_view_after_check(data) })
}
}
unsafe fn move_from_any_after_check(data: &mut TVMFFIAny) -> Self {
if data.type_index == TypeIndex::kTVMFFINone as i32 {
None
} else {
Some(unsafe { T::move_from_any_after_check(data) })
}
}
unsafe fn try_cast_from_any_view(data: &TVMFFIAny) -> Result<Self, ()> {
if data.type_index == TypeIndex::kTVMFFINone as i32 {
Ok(None)
} else {
unsafe { T::try_cast_from_any_view(data).map(Some) }
}
}
}
unsafe impl AnyCompatible for () {
fn type_str() -> String {
"None".to_string()
}
unsafe fn copy_to_any_view(_src: &Self, data: &mut TVMFFIAny) {
data.type_index = TypeIndex::kTVMFFINone as i32;
data.small_str_len = 0;
data.data_union.v_int64 = 0;
}
unsafe fn move_to_any(_src: Self, data: &mut TVMFFIAny) {
data.type_index = TypeIndex::kTVMFFINone as i32;
data.small_str_len = 0;
data.data_union.v_int64 = 0;
}
unsafe fn check_any_strict(data: &TVMFFIAny) -> bool {
data.type_index == TypeIndex::kTVMFFINone as i32
}
unsafe fn copy_from_any_view_after_check(_data: &TVMFFIAny) -> Self {}
unsafe fn move_from_any_after_check(_data: &mut TVMFFIAny) -> Self {}
unsafe fn try_cast_from_any_view(data: &TVMFFIAny) -> Result<Self, ()> {
if data.type_index == TypeIndex::kTVMFFINone as i32 {
Ok(())
} else {
Err(())
}
}
}
unsafe impl AnyCompatible for *mut core::ffi::c_void {
unsafe fn copy_to_any_view(src: &Self, data: &mut TVMFFIAny) {
data.type_index = TypeIndex::kTVMFFIOpaquePtr as i32;
data.small_str_len = 0;
data.data_union.v_ptr = *src;
}
unsafe fn check_any_strict(data: &TVMFFIAny) -> bool {
data.type_index == TypeIndex::kTVMFFIOpaquePtr as i32
}
unsafe fn copy_from_any_view_after_check(data: &TVMFFIAny) -> Self {
unsafe { data.data_union.v_ptr }
}
unsafe fn move_to_any(src: Self, data: &mut TVMFFIAny) {
data.type_index = TypeIndex::kTVMFFIOpaquePtr as i32;
data.small_str_len = 0;
data.data_union.v_int64 = src as i64;
}
unsafe fn move_from_any_after_check(data: &mut TVMFFIAny) -> Self {
unsafe { data.data_union.v_ptr }
}
unsafe fn try_cast_from_any_view(data: &TVMFFIAny) -> Result<Self, ()> {
if data.type_index == TypeIndex::kTVMFFINone as i32 {
Ok(std::ptr::null_mut())
} else if data.type_index == TypeIndex::kTVMFFIOpaquePtr as i32 {
unsafe { Ok(data.data_union.v_ptr) }
} else {
Err(())
}
}
fn type_str() -> String {
"void*".to_string()
}
}
mod container_element_ops {
use super::{Any, AnyCompatible, AnyView, TVMFFIAny};
pub trait Ops: Sized {
const CONTAINER_IS_ANY: bool = false;
unsafe fn container_copy_to_any_view(src: &Self, data: &mut TVMFFIAny);
unsafe fn container_move_to_any(src: Self, data: &mut TVMFFIAny);
unsafe fn container_check_any_strict(data: &TVMFFIAny) -> bool;
unsafe fn container_match_constraints(data: &TVMFFIAny) -> bool;
unsafe fn container_copy_from_any_view_after_check(data: &TVMFFIAny) -> Self;
unsafe fn container_move_from_any_after_check(data: &mut TVMFFIAny) -> Self;
unsafe fn container_try_cast_from_any_view(data: &TVMFFIAny) -> Result<Self, ()>;
fn container_get_mismatch_type_info(data: &TVMFFIAny) -> String;
fn container_type_str() -> String;
fn container_type_schema() -> String;
}
impl<T: AnyCompatible> Ops for T {
#[inline(always)]
unsafe fn container_copy_to_any_view(src: &Self, data: &mut TVMFFIAny) {
unsafe { <T as AnyCompatible>::copy_to_any_view(src, data) }
}
#[inline(always)]
unsafe fn container_move_to_any(src: Self, data: &mut TVMFFIAny) {
unsafe { <T as AnyCompatible>::move_to_any(src, data) }
}
#[inline(always)]
unsafe fn container_check_any_strict(data: &TVMFFIAny) -> bool {
unsafe { <T as AnyCompatible>::check_any_strict(data) }
}
#[inline(always)]
unsafe fn container_match_constraints(data: &TVMFFIAny) -> bool {
unsafe { <T as AnyCompatible>::match_constraints(data) }
}
#[inline(always)]
unsafe fn container_copy_from_any_view_after_check(data: &TVMFFIAny) -> Self {
unsafe { <T as AnyCompatible>::copy_from_any_view_after_check(data) }
}
#[inline(always)]
unsafe fn container_move_from_any_after_check(data: &mut TVMFFIAny) -> Self {
unsafe { <T as AnyCompatible>::move_from_any_after_check(data) }
}
#[inline(always)]
unsafe fn container_try_cast_from_any_view(data: &TVMFFIAny) -> Result<Self, ()> {
unsafe { <T as AnyCompatible>::try_cast_from_any_view(data) }
}
#[inline(always)]
fn container_get_mismatch_type_info(data: &TVMFFIAny) -> String {
<T as AnyCompatible>::get_mismatch_type_info(data)
}
#[inline(always)]
fn container_type_str() -> String {
<T as AnyCompatible>::type_str()
}
#[inline(always)]
fn container_type_schema() -> String {
<T as AnyCompatible>::type_schema()
}
}
impl Ops for Any {
const CONTAINER_IS_ANY: bool = true;
#[inline(always)]
unsafe fn container_copy_to_any_view(src: &Self, data: &mut TVMFFIAny) {
*data = *src.as_raw_ffi_any();
}
#[inline(always)]
unsafe fn container_move_to_any(src: Self, data: &mut TVMFFIAny) {
*data = Any::into_raw_ffi_any(src);
}
#[inline(always)]
unsafe fn container_check_any_strict(_data: &TVMFFIAny) -> bool {
true
}
#[inline(always)]
unsafe fn container_match_constraints(_data: &TVMFFIAny) -> bool {
true
}
#[inline(always)]
unsafe fn container_copy_from_any_view_after_check(data: &TVMFFIAny) -> Self {
unsafe { Any::from(AnyView::from_raw_ffi_any(*data)) }
}
#[inline(always)]
unsafe fn container_move_from_any_after_check(data: &mut TVMFFIAny) -> Self {
unsafe { Any::from_raw_ffi_any(std::mem::replace(data, TVMFFIAny::new())) }
}
#[inline(always)]
unsafe fn container_try_cast_from_any_view(data: &TVMFFIAny) -> Result<Self, ()> {
Ok(unsafe { Any::from(AnyView::from_raw_ffi_any(*data)) })
}
#[inline(always)]
fn container_get_mismatch_type_info(_data: &TVMFFIAny) -> String {
"Any".to_string()
}
#[inline(always)]
fn container_type_str() -> String {
"Any".to_string()
}
#[inline(always)]
fn container_type_schema() -> String {
<Any as super::TypeSchema>::type_schema()
}
}
}
pub(crate) fn runtime_type_key(type_index: i32) -> String {
unsafe {
let info = TVMFFIGetTypeInfo(type_index);
assert!(!info.is_null(), "TVMFFIGetTypeInfo returned null");
(*info).type_key.as_str().to_string()
}
}
pub(crate) fn type_schema(origin: &str, args: &[String]) -> String {
if args.is_empty() {
format!(r#"{{"type":"{origin}"}}"#)
} else {
format!(r#"{{"type":"{origin}","args":[{}]}}"#, args.join(","))
}
}
pub(crate) fn type_schema_metadata(type_schema: &str) -> String {
format!(r#"{{"type_schema":{}}}"#, escape_string_json(type_schema))
}
fn escape_string_json(text: &str) -> String {
let mut out = String::with_capacity(text.len() + 2);
out.push('"');
for ch in text.chars() {
match ch {
'"' => out.push_str("\\\""),
'\\' => out.push_str("\\\\"),
'/' => out.push_str("\\/"),
'\u{8}' => out.push_str("\\b"),
'\u{c}' => out.push_str("\\f"),
'\n' => out.push_str("\\n"),
'\r' => out.push_str("\\r"),
'\t' => out.push_str("\\t"),
ch if (ch as u32) < 0x20 || ch == '\u{7f}' => {
out.push_str(&format!("\\u{:04x}", ch as u32))
}
ch => out.push(ch),
}
}
out.push('"');
out
}
pub(crate) trait TypeSchema {
fn type_schema() -> String;
}
impl<T: AnyCompatible> TypeSchema for T {
fn type_schema() -> String {
<T as AnyCompatible>::type_schema()
}
}
impl TypeSchema for Any {
fn type_schema() -> String {
type_schema("Any", &[])
}
}
impl TypeSchema for AnyView<'_> {
fn type_schema() -> String {
type_schema("Any", &[])
}
}