10000 rune: Store Option and Result in AnyObj instead of Mutable (relates #… · rune-rs/rune@6123430 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6123430

Browse files
committed
rune: Store Option and Result in AnyObj instead of Mutable (relates #844)
1 parent 6c4298d commit 6123430

File tree

12 files changed

+194
-307
lines changed

12 files changed

+194
-307
lines changed

crates/rune-macros/src/any.rs

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -669,32 +669,7 @@ where
669669
let type_parameters =
670670
quote!(#hash::parameters([#(<#generic_names as #type_hash_t>::HASH),*]));
671671

672-
let impl_type_of = is_generic.is_none().then(|| quote! {
673-
#[automatically_derived]
674-
#(#attrs)*
675-
impl #impl_generics #type_hash_t for #ident #type_generics #where_clause {
676-
const HASH: #hash = #make_hash;
677-
}
678-
679-
#[automatically_derived]
680-
#(#attrs)*
681-
impl #impl_generics #type_of for #ident #type_generics #where_clause {
682-
const PARAMETERS: #hash = #type_parameters;
683-
const STATIC_TYPE_INFO: #any_type_info = <Self as #any_t>::ANY_TYPE_INFO;
684-
}
685-
686-
#[automatically_derived]
687-
#(#attrs)*
688-
impl #impl_generics #maybe_type_of for #ident #type_generics #where_clause {
689-
#[inline]
690-
fn maybe_type_of() -> #alloc::Result<#meta::DocType> {
691-
#meta::DocType::with_generics(
692-
<Self as #type_hash_t>::HASH,
693-
[#(<#generic_names as #maybe_type_of>::maybe_type_of()?),*]
694-
)
695-
}
696-
}
697-
672+
let to_value_impl = quote! {
698673
#[automatically_derived]
699674
#(#attrs)*
700675
impl #impl_generics #unsafe_to_ref for #ident #type_generics #where_clause {
@@ -738,6 +713,35 @@ where
738713
#vm_result::Ok((shared, guard))
739714
}
740715
}
716+
};
717+
718+
let impl_type_of = is_generic.is_none().then(|| {
719+
quote! {
720+
#[automatically_derived]
721+
#(#attrs)*
722+
impl #impl_generics #type_hash_t for #ident #type_generics #where_clause {
723+
const HASH: #hash = #make_hash;
724+
}
725+
726+
#[automatically_derived]
727+
#(#attrs)*
728+
impl #impl_generics #type_of for #ident #type_generics #where_clause {
729+
const PARAMETERS: #hash = #type_parameters;
730+
const STATIC_TYPE_INFO: #any_type_info = <Self as #any_t>::ANY_TYPE_INFO;
731+
}
732+
733+
#[automatically_derived]
734+
#(#attrs)*
735+
impl #impl_generics #maybe_type_of for #ident #type_generics #where_clause {
736+
#[inline]
737+
fn maybe_type_of() -> #alloc::Result<#meta::DocType> {
738+
#meta::DocType::with_generics(
739+
<Self as #type_hash_t>::HASH,
740+
[#(<#generic_names as #maybe_type_of>::maybe_type_of()?),*]
741+
)
742+
}
743+
}
744+
}
741745
});
742746

743747
let impl_any = quote! {
@@ -759,6 +763,7 @@ where
759763
quote! {
760764
#install_with
761765
#impl_named
766+
#to_value_impl
762767
#impl_type_of
763768
#impl_any
764769
#impl_non_generic

crates/rune/src/cli/tests.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use crate::cli::{
1818
use crate::compile::FileSourceLoader;
1919
use crate::doc::{TestKind, TestParams};
2020
use crate::modules::capture_io::CaptureIo;
21-
use crate::runtime::{Mutable, OwnedRepr, Value, Vm, VmError, VmResult};
22-
use crate::{Diagnostics, Hash, Item, ItemBuf, Source, Sources, Unit};
21+
use crate::runtime::{RefRepr, Value, Vm, VmError, VmResult};
22+
use crate::{Diagnostics, Hash, Item, ItemBuf, Source, Sources, TypeHash, Unit};
2323

2424
mod cli {
2525
use std::string::String;
@@ -534,14 +534,25 @@ impl TestCase {
534534
capture_io.drain_into(&mut self.output)?;
535535

536536
self.outcome = match result {
537-
VmResult::Ok(v) => match v.take_repr()? {
538-
OwnedRepr::Mutable(Mutable::Result(result)) => match result {
539-
Ok(..) => Outcome::Ok,
540-
Err(error) => Outcome::Err(error),
541-
},
542-
OwnedRepr::Mutable(Mutable::Option(option)) => match option {
543-
Some(..) => Outcome::Ok,
544-
None => Outcome::None,
537+
VmResult::Ok(v) => match v.as_ref_repr()? {
538+
RefRepr::Any(value) => match value.type_hash() {
539+
Result::<Value, Value>::HASH => {
540+
let result = value.borrow_ref::<Result<Value, Value>>()?;
541+
542+
match &*result {
543+
Ok(..) => Outcome::Ok,
544+
Err(error) => Outcome::Err(error.clone()),
545+
}
546+
}
547+
Option::<Value>::HASH => {
548+
let option = value.borrow_ref::<Option<Value>>()?;
549+
550+
match &*option {
551+
Some(..) => Outcome::Ok,
552+
None => Outcome::None,
553+
}
554+
}
555+
_ => Outcome::Ok,
545556
},
546557
_ => Outcome::Ok,
547558
},

crates/rune/src/internal_macros.rs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -87,48 +87,6 @@ macro_rules! cfg_std {
8787
}
8888
}
8989

90-
macro_rules! from_value_ref {
91-
($ty:ty, $into_ref:ident, $into_mut:ident) => {
92-
impl $crate::runtime::UnsafeToRef for $ty {
93-
type Guard = $crate::runtime::RawAnyGuard;
94-
95-
unsafe fn unsafe_to_ref<'a>(
96-
value: $crate::runtime::Value,
97-
) -> $crate::runtime::VmResult<(&'a Self, Self::Guard)> {
98-
let value = vm_try!(value.$into_ref());
99-
let (value, guard) = $crate::runtime::Ref::into_raw(value);
100-
$crate::runtime::VmResult::Ok((value.as_ref(), guard))
101-
}
102-
}
103-
104-
impl $crate::runtime::UnsafeToMut for $ty {
105-
type Guard = $crate::runtime::RawAnyGuard;
106-
107-
unsafe fn unsafe_to_mut<'a>(
108-
value: $crate::runtime::Value,
109-
) -> $crate::runtime::VmResult<(&'a mut Self, Self::Guard)> {
110-
let value = vm_try!(value.$into_mut());
111-
let (mut value, guard) = $crate::runtime::Mut::into_raw(value);
112-
$crate::runtime::VmResult::Ok((value.as_mut(), guard))
113-
}
114-
}
115-
116-
impl $crate::runtime::FromValue for $crate::runtime::Ref<$ty> {
117-
fn from_value(value: Value) -> Result<Self, $crate::runtime::RuntimeError> {
118-
let value = value.$into_ref()?;
119-
Ok(value)
120-
}
121-
}
122-
123-
impl $crate::runtime::FromValue for $crate::runtime::Mut<$ty> {
124-
fn from_value(value: Value) -> Result<Self, $crate::runtime::RuntimeError> {
125-
let value = value.$into_mut()?;
126-
Ok(value)
127-
}
128-
}
129-
};
130-
}
131-
13290
macro_rules! impl_builtin_type_of {
13391
(
13492
$(

crates/rune/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,6 @@ mod tests;
682682
rune_macros::binding! {
683683
#[generic]
684684
impl ::std::option::Option for Option<Value>;
685-
686685
#[generic]
687686
impl ::std::result::Result for Result<Value, Value>;
688687

@@ -763,6 +762,3 @@ impl_builtin_type_of! {
763762

764763
impl ::std::any::Type, crate::runtime::Type;
765764
}
766-
767-
from_value_ref!(Result<Value, Value>, into_result_ref, into_result_mut);
768-
from_value_ref!(Option<Value>, into_option_ref, into_option_mut);

crates/rune/src/modules/option.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ fn transpose(this: &Option<Value>) -> VmResult<Value> {
329329
}
330330
};
331331

332-
match &*vm_try!(value.borrow_result_ref()) {
332+
match &*vm_try!(value.borrow_ref::<Result<Value, Value>>()) {
333333
Ok(ok) => {
334334
let some = vm_try!(Value::try_from(Some(ok.clone())));
335335
let result = vm_try!(Value::try_from(Ok(some)));

crates/rune/src/runtime/const_value.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use crate::runtime;
1616
use crate::{Hash, TypeHash};
1717

1818
use super::{
19-
BorrowRefRepr, Bytes, FromValue, Inline, Mutable, Object, OwnedTuple, ToValue, Tuple, Type,
20-
TypeInfo, Value, VmErrorKind,
19+
BorrowRefRepr, Bytes, FromValue, Inline, Object, OwnedTuple, ToValue, Tuple, Type, TypeInfo,
20+
Value, VmErrorKind,
2121
};
2222

2323
/// Derive for the [`ToConstValue`](trait@ToConstValue) trait.
@@ -237,17 +237,21 @@ impl ConstValue {
237237
let inner = match value.borrow_ref_repr()? {
238238
BorrowRefRepr::Inline(value) => ConstValueKind::Inline(*value),
239239
BorrowRefRepr::Mutable(value) => match &*value {
240-
Mutable::Option(option) => ConstValueKind::Option(match option {
241-
Some(some) => Some(Box::try_new(Self::from_value_ref(some)?)?),
242-
None => None,
243-
}),
244240
value => {
245241
return Err(RuntimeError::from(VmErrorKind::ConstNotSupported {
246242
actual: value.type_info(),
247243
}))
248244
}
249245
},
250246
BorrowRefRepr::Any(value) => match value.type_hash() {
247+
Option::<Value>::HASH => {
248+
let option = value.borrow_ref::<Option<Value>>()?;
249+
250+
ConstValueKind::Option(match &*option {
251+
Some(some) => Some(Box::try_new(Self::from_value_ref(some)?)?),
252+
None => None,
253+
})
254+
}
251255
String::HASH => {
252256
let s = value.borrow_ref::<String>()?;
253257
ConstValueKind::String(s.try_to_owned()?)

crates/rune/src/runtime/from_value.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ where
291291
T: FromValue,
292292
{
293293
fn from_value(value: Value) -> Result<Self, RuntimeError> {
294-
Ok(match &*value.into_option_ref()? {
294+
Ok(match value.into_any::<Option<Value>>()? {
295295
Some(some) => Some(T::from_value(some.clone())?),
296296
None => None,
297297
})
@@ -356,7 +356,7 @@ where
356356
{
357357
#[inline]
358358
fn from_value(value: Value) -> Result<Self, RuntimeError> {
359-
Ok(match &*value.into_result_ref()? {
359+
Ok(match value.into_any::<Result<Value, Value>>()? {
360360
Ok(ok) => Result::Ok(T::from_value(ok.clone())?),
361361
Err(err) => Result::Err(E::from_value(err.clone())?),
362362
})

crates/rune/src/runtime/inst.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,6 @@ pub enum TypeCheck {
6565
Object,
6666
/// Matches a vector.
6767
Vec,
68-
/// An option type, and the specified variant index.
69-
#[musli(packed)]
70-
Option(usize),
71-
/// A result type, and the specified variant index.
72-
#[musli(packed)]
73-
Result(usize),
7468
}
7569

7670
impl fmt::Display for TypeCheck {
@@ -80,10 +74,6 @@ impl fmt::Display for TypeCheck {
8074
Self::Tuple => write!(fmt, "Tuple"),
8175
Self::Object => write!(fmt, "Object"),
8276
Self::Vec => write!(fmt, "Vec"),
83-
Self::Option(0) => write!(fmt, "Option::Some"),
84-
Self::Option(..) => write!(fmt, "Option::None"),
85-
Self::Result(0) => write!(fmt, "Result::Ok"),
86-
Self::Result(..) => write!(fmt, "Result::Err"),
8777
}
8878
}
8979
}

0 commit comments

Comments
 (0)
0