Skip to content

Commit b02e5e2

Browse files
committed
rune: Move ToValue to RuntimeError
1 parent 571bbc0 commit b02e5e2

23 files changed

+237
-196
lines changed

crates/rune-macros/src/any.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ fn expand_enum_install_with(
282282
to_value,
283283
type_of,
284284
vm_result,
285+
vm_try,
285286
..
286287
} = tokens;
287288

@@ -337,9 +338,9 @@ fn expand_enum_install_with(
337338
let fields = field_fns.entry(f_name).or_default();
338339

339340
let value = if attrs.copy {
340-
quote!(#to_value::to_value(*#f_ident))
341+
quote!(#vm_result::Ok(#vm_try!(#to_value::to_value(*#f_ident))))
341342
} else {
342-
quote!(#to_value::to_value(#f_ident.clone()))
343+
quote!(#vm_result::Ok(#vm_try!(#to_value::to_value(#f_ident.clone()))))
343344
};
344345

345346
fields.push(quote!(#ident::#variant_ident { #f_ident, .. } => #value));
@@ -365,9 +366,9 @@ fn expand_enum_install_with(
365366
let n = syn::LitInt::new(&n.to_string(), span);
366367

367368
let value = if attrs.copy {
368-
quote!(#to_value::to_value(*value))
369+
quote!(#vm_result::Ok(#vm_try!(#to_value::to_value(*value))))
369370
} else {
370-
quote!(#to_value::to_value(value.clone()))
371+
quote!(#vm_result::Ok(#vm_try!(#to_value::to_value(value.clone()))))
371372
};
372373

373374
fields.push(quote!(#ident::#variant_ident { #n: value, .. } => #value));

crates/rune-macros/src/const_value.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl Derive {
8989
});
9090

9191
from_value_fields.push(quote! {
92-
<#ty as #from_value>::from_value(#value::take(#var)).into_result()?
92+
<#ty as #from_value>::from_value(#value::take(#var))?
9393
});
9494
}
9595

@@ -98,7 +98,7 @@ impl Derive {
9898
}
9999

100100
body = quote! {
101-
#const_value::for_struct(<Self as #type_hash_t>::HASH, [#(#fields),*])?
101+
#const_value::for_struct(<Self as #type_hash_t>::HASH, [#(#fields),*])
102102
};
103103
}
104104
syn::Data::Enum(..) => {
@@ -161,7 +161,7 @@ where
161161
impl #to_const_value_t for #ident {
162162
#[inline]
163163
fn to_const_value(self) -> #result<#const_value, #runtime_error> {
164-
#result::Ok(#body)
164+
#body
165165
}
166166

167167
#[inline]

crates/rune-macros/src/to_value.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ impl Expander {
2020

2121
let Tokens {
2222
value,
23-
vm_result,
2423
to_value,
24+
result,
25+
runtime_error,
2526
..
2627
} = &self.tokens;
2728

2829
Ok(quote! {
2930
#[automatically_derived]
3031
impl #to_value for #ident {
31-
fn to_value(self) -> #vm_result<#value> {
32+
fn to_value(self) -> #result<#value, #runtime_error> {
3233
#inner
3334
}
3435
}
@@ -58,8 +59,7 @@ impl Expander {
5859
to_value,
5960
value,
6061
owned_tuple,
61-
vm_result,
62-
vm_try,
62+
result,
6363
try_from,
6464
vec,
6565
..
@@ -68,16 +68,16 @@ impl Expander {
6868
for (index, f) in unnamed.unnamed.iter().enumerate() {
6969
let _ = self.cx.field_attrs(&f.attrs)?;
7070
let index = syn::Index::from(index);
71-
to_values.push(quote!(#vm_try!(#vec::try_push(&mut tuple, #vm_try!(#to_value::to_value(self.#index))))));
71+
to_values.push(quote!(#vec::try_push(&mut tuple, #to_value::to_value(self.#index)?)?));
7272
}
7373

7474
let cap = unnamed.unnamed.len();
7575

7676
Ok(quote! {
77-
let mut tuple = #vm_try!(#vec::try_with_capacity(#cap));
77+
let mut tuple = #vec::try_with_capacity(#cap)?;
7878
#(#to_values;)*
79-
let tuple = #vm_try!(<#owned_tuple as #try_from<_>>::try_from(tuple));
80-
#vm_result::Ok(#vm_try!(<#value as #try_from<_>>::try_from(tuple)))
79+
let tuple = <#owned_tuple as #try_from<_>>::try_from(tuple)?;
80+
#result::Ok(<#value as #try_from<_>>::try_from(tuple)?)
8181
})
8282
}
8383

@@ -87,8 +87,7 @@ impl Expander {
8787
to_value,
8888
value,
8989
object,
90-
vm_result,
91-
vm_try,
90+
result,
9291
string,
9392
try_from,
9493
..
@@ -103,14 +102,14 @@ impl Expander {
103102
let name = &syn::LitStr::new(&ident.to_string(), ident.span());
104103

105104
to_values.push(quote! {
106-
object.insert(#vm_try!(<#string as #try_from<_>>::try_from(#name)), #vm_try!(#to_value::to_value(self.#ident)))
105+
object.insert(<#string as #try_from<_>>::try_from(#name)?, #to_value::to_value(self.#ident)?)
107106
});
108107
}
109108

110109
Ok(quote! {
111110
let mut object = <#object>::new();
112111
#(#to_values;)*
113-
#vm_result::Ok(#vm_try!(<#value as #try_from<_>>::try_from(object)))
112+
#result::Ok(<#value as #try_from<_>>::try_from(object)?)
114113
})
115114
}
116115
}

crates/rune/src/function/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use crate::alloc;
88
use crate::compile::meta;
99
use crate::hash::Hash;
1010
use crate::runtime::{
11-
self, FromValue, InstAddress, MaybeTypeOf, Memory, Output, ToValue, TypeHash, TypeInfo, TypeOf,
12-
UnsafeToMut, UnsafeToRef, Value, VmErrorKind, VmResult,
11+
self, FromValue, InstAddress, MaybeTypeOf, Memory, Output, ToReturn, TypeHash, TypeInfo,
12+
TypeOf, UnsafeToMut, UnsafeToRef, Value, VmErrorKind, VmResult,
1313
};
1414

1515
// Expand to function variable bindings.
@@ -242,7 +242,7 @@ macro_rules! impl_function_traits {
242242
impl<T, U, $($ty,)*> Function<($($place,)*), Plain> for T
243243
where
244244
T: 'static + Send + Sync + Fn($($($mut)* $ty),*) -> U,
245-
U: ToValue,
245+
U: ToReturn,
246246
$($ty: $($trait)*,)*
247247
{
248248
type Return = U;
@@ -260,7 +260,7 @@ macro_rules! impl_function_traits {
260260
let ret = self($($var.0),*);
261261
$(drop($var.1);)*
262262

263-
let value = vm_try!(ToValue::to_value(ret));
263+
let value = vm_try!(ToReturn::to_return(ret));
264264
vm_try!(out.store(memory, value));
265265
VmResult::Ok(())
266266
}
@@ -270,7 +270,7 @@ macro_rules! impl_function_traits {
270270
where
271271
T: 'static + Send + Sync + Fn($($($mut)* $ty),*) -> U,
272272
U: 'static + Future,
273-
U::Output: ToValue,
273+
U::Output: ToReturn,
274274
$($ty: $($trait)*,)*
275275
{
276276
type Return = U::Output;
@@ -292,7 +292,7 @@ macro_rules! impl_function_traits {
292292

293293
let ret = vm_try!(runtime::Future::new(async move {
294294
let output = fut.await;
295-
VmResult::Ok(vm_try!(output.to_value()))
295+
VmResult::Ok(vm_try!(ToReturn::to_return(output)))
296296
}));
297297

298298
let value = vm_try!(Value::try_from(ret));

crates/rune/src/modules/future.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,21 @@ async fn join(value: Value) -> VmResult<Value> {
110110
]),
111111
},
112112
BorrowRefRepr::Mutable(value) => match *value {
113-
Mutable::Tuple(ref tuple) => VmResult::Ok(vm_try!(
114-
try_join_impl(tuple.iter(), tuple.len(), |vec| VmResult::Ok(vm_try!(
115-
Value::tuple(vec)
116-
)))
117-
.await
118-
)),
119-
Mutable::Vec(ref vec) => VmResult::Ok(vm_try!(
120-
try_join_impl(vec.iter(), vec.len(), Value::vec).await
121-
)),
113+
Mutable::Tuple(ref tuple) => {
114+
let result = try_join_impl(tuple.iter(), tuple.len(), |vec| {
115+
VmResult::Ok(vm_try!(Value::tuple(vec)))
116+
})
117+
.await;
118+
119+
VmResult::Ok(vm_try!(result))
120+
}
121+
Mutable::Vec(ref vec) => {
122+
let result = try_join_impl(vec.iter(), vec.len(), |vec| {
123+
VmResult::Ok(vm_try!(Value::vec(vec)))
124+
})
125+
.await;
126+
VmResult::Ok(vm_try!(result))
127+
}
122128
ref value => VmResult::err([
123129
VmErrorKind::bad_argument(0),
124130
VmErrorKind::expected::<crate::runtime::Vec>(value.type_info()),

crates/rune/src/runtime/const_value.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::alloc::prelude::*;
88
use crate::alloc::{self, HashMap};
99
use crate::runtime::{
1010
self, BorrowRefRepr, Bytes, FromValue, Inline, Mutable, Object, OwnedTuple, RawStr, ToValue,
11-
TypeInfo, Value, VmErrorKind, VmResult,
11+
TypeInfo, Value, VmErrorKind,
1212
};
1313
use crate::{Hash, TypeHash};
1414

@@ -295,11 +295,8 @@ impl FromValue for ConstValue {
295295

296296
impl ToValue for ConstValue {
297297
#[inline]
298-
fn to_value(self) -> VmResult<Value> {
299-
VmResult::Ok(vm_try!(ConstValue::to_value_with(
300-
&self,
301-
&EmptyConstContext
302-
)))
298+
fn to_value(self) -> Result<Value, RuntimeError> {
299+
ConstValue::to_value_with(&self, &EmptyConstContext)
303300
}
304301
}
305302

crates/rune/src/runtime/control_flow.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,13 @@ where
172172
C: ToValue,
173173
{
174174
#[inline]
175-
fn to_value(self) -> VmResult<Value> {
175+
fn to_value(self) -> Result<Value, RuntimeError> {
176176
let value = match self {
177-
ops::ControlFlow::Continue(value) => {
178-
ControlFlow::Continue(vm_try!(ToValue::to_value(value)))
179-
}
180-
ops::ControlFlow::Break(value) => ControlFlow::Break(vm_try!(ToValue::to_value(value))),
177+
ops::ControlFlow::Continue(value) => ControlFlow::Continue(C::to_value(value)?),
178+
ops::ControlFlow::Break(value) => ControlFlow::Break(B::to_value(value)?),
181179
};
182180

183-
VmResult::Ok(vm_try!(Value::try_from(value)))
181+
Ok(Value::try_from(value)?)
184182
}
185183
}
186184

crates/rune/src/runtime/future.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ impl Future {
4545
poll: |future, cx| unsafe {
4646
match Pin::new_unchecked(&mut *future.cast::<T>()).poll(cx) {
4747
Poll::Pending => Poll::Pending,
48-
Poll::Ready(VmResult::Ok(result)) => Poll::Ready(result.to_value()),
48+
Poll::Ready(VmResult::Ok(result)) => match result.to_value() {
49+
Ok(value) => Poll::Ready(VmResult::Ok(value)),
50+
Err(err) => Poll::Ready(VmResult::Err(err.into())),
51+
},
4952
Poll::Ready(VmResult::Err(err)) => Poll::Ready(VmResult::Err(err)),
5053
}
5154
},

crates/rune/src/runtime/generator.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::iter;
33

44
use crate as rune;
55
use crate::alloc::clone::TryClone;
6-
use crate::runtime::{GeneratorState, Value, Vm, VmErrorKind, VmExecution, VmResult};
6+
use crate::runtime::{GeneratorState, Value, Vm, VmError, VmErrorKind, VmExecution, VmResult};
77
use crate::Any;
88

99
/// The return value of a function producing a generator.
@@ -112,7 +112,7 @@ impl Generator<Vm> {
112112
}
113113

114114
impl IntoIterator for Generator<Vm> {
115-
type Item = VmResult<Value>;
115+
type Item = Result<Value, VmError>;
116116
type IntoIter = Iter;
117117

118118
#[inline]
@@ -129,21 +129,21 @@ pub struct Iter {
129129

130130
impl Iter {
131131
#[rune::function(instance, keep, protocol = NEXT)]
132-
pub(crate) fn next(&mut self) -> Option<VmResult<Value>> {
133-
match self.generator.next() {
134-
VmResult::Ok(Some(value)) => Some(VmResult::Ok(value)),
135-
VmResult::Ok(None) => None,
136-
VmResult::Err(error) => Some(VmResult::Err(error)),
137-
}
132+
pub(crate) fn next(&mut self) -> VmResult<Option<Value>> {
133+
self.generator.next()
138134
}
139135
}
140136

141137
impl iter::Iterator for Iter {
142-
type Item = VmResult<Value>;
138+
type Item = Result<Value, VmError>;
143139

144140
#[inline]
145-
fn next(&mut self) -> Option<VmResult<Value>> {
146-
Iter::next(self)
141+
fn next(&mut self) -> Option<Result<Value, VmError>> {
142+
match Iter::next(self) {
143+
VmResult::Ok(Some(value)) => Some(Ok(value)),
144+
VmResult::Ok(None) => None,
145+
VmResult::Err(error) => Some(Err(error)),
146+
}
147147
}
148148
}
149149

crates/rune/src/runtime/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ mod stream;
149149
pub use self::stream::Stream;
150150

151151
mod to_value;
152-
pub use self::to_value::{to_value, ToValue, UnsafeToValue};
152+
pub use self::to_value::{to_value, ToReturn, ToValue, UnsafeToValue};
153153

154154
mod tuple;
155155
pub use self::tuple::{OwnedTuple, Tuple};

0 commit comments

Comments
 (0)