diff --git a/crates/rune-macros/src/any.rs b/crates/rune-macros/src/any.rs index c138fa022..5bd0580c9 100644 --- a/crates/rune-macros/src/any.rs +++ b/crates/rune-macros/src/any.rs @@ -128,7 +128,7 @@ impl Derive { ) -> Result, ()> { let mut installers = Vec::new(); - expand_install_with(cx, &self.input, &tokens, &attr, &mut installers)?; + expand_install_with(cx, &self.input, tokens, attr, &mut installers)?; if matches!(&self.input.data, syn::Data::Enum(..)) { if let Some(span) = attr.constructor { @@ -563,7 +563,7 @@ where let Tokens { alloc, any_t, - any_from_t, + any_marker_t, context_error, fmt, hash, @@ -576,7 +576,7 @@ where non_null, raw_value_guard, result, - static_type_info, + any_type_info, type_hash_t, type_of, unsafe_to_mut, @@ -680,7 +680,7 @@ where #(#attrs)* impl #impl_generics #type_of for #ident #type_generics #where_clause { const PARAMETERS: #hash = #type_parameters; - const STATIC_TYPE_INFO: #static_type_info = #static_type_info::any_type_info(::ANY_TYPE_INFO); + const STATIC_TYPE_INFO: #any_type_info = ::ANY_TYPE_INFO; } #[automatically_derived] @@ -751,7 +751,7 @@ where quote! { #[automatically_derived] #(#attrs)* - impl #impl_generics #any_from_t for #ident #type_generics #where_clause { + impl #impl_generics #any_marker_t for #ident #type_generics #where_clause { } } }); diff --git a/crates/rune-macros/src/context.rs b/crates/rune-macros/src/context.rs index a01062de7..cb9f2f6e5 100644 --- a/crates/rune-macros/src/context.rs +++ b/crates/rune-macros/src/context.rs @@ -720,8 +720,9 @@ impl Context { Tokens { alloc: path(m, ["alloc"]), + any_marker_t: path(m, ["__private", "AnyMarker"]), any_t: path(m, ["Any"]), - any_from_t: path(m, ["__private", "AnyFrom"]), + any_type_info: path(m, ["runtime", "AnyTypeInfo"]), arc: path(m, ["__private", "Arc"]), compile_error: path(m, ["compile", "Error"]), const_construct_t: path(m, ["runtime", "ConstConstruct"]), @@ -756,7 +757,6 @@ impl Context { runtime_error: path(m, ["runtime", "RuntimeError"]), span: path(m, ["ast", "Span"]), spanned: path(m, ["ast", "Spanned"]), - static_type_info: path(m, ["runtime", "StaticTypeInfo"]), string: path(m, ["alloc", "String"]), to_const_value_t: path(m, ["runtime", "ToConstValue"]), to_tokens: path(m, ["macros", "ToTokens"]), @@ -815,8 +815,9 @@ fn path(base: &syn::Path, path: [&'static str; N]) -> syn::Path pub(crate) struct Tokens { pub(crate) alloc: syn::Path, - pub(crate) any_from_t: syn::Path, + pub(crate) any_marker_t: syn::Path, pub(crate) any_t: syn::Path, + pub(crate) any_type_info: syn::Path, pub(crate) arc: syn::Path, pub(crate) compile_error: syn::Path, pub(crate) const_construct_t: syn::Path, @@ -851,7 +852,6 @@ pub(crate) struct Tokens { pub(crate) runtime_error: syn::Path, pub(crate) span: syn::Path, pub(crate) spanned: syn::Path, - pub(crate) static_type_info: syn::Path, pub(crate) string: syn::Path, pub(crate) to_const_value_t: syn::Path, pub(crate) to_tokens: syn::Path, diff --git a/crates/rune-macros/src/lib.rs b/crates/rune-macros/src/lib.rs index b2c5a34bd..b4180a532 100644 --- a/crates/rune-macros/src/lib.rs +++ b/crates/rune-macros/src/lib.rs @@ -42,6 +42,7 @@ mod macro_; mod module; mod opaque; mod parse; +mod path_in; mod quote; mod spanned; mod to_tokens; @@ -215,6 +216,20 @@ pub fn hash(input: proc_macro::TokenStream) -> proc_macro::TokenStream { stream.into() } +#[doc(hidden)] +#[proc_macro] +pub fn hash_in(input: proc_macro::TokenStream) -> proc_macro::TokenStream { + let path_in::PathIn { in_crate, item, .. } = + syn::parse_macro_input!(input as path_in::PathIn); + + let stream = Context::build(|cx| { + let value = item.build_type_hash(cx)?.into_inner(); + Ok(::quote::quote!(#in_crate::Hash(#value))) + }); + + stream.into() +} + /// Calculate an item reference at compile time. /// /// # Examples @@ -246,6 +261,21 @@ pub fn item(input: proc_macro::TokenStream) -> proc_macro::TokenStream { stream.into() } +#[proc_macro] +#[doc(hidden)] +pub fn item_in(input: proc_macro::TokenStream) -> proc_macro::TokenStream { + let path_in::PathIn { in_crate, item, .. } = syn::parse_macro_input!(input as path_in::PathIn); + + let stream = match self::item::build_item(&item) { + Ok(hash) => { + ::quote::quote!(unsafe { #in_crate::Item::from_bytes(&#hash) }) + } + Err(error) => to_compile_errors([error]), + }; + + stream.into() +} + /// Helper to generate bindings for an external type. /// /// This can *only* be used inside of the `rune` crate itself. diff --git a/crates/rune-macros/src/path_in.rs b/crates/rune-macros/src/path_in.rs new file mode 100644 index 000000000..dc3d8a8f4 --- /dev/null +++ b/crates/rune-macros/src/path_in.rs @@ -0,0 +1,23 @@ +use syn::parse::{Parse, ParseStream}; +use syn::Token; + +pub(super) struct PathIn { + pub(super) in_crate: syn::Path, + #[allow(unused)] + pub(super) comma_token: Token![,], + pub(super) item: T, +} + +impl Parse for PathIn +where + T: Parse, +{ + #[inline] + fn parse(input: ParseStream) -> syn::Result { + Ok(Self { + in_crate: input.parse()?, + comma_token: input.parse()?, + item: input.parse()?, + }) + } +} diff --git a/crates/rune/src/any.rs b/crates/rune/src/any.rs index 25125b0db..32d6cf52e 100644 --- a/crates/rune/src/any.rs +++ b/crates/rune/src/any.rs @@ -132,4 +132,4 @@ pub trait Any: TypeHash + Named + any::Any { /// [`Any`] derive instead. /// /// [`Any`]: derive@Any -pub trait AnyFrom: Any {} +pub trait AnyMarker: Any {} diff --git a/crates/rune/src/compile/context.rs b/crates/rune/src/compile/context.rs index 8ee65eafe..c3427606c 100644 --- a/crates/rune/src/compile/context.rs +++ b/crates/rune/src/compile/context.rs @@ -20,8 +20,8 @@ use crate::module::{ TypeSpecification, }; use crate::runtime::{ - ConstConstruct, ConstContext, ConstValue, FunctionHandler, InstAddress, Memory, Output, - Protocol, RuntimeContext, StaticTypeInfo, TypeCheck, TypeInfo, VariantRtti, VmResult, + AnyTypeInfo, ConstConstruct, ConstContext, ConstValue, FunctionHandler, InstAddress, Memory, + Output, Protocol, RuntimeContext, TypeCheck, TypeInfo, VariantRtti, VmResult, }; use crate::{Hash, Item, ItemBuf}; @@ -969,7 +969,7 @@ impl Context { fn install_construct( &mut self, hash: Hash, - type_info: &StaticTypeInfo, + type_info: &AnyTypeInfo, construct: &Arc, ) -> Result<(), ContextError> { let old = self.construct.try_insert(hash, construct.clone())?; diff --git a/crates/rune/src/function/mod.rs b/crates/rune/src/function/mod.rs index ce255871e..e1eaac123 100644 --- a/crates/rune/src/function/mod.rs +++ b/crates/rune/src/function/mod.rs @@ -8,7 +8,7 @@ use crate::alloc; use crate::compile::meta; use crate::hash::Hash; use crate::runtime::{ - self, FromValue, InstAddress, MaybeTypeOf, Memory, Output, StaticTypeInfo, ToReturn, TypeHash, + self, AnyTypeInfo, FromValue, InstAddress, MaybeTypeOf, Memory, Output, ToReturn, TypeHash, TypeOf, UnsafeToMut, UnsafeToRef, Value, VmErrorKind, VmResult, }; @@ -161,7 +161,7 @@ where T: ?Sized + TypeOf, { const PARAMETERS: Hash = T::PARAMETERS; - const STATIC_TYPE_INFO: StaticTypeInfo = T::STATIC_TYPE_INFO; + const STATIC_TYPE_INFO: AnyTypeInfo = T::STATIC_TYPE_INFO; } /// Zero-sized marker struct for mutable references. @@ -189,7 +189,7 @@ where T: ?Sized + TypeOf, { const PARAMETERS: Hash = T::PARAMETERS; - const STATIC_TYPE_INFO: StaticTypeInfo = T::STATIC_TYPE_INFO; + const STATIC_TYPE_INFO: AnyTypeInfo = T::STATIC_TYPE_INFO; } // Fake guard for owned values. diff --git a/crates/rune/src/internal_macros.rs b/crates/rune/src/internal_macros.rs index 83ea2eb4f..e6165c61f 100644 --- a/crates/rune/src/internal_macros.rs +++ b/crates/rune/src/internal_macros.rs @@ -8,27 +8,25 @@ macro_rules! resolve_context { }; } -macro_rules! impl_any_type { - (impl $(<$($p:ident),*>)? for $ty:ty, $path:path) => { +macro_rules! impl_one_builtin_type_of { + (impl $(<$($p:ident),*>)? $path:path, $ty:ty) => { impl $(<$($p,)*>)* $crate::TypeHash for $ty { - const HASH: $crate::Hash = ::rune_macros::hash!($path); + const HASH: $crate::Hash = ::rune_macros::hash_in!(crate, $path); } impl $(<$($p,)*>)* $crate::runtime::TypeOf for $ty where $($($p: $crate::runtime::MaybeTypeOf,)*)* { - const STATIC_TYPE_INFO: $crate::runtime::StaticTypeInfo = $crate::runtime::StaticTypeInfo::any_type_info( - $crate::runtime::AnyTypeInfo::new( - { - fn full_name(f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { - write!(f, "{}", ::rune_macros::item!($path)) - } + const STATIC_TYPE_INFO: $crate::runtime::AnyTypeInfo = $crate::runtime::AnyTypeInfo::new( + { + fn full_name(f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { + write!(f, "{}", ::rune_macros::item_in!(crate, $path)) + } - full_name - }, - ::HASH, - ) + full_name + }, + ::HASH, ); } @@ -44,39 +42,6 @@ macro_rules! impl_any_type { } } -/// Build an implementation of `TypeOf` basic of a static type. -macro_rules! impl_static_type { - (impl $(<$($p:ident),*>)? for $ty:ty, $name:ident, $hash:ident) => { - impl $(<$($p,)*>)* $crate::TypeHash for $ty { - const HASH: $crate::Hash = $crate::runtime::static_type::$hash; - } - - impl $(<$($p,)*>)* $crate::runtime::TypeOf for $ty - where - $( - $($p: $crate::runtime::MaybeTypeOf,)* - )* - { - const STATIC_TYPE_INFO: $crate::runtime::StaticTypeInfo = $crate::runtime::StaticTypeInfo::static_type($crate::runtime::static_type::$name); - } - - impl $(<$($p,)*>)* $crate::runtime::MaybeTypeOf for $ty - where - $( - $($p: $crate::runtime::MaybeTypeOf,)* - )* - { - #[inline] - fn maybe_type_of() -> $crate::alloc::Result<$crate::compile::meta::DocType> { - $crate::compile::meta::DocType::with_generics( - <$ty as $crate::TypeHash>::HASH, - [$($(<$p as $crate::runtime::MaybeTypeOf>::maybe_type_of()?),*)*] - ) - } - } - }; -} - /// Call the given macro with repeated type arguments and counts. macro_rules! repeat_macro { ($macro:ident) => { @@ -163,3 +128,17 @@ macro_rules! from_value_ref { } }; } + +macro_rules! impl_builtin_type_of { + ( + $( + $(#[$($impl_meta:meta)*])* + impl $(<$($p:ident),*>)? $path:path, $ty:ty; + )* + ) => { + $( + $(#[$($impl_meta)*])* + impl_one_builtin_type_of!(impl $(<$($p),*>)* $path, $ty); + )* + } +} diff --git a/crates/rune/src/lib.rs b/crates/rune/src/lib.rs index 57875d89f..cd2ae8f7b 100644 --- a/crates/rune/src/lib.rs +++ b/crates/rune/src/lib.rs @@ -661,7 +661,7 @@ pub(crate) mod doc; /// Privately exported details. #[doc(hidden)] pub mod __private { - pub use crate::any::AnyFrom; + pub use crate::any::AnyMarker; pub use crate::function_meta::{ FunctionMetaData, FunctionMetaKind, FunctionMetaStatics, MacroMetaData, MacroMetaKind, }; @@ -705,5 +705,64 @@ rune_macros::binding! { impl ::std::string::Utf8Error for core::str::Utf8Error; } +impl_builtin_type_of! { + impl ::std::result::Result, core::result::Result; + impl ::std::option::Option, core::option::Option; + + impl ::std::bool, bool; + impl ::std::char, char; + + impl ::std::i64, i8; + impl ::std::i64, i16; + impl ::std::i64, i32; + impl ::std::i64, i64; + impl ::std::i64, i128; + impl ::std::i64, isize; + impl ::std::u64, u8; + impl ::std::u64, u16; + impl ::std::u64, u32; + impl ::std::u64, u64; + impl ::std::u64, u128; + impl ::std::u64, usize; + + impl ::std::f64, f32; + impl ::std::f64, f64; + + impl ::std::ops::ControlFlow, core::ops::ControlFlow; + + impl ::std::bytes::Bytes, [u8]; + + impl ::std::cmp::Ordering, core::cmp::Ordering; + + #[cfg(feature = "alloc")] + #[cfg_attr(rune_docsrs, doc(cfg(feature = "alloc")))] + impl ::std::string::String, ::rust_alloc::string::String; + impl ::std::string::String, crate::alloc::Box; + impl ::std::string::String, str; + + impl ::std::vec::Vec, [Value]; + #[cfg(feature = "alloc")] + #[cfg_attr(rune_docsrs, doc(cfg(feature = "alloc")))] + impl ::std::vec::Vec, ::rust_alloc::vec::Vec; + impl ::std::vec::Vec, crate::alloc::Vec; + impl ::std::vec::Vec, crate::runtime::VecTuple; + + impl ::std::tuple::Tuple, crate::runtime::Tuple; + + impl ::std::object::Object, crate::runtime::Struct; + impl ::std::object::Object, crate::alloc::HashMap<::rust_alloc::string::String, T>; + impl ::std::object::Object, crate::alloc::HashMap; + + #[cfg(feature = "std")] + #[cfg_attr(rune_docsrs, doc(cfg(feature = "std")))] + impl ::std::object::Object, std::collections::HashMap<::rust_alloc::string::String, T>; + + #[cfg(feature = "std")] + #[cfg_attr(rune_docsrs, doc(cfg(feature = "std")))] + impl ::std::object::Object, std::collections::HashMap; + + impl ::std::any::Type, crate::runtime::Type; +} + from_value_ref!(Result, into_result_ref, into_result_mut); from_value_ref!(Option, into_option_ref, into_option_mut); diff --git a/crates/rune/src/module/module.rs b/crates/rune/src/module/module.rs index 64c97ab1b..baaabb684 100644 --- a/crates/rune/src/module/module.rs +++ b/crates/rune/src/module/module.rs @@ -17,8 +17,8 @@ use crate::item::IntoComponent; use crate::macros::{MacroContext, TokenStream}; use crate::module::DocFunction; use crate::runtime::{ - ConstConstruct, InstAddress, MaybeTypeOf, Memory, Output, Protocol, StaticTypeInfo, - ToConstValue, TypeHash, TypeOf, VmResult, + AnyTypeInfo, ConstConstruct, InstAddress, MaybeTypeOf, Memory, Output, Protocol, ToConstValue, + TypeHash, TypeOf, VmResult, }; use crate::{Hash, Item, ItemBuf}; @@ -71,7 +71,7 @@ pub struct Module { /// A re-export in the current module. pub(crate) reexports: Vec, /// Constant constructors. - pub(crate) construct: Vec<(Hash, StaticTypeInfo, Arc)>, + pub(crate) construct: Vec<(Hash, AnyTypeInfo, Arc)>, /// Defines construct hashes. pub(crate) construct_hash: HashSet, /// Module level metadata. diff --git a/crates/rune/src/modules/option.rs b/crates/rune/src/modules/option.rs index 57fa791a5..59aab7fab 100644 --- a/crates/rune/src/modules/option.rs +++ b/crates/rune/src/modules/option.rs @@ -41,12 +41,10 @@ pub fn module() -> Result { m.index_function(Protocol::GET, 0, |this: &Option| match this { Option::Some(value) => VmResult::Ok(value.clone()), - _ => { - return VmResult::err(RuntimeError::__rune_macros__unsupported_tuple_index_get( - as Any>::ANY_TYPE_INFO, - 0, - )) - } + _ => VmResult::err(RuntimeError::__rune_macros__unsupported_tuple_index_get( + as Any>::ANY_TYPE_INFO, + 0, + )), })?; // Sorted for ease of finding diff --git a/crates/rune/src/runtime/from_value.rs b/crates/rune/src/runtime/from_value.rs index 01b66bc57..50be90f75 100644 --- a/crates/rune/src/runtime/from_value.rs +++ b/crates/rune/src/runtime/from_value.rs @@ -1,7 +1,7 @@ use core::cmp::Ordering; use crate::alloc::{self, String}; -use crate::any::AnyFrom; +use crate::any::AnyMarker; use super::{ AnyObj, ConstValue, FromConstValue, Mut, RawAnyGuard, Ref, RuntimeError, Value, VmResult, @@ -243,7 +243,7 @@ pub trait UnsafeFromValue: Sized { impl FromValue for T where - T: AnyFrom, + T: AnyMarker, { #[inline] fn from_value(value: Value) -> Result { @@ -253,7 +253,7 @@ where impl FromValue for Mut where - T: AnyFrom, + T: AnyMarker, { #[inline] fn from_value(value: Value) -> Result { @@ -263,7 +263,7 @@ where impl FromValue for Ref where - T: AnyFrom, + T: AnyMarker, { #[inline] fn from_value(value: Value) -> Result { diff --git a/crates/rune/src/runtime/mod.rs b/crates/rune/src/runtime/mod.rs index 72ef0884a..e0fef4304 100644 --- a/crates/rune/src/runtime/mod.rs +++ b/crates/rune/src/runtime/mod.rs @@ -141,9 +141,6 @@ pub use self::stack::{Memory, SliceError, Stack, StackError}; mod static_string; pub use self::static_string::StaticString; -pub(crate) mod static_type; -pub use self::static_type::StaticType; - mod stream; pub use self::stream::Stream; @@ -157,8 +154,7 @@ mod type_info; pub use self::type_info::{AnyTypeInfo, TypeInfo}; mod type_of; -use self::type_of::StaticTypeInfoKind; -pub use self::type_of::{MaybeTypeOf, StaticTypeInfo, TypeHash, TypeOf}; +pub use self::type_of::{MaybeTypeOf, TypeHash, TypeOf}; pub mod unit; pub(crate) use self::unit::UnitFn; diff --git a/crates/rune/src/runtime/static_type.rs b/crates/rune/src/runtime/static_type.rs deleted file mode 100644 index 4eb971497..000000000 --- a/crates/rune/src/runtime/static_type.rs +++ /dev/null @@ -1,195 +0,0 @@ -use crate as rune; - -use core::cmp::{Eq, Ordering, PartialEq}; -use core::hash; -use core::ops::ControlFlow; - -use crate::alloc::clone::TryClone; -use crate::alloc::{self, HashMap}; -use crate::runtime as rt; -use crate::{Hash, Item}; - -/// Static type information. -#[derive(TryClone, Debug, Clone, Copy)] -pub struct StaticType { - /// The name of the static type. - #[try_clone(copy)] - pub(crate) item: &'static Item, - /// The hash of the static type. - #[try_clone(copy)] - pub(crate) hash: Hash, -} - -impl PartialEq for StaticType { - fn eq(&self, other: &Self) -> bool { - self.hash == other.hash - } -} - -impl Eq for StaticType {} - -impl PartialEq for StaticType { - fn eq(&self, other: &Hash) -> bool { - self.hash == *other - } -} - -impl hash::Hash for StaticType { - fn hash(&self, state: &mut H) { - self.hash.hash(state) - } -} - -macro_rules! any_type { - ( - $( - $(#[$($meta:meta)*])* - $path:path { - $( - $(#[$($impl_meta:meta)*])* - impl $(<$($p:ident),*>)? for $ty:ty; - )* - } - )* - ) => { - $( - $( - $(#[$($impl_meta)*])* - impl_any_type!(impl $(<$($p),*>)* for $ty, $path); - )* - )* - } -} - -macro_rules! static_type { - ( - $( - $(#[$($meta:meta)*])* - $vis:vis static [$name:ident, $hash:ident] = $path:path { - $( - $(#[$($impl_meta:meta)*])* - impl $(<$($p:ident),*>)? for $ty:ty; - )* - } - )* - ) => { - $( - $vis const $hash: Hash = ::rune_macros::hash!($path); - - $(#[$($meta)*])* - $vis const $name: StaticType = StaticType { - item: ::rune_macros::item!($path), - hash: $hash, - }; - - $( - $(#[$($impl_meta)*])* - impl_static_type!(impl $(<$($p),*>)* for $ty, $name, $hash); - )* - )* - } -} - -any_type! { - ::std::result::Result { - impl for Result; - } - - ::std::option::Option { - impl for Option; - } - - /// The specialized type information for a bool type. - ::std::bool { - impl for bool; - } - - /// The specialized type information for a char type. - ::std::char { - impl for char; - } - - /// The specialized type information for a integer type. - ::std::i64 { - impl for i8; - impl for i16; - impl for i32; - impl for i64; - impl for i128; - impl for isize; - } - - /// The specialized type information for an unsigned integer type. - ::std::u64 { - impl for u8; - impl for u16; - impl for u32; - impl for u64; - impl for u128; - impl for usize; - } - - /// The specialized type information for a float type. - ::std::f64 { - impl for f32; - impl for f64; - } - - ::std::ops::ControlFlow { - impl for ControlFlow; - } - - /// The specialized type information for the [`Bytes`] type. - ::std::bytes::Bytes { - impl for [u8]; - } - - ::std::cmp::Ordering { - impl for Ordering; - } - - /// The specialized type information for a float type. - ::std::string::String { - #[cfg(feature = "alloc")] - #[cfg_attr(rune_docsrs, doc(cfg(feature = "alloc")))] - impl for ::rust_alloc::string::String; - impl for alloc::Box; - impl for str; - } - - /// The specialized type information for the [`Vec`] type. - ::std::vec::Vec { - impl for [rt::Value]; - #[cfg(feature = "alloc")] - #[cfg_attr(rune_docsrs, doc(cfg(feature = "alloc")))] - impl for ::rust_alloc::vec::Vec; - impl for alloc::Vec; - impl for rt::VecTuple; - } - - /// The specialized type information for the [`Tuple`] type. - ::std::tuple::Tuple { - impl for rt::Tuple; - } - - /// The specialized type information for the [`Object`] type. - ::std::object::Object { - impl for rt::Struct; - impl for HashMap<::rust_alloc::string::String, T>; - impl for HashMap; - - #[cfg(feature = "std")] - #[cfg_attr(rune_docsrs, doc(cfg(feature = "std")))] - impl for ::std::collections::HashMap<::rust_alloc::string::String, T>; - - #[cfg(feature = "std")] - #[cfg_attr(rune_docsrs, doc(cfg(feature = "std")))] - impl for ::std::collections::HashMap; - } -} - -static_type! { - pub(crate) static [TYPE, TYPE_HASH] = ::std::any::Type { - impl for rt::Type; - } -} diff --git a/crates/rune/src/runtime/to_value.rs b/crates/rune/src/runtime/to_value.rs index f48997299..8d51490b8 100644 --- a/crates/rune/src/runtime/to_value.rs +++ b/crates/rune/src/runtime/to_value.rs @@ -1,6 +1,6 @@ use crate::alloc::prelude::*; use crate::alloc::{self, HashMap}; -use crate::any::AnyFrom; +use crate::any::AnyMarker; use super::{AnyObj, Object, RuntimeError, Value, VmResult}; @@ -158,7 +158,7 @@ pub trait UnsafeToValue: Sized { impl ToValue for T where - T: AnyFrom, + T: AnyMarker, { #[inline] fn to_value(self) -> Result { diff --git a/crates/rune/src/runtime/tuple.rs b/crates/rune/src/runtime/tuple.rs index b5657e32b..81ae1745c 100644 --- a/crates/rune/src/runtime/tuple.rs +++ b/crates/rune/src/runtime/tuple.rs @@ -348,7 +348,7 @@ impl TryFrom<::rust_alloc::boxed::Box<[ConstValue]>> for OwnedTuple { macro_rules! impl_tuple { // Skip conflicting implementation with `()`. (0) => { - impl_any_type!(impl for (), ::std::tuple::Tuple); + impl_one_builtin_type_of!(impl ::std::tuple::Tuple, ()); impl FromValue for () { #[inline] @@ -366,7 +366,7 @@ macro_rules! impl_tuple { }; ($count:expr $(, $ty:ident $var:ident $ignore_count:expr)*) => { - impl_any_type!(impl <$($ty),*> for ($($ty,)*), ::std::tuple::Tuple); + impl_one_builtin_type_of!(impl <$($ty),*> ::std::tuple::Tuple, ($($ty,)*)); impl <$($ty,)*> FromValue for ($($ty,)*) where diff --git a/crates/rune/src/runtime/type_info.rs b/crates/rune/src/runtime/type_info.rs index 1bc5a6f19..1ff64cdc2 100644 --- a/crates/rune/src/runtime/type_info.rs +++ b/crates/rune/src/runtime/type_info.rs @@ -9,12 +9,10 @@ use crate::{Any, TypeHash}; use ::rust_alloc::sync::Arc; -use super::{Rtti, StaticType, StaticTypeInfo, StaticTypeInfoKind, VariantRtti}; +use super::{Rtti, VariantRtti}; #[derive(Debug, TryClone, PartialEq, Eq)] enum TypeInfoKind { - /// The static type of a value. - StaticType(StaticType), /// Reference to an external type. Any(AnyTypeInfo), /// A named type. @@ -67,11 +65,6 @@ impl TypeInfo { Self::new(TypeInfoKind::Any(type_info)) } - #[doc(hidden)] - pub(crate) const fn static_type(ty: StaticType) -> Self { - Self::new(TypeInfoKind::StaticType(ty)) - } - #[inline] pub(crate) const fn typed(rtti: Arc) -> Self { Self::new(TypeInfoKind::Typed(rtti)) @@ -85,7 +78,6 @@ impl TypeInfo { #[cfg(feature = "emit")] pub(crate) fn type_hash(&self) -> Hash { match &self.kind { - TypeInfoKind::StaticType(ty) => ty.hash, TypeInfoKind::Typed(ty) => ty.hash, TypeInfoKind::Variant(ty) => ty.hash, TypeInfoKind::Any(ty) => ty.hash, @@ -103,9 +95,6 @@ impl fmt::Debug for TypeInfo { impl fmt::Display for TypeInfo { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match &self.kind { - TypeInfoKind::StaticType(ty) => { - write!(f, "{}", ty.item)?; - } TypeInfoKind::Typed(rtti) => { write!(f, "{}", rtti.item)?; } @@ -128,16 +117,6 @@ impl From for TypeInfo { } } -impl From for TypeInfo { - #[inline] - fn from(type_info: StaticTypeInfo) -> Self { - match type_info.into_kind() { - StaticTypeInfoKind::StaticType(static_type) => Self::static_type(static_type), - StaticTypeInfoKind::AnyTypeInfo(any_type_info) => Self::any_type_info(any_type_info), - } - } -} - /// Type information for the [`Any`][crate::Any] type. #[derive(Debug, TryClone, Clone, Copy)] #[try_clone(copy)] diff --git a/crates/rune/src/runtime/type_of.rs b/crates/rune/src/runtime/type_of.rs index 8b0f072ec..fcf308690 100644 --- a/crates/rune/src/runtime/type_of.rs +++ b/crates/rune/src/runtime/type_of.rs @@ -2,7 +2,7 @@ use crate::alloc; use crate::compile::meta; use crate::Hash; -use super::{AnyTypeInfo, Mut, Ref, Shared, StaticType, TypeInfo}; +use super::{AnyTypeInfo, Mut, Ref, Shared, TypeInfo}; /// Static type hash for a given type. /// @@ -60,39 +60,6 @@ where const HASH: Hash = T::HASH; } -/// Static type information. -#[derive(Clone, Copy)] -pub struct StaticTypeInfo { - kind: StaticTypeInfoKind, -} - -impl StaticTypeInfo { - #[inline] - pub(super) fn into_kind(self) -> StaticTypeInfoKind { - self.kind - } - - #[doc(hidden)] - pub const fn static_type(ty: StaticType) -> Self { - Self { - kind: StaticTypeInfoKind::StaticType(ty), - } - } - - #[doc(hidden)] - pub const fn any_type_info(ty: AnyTypeInfo) -> Self { - Self { - kind: StaticTypeInfoKind::AnyTypeInfo(ty), - } - } -} - -#[derive(Clone, Copy)] -pub(super) enum StaticTypeInfoKind { - StaticType(StaticType), - AnyTypeInfo(AnyTypeInfo), -} - /// Trait used for Rust types for which we can determine the runtime type of. pub trait TypeOf: TypeHash { /// Type parameters for the type. @@ -112,7 +79,7 @@ pub trait TypeOf: TypeHash { /// /// [`Debug`]: core::fmt::Debug /// [`Display`]: core::fmt::Display - const STATIC_TYPE_INFO: StaticTypeInfo; + const STATIC_TYPE_INFO: AnyTypeInfo; #[inline] /// Get type info associated with the current type. @@ -183,7 +150,7 @@ where T: ?Sized + TypeOf, { const PARAMETERS: Hash = T::PARAMETERS; - const STATIC_TYPE_INFO: StaticTypeInfo = T::STATIC_TYPE_INFO; + const STATIC_TYPE_INFO: AnyTypeInfo = T::STATIC_TYPE_INFO; } /// Blanket implementation for mutable references. @@ -192,7 +159,7 @@ where T: ?Sized + TypeOf, { const PARAMETERS: Hash = T::PARAMETERS; - const STATIC_TYPE_INFO: StaticTypeInfo = T::STATIC_TYPE_INFO; + const STATIC_TYPE_INFO: AnyTypeInfo = T::STATIC_TYPE_INFO; } /// Blanket implementation for owned references. @@ -209,7 +176,7 @@ where T: ?Sized + TypeOf, { const PARAMETERS: Hash = T::PARAMETERS; - const STATIC_TYPE_INFO: StaticTypeInfo = T::STATIC_TYPE_INFO; + const STATIC_TYPE_INFO: AnyTypeInfo = T::STATIC_TYPE_INFO; } /// Blanket implementation for owned mutable references. @@ -226,7 +193,7 @@ where T: ?Sized + TypeOf, { const PARAMETERS: Hash = T::PARAMETERS; - const STATIC_TYPE_INFO: StaticTypeInfo = T::STATIC_TYPE_INFO; + const STATIC_TYPE_INFO: AnyTypeInfo = T::STATIC_TYPE_INFO; } /// Blanket implementation for owned shared values. @@ -243,5 +210,5 @@ where T: ?Sized + TypeOf, { const PARAMETERS: Hash = T::PARAMETERS; - const STATIC_TYPE_INFO: StaticTypeInfo = T::STATIC_TYPE_INFO; + const STATIC_TYPE_INFO: AnyTypeInfo = T::STATIC_TYPE_INFO; } diff --git a/crates/rune/src/runtime/value/inline.rs b/crates/rune/src/runtime/value/inline.rs index 796a3051f..ceb970c23 100644 --- a/crates/rune/src/runtime/value/inline.rs +++ b/crates/rune/src/runtime/value/inline.rs @@ -7,8 +7,7 @@ use serde::{Deserialize, Serialize}; use crate::hash::Hash; use crate::runtime::{ - static_type, OwnedTuple, Protocol, RuntimeError, Type, TypeInfo, VmErrorKind, VmIntegerRepr, - VmResult, + OwnedTuple, Protocol, RuntimeError, Type, TypeInfo, VmErrorKind, VmIntegerRepr, VmResult, }; use crate::TypeHash; @@ -197,7 +196,7 @@ impl Inline { Inline::Unsigned(..) => TypeInfo::named::(), Inline::Signed(..) => TypeInfo::named::(), Inline::Float(..) => TypeInfo::named::(), - Inline::Type(..) => TypeInfo::static_type(static_type::TYPE), + Inline::Type(..) => TypeInfo::named::(), Inline::Ordering(..) => TypeInfo::named::(), } } @@ -214,7 +213,7 @@ impl Inline { Inline::Signed(..) => i64::HASH, Inline::Unsigned(..) => u64::HASH, Inline::Float(..) => f64::HASH, - Inline::Type(..) => static_type::TYPE.hash, + Inline::Type(..) => Type::HASH, Inline::Ordering(..) => Ordering::HASH, } } diff --git a/crates/rune/src/runtime/vm.rs b/crates/rune/src/runtime/vm.rs index 47672881b..e1c54b52c 100644 --- a/crates/rune/src/runtime/vm.rs +++ b/crates/rune/src/runtime/vm.rs @@ -7,8 +7,10 @@ use core::slice; use ::rust_alloc::sync::Arc; +use crate as rune; use crate::alloc::prelude::*; use crate::alloc::{self, String}; +use crate::hash; use crate::hash::{Hash, IntoHash, ToTypeHash}; use crate::modules::{option, result}; use crate::runtime; @@ -3275,11 +3277,28 @@ impl Vm { let is_match = 'out: { match vm_try!(value.borrow_ref_repr()) { - BorrowRefRepr::Mutable(value) => match &*value { - Mutable::Variant(variant) => { - break 'out variant.rtti().hash == variant_hash; + BorrowRefRepr::Mutable(value) => match (enum_hash, &*value) { + (hash!(::std::result::Result), Mutable::Result(result)) => { + break 'out match (variant_hash, result) { + (hash!(::std::result::Result::Ok), Ok(..)) => true, + (hash!(::std::result::Result::Err), Err(..)) => true, + _ => false, + }; + } + (hash!(::std::option::Option), Mutable::Option(option)) => { + break 'out match (variant_hash, option) { + (hash!(::std::option::Option::None), None) => true, + (hash!(::std::option::Option::Some), Some(..)) => true, + _ => false, + }; + } + (enum_hash, Mutable::Variant(variant)) => { + let rtti = variant.rtti(); + break 'out rtti.enum_hash == enum_hash && rtti.hash == variant_hash; + } + _ => { + break 'out false; } - _ => {} }, BorrowRefRepr::Any(any) => { if any.type_hash() != enum_hash { diff --git a/crates/rune/src/tests/bug_344.rs b/crates/rune/src/tests/bug_344.rs index 28c4f163c..ea5b02702 100644 --- a/crates/rune/src/tests/bug_344.rs +++ b/crates/rune/src/tests/bug_344.rs @@ -13,7 +13,7 @@ use std::cell::Cell; use std::rc::Rc; use rune::compile::meta; -use runtime::{AnyTypeInfo, StaticTypeInfo}; +use runtime::AnyTypeInfo; #[test] fn bug_344_function() -> Result<()> { @@ -165,6 +165,7 @@ impl GuardCheck { } impl Any for GuardCheck {} +impl rune::__private::AnyMarker for GuardCheck {} impl Named for GuardCheck { const ITEM: &'static Item = rune_macros::item!(GuardCheck); @@ -175,8 +176,7 @@ impl TypeHash for GuardCheck { } impl TypeOf for GuardCheck { - const STATIC_TYPE_INFO: StaticTypeInfo = - StaticTypeInfo::any_type_info(AnyTypeInfo::new(Self::full_name, Self::HASH)); + const STATIC_TYPE_INFO: AnyTypeInfo = GuardCheck::ANY_TYPE_INFO; } impl MaybeTypeOf for GuardCheck {