Skip to content

Commit 571bbc0

Browse files
committed
rune: Move FromValue to RuntimeError
1 parent 74a144d commit 571bbc0

29 files changed

+325
-320
lines changed

crates/rune-macros/src/any.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ fn expand_enum_install_with(
278278
) -> Result<(), ()> {
279279
let Tokens {
280280
protocol,
281+
runtime_error,
281282
to_value,
282283
type_of,
283284
vm_result,
@@ -422,7 +423,7 @@ fn expand_enum_install_with(
422423
module.field_function(#protocol::GET, #field, |this: &Self| {
423424
match this {
424425
#(#matches,)*
425-
_ => return #vm_result::__rune_macros__unsupported_object_field_get(<Self as #type_of>::type_info()),
426+
_ => return #vm_result::err(#runtime_error::__rune_macros__unsupported_object_field_get(<Self as #type_of>::type_info())),
426427
}
427428
})?;
428429
});
@@ -433,7 +434,7 @@ fn expand_enum_install_with(
433434
module.index_function(#protocol::GET, #index, |this: &Self| {
434435
match this {
435436
#(#matches,)*
436-
_ => return #vm_result::__rune_macros__unsupported_tuple_index_get(<Self as #type_of>::type_info(), #index),
437+
_ => return #vm_result::err(#runtime_error::__rune_macros__unsupported_tuple_index_get(<Self as #type_of>::type_info(), #index)),
437438
}
438439
})?;
439440
});
@@ -533,6 +534,8 @@ where
533534
value,
534535
vm_result,
535536
vm_try,
537+
runtime_error,
538+
result,
536539
..
537540
} = &tokens;
538541

@@ -711,9 +714,8 @@ where
711714

712715
Some(quote! {
713716
impl #from_value for #ty {
714-
fn from_value(value: Value) -> #vm_result<Self> {
715-
let value = #vm_try!(#path(value));
716-
#vm_result::Ok(value)
717+
fn from_value(value: Value) -> #result<Self, #runtime_error> {
718+
#path(value)
717719
}
718720
}
719721
})
@@ -742,9 +744,8 @@ where
742744
}
743745

744746
impl #from_value for #ref_<#ty> {
745-
fn from_value(value: Value) -> #vm_result<Self> {
746-
let value = #vm_try!(#path(value));
747-
#vm_result::Ok(value)
747+
fn from_value(value: #value) -> #result<Self, #runtime_error> {
748+
#path(value)
748749
}
749750
}
750751
})
@@ -773,9 +774,8 @@ where
773774
}
774775

775776
impl #from_value for #mut_<#ty> {
776-
fn from_value(value: Value) -> #vm_result<Self> {
777-
let value = #vm_try!(#path(value));
778-
#vm_result::Ok(value)
777+
fn from_value(value: #value) -> #result<Self, #runtime_error> {
778+
#path(value)
779779
}
780780
}
781781
})

crates/rune-macros/src/context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,10 @@ impl Context {
211211
return Ok(());
212212
}
213213

214-
return Err(syn::Error::new_spanned(
214+
Err(syn::Error::new_spanned(
215215
&meta.path,
216216
"Unsupported field attribute",
217-
));
217+
))
218218
});
219219

220220
if let Err(e) = result {
@@ -493,10 +493,10 @@ impl Context {
493493
return Ok(());
494494
}
495495

496-
return Err(syn::Error::new_spanned(
496+
Err(syn::Error::new_spanned(
497497
&meta.path,
498498
"Unsupported type attribute",
499-
));
499+
))
500500
});
501501

502502
if let Err(e) = result {

crates/rune-macros/src/from_value.rs

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ impl Expander {
2121
value,
2222
type_value,
2323
from_value,
24-
vm_result,
24+
result,
2525
tuple,
26-
vm_try,
26+
runtime_error,
2727
..
2828
} = &self.tokens;
2929

3030
let (expanded, expected) = match &st.fields {
3131
syn::Fields::Unit => {
3232
let expanded = quote! {
3333
#type_value::Unit => {
34-
#vm_result::Ok(Self)
34+
#result::Ok(Self)
3535
}
3636
#type_value::EmptyStruct(..) => {
37-
#vm_result::Ok(Self)
37+
#result::Ok(Self)
3838
}
3939
};
4040

@@ -46,13 +46,13 @@ impl Expander {
4646
let expanded = quote! {
4747
#type_value::Unit => {
4848
let tuple = #tuple::new(&[]);
49-
#vm_result::Ok(Self(#expanded))
49+
#result::Ok(Self(#expanded))
5050
}
5151
#type_value::Tuple(tuple) => {
52-
#vm_result::Ok(Self(#expanded))
52+
#result::Ok(Self(#expanded))
5353
}
5454
#type_value::TupleStruct(tuple) => {
55-
#vm_result::Ok(Self(#expanded))
55+
#result::Ok(Self(#expanded))
5656
}
5757
};
5858

@@ -63,10 +63,10 @@ impl Expander {
6363

6464
let expanded = quote! {
6565
#type_value::Object(object) => {
66-
#vm_result::Ok(Self { #expanded })
66+
#result::Ok(Self { #expanded })
6767
}
6868
#type_value::Struct(object) => {
69-
#vm_result::Ok(Self { #expanded })
69+
#result::Ok(Self { #expanded })
7070
}
7171
};
7272

@@ -77,11 +77,11 @@ impl Expander {
7777
Ok(quote! {
7878
#[automatically_derived]
7979
impl #from_value for #ident {
80-
fn from_value(value: #value) -> #vm_result<Self> {
81-
match #vm_try!(#value::into_type_value(value)) {
80+
fn from_value(value: #value) -> #result<Self, #runtime_error> {
81+
match #value::into_type_value(value)? {
8282
#expanded
8383
actual => {
84-
#vm_result::expected::<#expected>(#type_value::type_info(&actual))
84+
#result::Err(#runtime_error::expected::<#expected>(#type_value::type_info(&actual)))
8585
}
8686
}
8787
}
@@ -106,8 +106,8 @@ impl Expander {
106106
from_value,
107107
variant_data,
108108
value,
109-
vm_result,
110-
vm_try,
109+
result,
110+
runtime_error,
111111
..
112112
} = &self.tokens;
113113

@@ -118,37 +118,38 @@ impl Expander {
118118
match &variant.fields {
119119
syn::Fields::Unit => {
120120
unit_matches.push(quote! {
121-
#lit_str => #vm_result::Ok(Self::#ident)
121+
#lit_str => #result::Ok(Self::#ident)
122122
});
123123
}
124124
syn::Fields::Unnamed(named) => {
125125
let expanded = self.expand_unnamed(named)?;
126126

127127
unnamed_matches.push(quote! {
128-
#lit_str => #vm_result::Ok(Self::#ident ( #expanded ))
128+
#lit_str => #result::Ok(Self::#ident ( #expanded ))
129129
});
130130
}
131131
syn::Fields::Named(named) => {
132132
let expanded = self.expand_named(named)?;
133133

134134
named_matches.push(quote! {
135-
#lit_str => #vm_result::Ok(Self::#ident { #expanded })
135+
#lit_str => #result::Ok(Self::#ident { #expanded })
136136
});
137137
}
138138
}
139139
}
140140

141141
let missing = quote! {
142-
name => #vm_try!(#vm_result::__rune_macros__missing_variant(name))
142+
name => {
143+
return #result::Err(#runtime_error::__rune_macros__missing_variant(name)?);
144+
}
143145
};
144146

145147
let variant = quote! {
146148
#type_value::Variant(variant) => {
147149
let mut it = variant.rtti().item.iter();
148150

149-
let name = match it.next_back_str() {
150-
Some(name) => name,
151-
None => return #vm_result::__rune_macros__missing_variant_name(),
151+
let Some(name) = it.next_back_str() else {
152+
return #result::Err(#runtime_error::__rune_macros__missing_variant_name());
152153
};
153154

154155
match variant.data() {
@@ -168,11 +169,11 @@ impl Expander {
168169
Ok(quote! {
169170
#[automatically_derived]
170171
impl #from_value for #ident {
171-
fn from_value(value: #value) -> #vm_result<Self> {
172-
match #vm_try!(#value::into_type_value(value)) {
172+
fn from_value(value: #value) -> #result<Self, #runtime_error> {
173+
match #value::into_type_value(value)? {
173174
#variant,
174175
actual => {
175-
#vm_result::__rune_macros__expected_variant(#type_value::type_info(&actual))
176+
#result::Err(#runtime_error::__rune_macros__expected_variant(#type_value::type_info(&actual)))
176177
}
177178
}
178179
}
@@ -200,10 +201,10 @@ impl Expander {
200201

201202
let Tokens {
202203
from_value,
203-
vm_result,
204+
result,
204205
type_name,
205-
vm_try,
206206
try_clone,
207+
runtime_error,
207208
..
208209
} = &self.tokens;
209210

@@ -213,11 +214,11 @@ impl Expander {
213214
from_values.push(quote! {
214215
match tuple.get(#index) {
215216
Some(value) => {
216-
let value = #vm_try!(#try_clone::try_clone(value));
217-
#vm_try!(#from_value::from_value(value))
217+
let value = #try_clone::try_clone(value)?;
218+
#from_value::from_value(value)?
218219
}
219220
None => {
220-
return #vm_result::__rune_macros__missing_tuple_index(#type_name::<Self>(), #index);
221+
return #result::Err(#runtime_error::__rune_macros__missing_tuple_index(#type_name::<Self>(), #index));
221222
}
222223
}
223224
});
@@ -238,24 +239,24 @@ impl Expander {
238239

239240
let Tokens {
240241
from_value,
241-
vm_result,
242+
result,
243+
runtime_error,
242244
type_name,
243-
vm_try,
244245
..
245246
} = &self.tokens;
246247

247248
from_values.push(quote_spanned! {
248249
field.span() =>
249250
#ident: match object.get(#name) {
250-
Some(value) => #vm_try!(#from_value::from_value(value.clone())),
251+
Some(value) => #from_value::from_value(value.clone())?,
251252
None => {
252-
return #vm_result::__rune_macros__missing_struct_field(#type_name::<Self>(), #name);
253+
return #result::Err(#runtime_error::__rune_macros__missing_struct_field(#type_name::<Self>(), #name));
253254
}
254255
}
255256
});
256257
}
257258

258-
Ok(quote_spanned!(named.span() => #(#from_values),* ))
259+
Ok(quote!(#(#from_values),*))
259260
}
260261
}
261262

crates/rune/src/compile/ir/compiler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub(crate) fn expr(hir: &hir::Expr<'_>, c: &mut Ctxt<'_, '_>) -> compile::Result
5151
));
5252
};
5353

54-
let value = value.to_value(c.q.context).with_span(span)?;
54+
let value = value.to_value_with(c.q.context).with_span(span)?;
5555
ir::Ir::new(span, value)
5656
}
5757
hir::ExprKind::Variable(name) => {

crates/rune/src/compile/ir/interpreter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl Interpreter<'_, '_> {
105105
.alloc_item(base.extended(name.try_to_string()?)?)?;
106106

107107
if let Some(const_value) = self.q.consts.get(item) {
108-
return Ok(const_value.to_value(self.q.context).with_span(span)?);
108+
return Ok(const_value.to_value_with(self.q.context).with_span(span)?);
109109
}
110110

111111
if let Some(meta) = self.q.query_meta(span, item, used)? {
@@ -118,7 +118,7 @@ impl Interpreter<'_, '_> {
118118
));
119119
};
120120

121-
return Ok(const_value.to_value(self.q.context).with_span(span)?);
121+
return Ok(const_value.to_value_with(self.q.context).with_span(span)?);
122122
}
123123
_ => {
124124
return Err(compile::Error::new(

crates/rune/src/internal_macros.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,16 @@ macro_rules! from_value_ref {
116116
}
117117

118118
impl $crate::runtime::FromValue for $crate::runtime::Ref<$ty> {
119-
fn from_value(value: Value) -> $crate::runtime::VmResult<Self> {
120-
let value = vm_try!(value.$into_ref());
121-
$crate::runtime::VmResult::Ok(value)
119+
fn from_value(value: Value) -> Result<Self, $crate::runtime::RuntimeError> {
120+
let value = value.$into_ref()?;
121+
Ok(value)
122122
}
123123
}
124124

125125
impl $crate::runtime::FromValue for $crate::runtime::Mut<$ty> {
126-
fn from_value(value: Value) -> VmResult<Self> {
127-
let value = vm_try!(value.$into_mut());
128-
$crate::runtime::VmResult::Ok(value)
126+
fn from_value(value: Value) -> Result<Self, $crate::runtime::RuntimeError> {
127+
let value = value.$into_mut()?;
128+
Ok(value)
129129
}
130130
}
131131
};
@@ -135,9 +135,9 @@ macro_rules! from_value_ref {
135135
macro_rules! from_value2 {
136136
($ty:ty, $into_ref:ident, $into_mut:ident, $into:ident) => {
137137
impl $crate::runtime::FromValue for $ty {
138-
fn from_value(value: Value) -> $crate::runtime::VmResult<Self> {
139-
let value = vm_try!(value.$into());
140-
$crate::runtime::VmResult::Ok(value)
138+
fn from_value(value: Value) -> Result<Self, $crate::runtime::RuntimeError> {
139+
let value = value.$into()?;
140+
Ok(value)
141141
}
142142
}
143143

0 commit comments

Comments
 (0)