8000 rune: Store String in AnyObj instead of Mutable (relates #844) · rune-rs/rune@4ee91fa · GitHub
[go: up one dir, main page]

Skip to content

Commit 4ee91fa

Browse files
committed
rune: Store String in AnyObj instead of Mutable (relates #844)
1 parent 7a35743 commit 4ee91fa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+546
-459
lines changed

crates/rune-modules/src/base64.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ impl From<base64::DecodeSliceError> for DecodeError {
9696
impl DecodeError {
9797
#[rune::function(instance, protocol = STRING_DISPLAY)]
9898
fn string_display(&self, f: &mut Formatter) -> VmResult<()> {
99-
rune::vm_write!(f, "{}", self.inner);
100-
VmResult::Ok(())
99+
rune::vm_write!(f, "{}", self.inner)
101100
}
102101
}

crates/rune-modules/src/http.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ impl From<reqwest::Error> for Error {
108108
impl Error {
109109
#[rune::function(instance, protocol = STRING_DISPLAY)]
110110
fn string_display(&self, f: &mut Formatter) -> VmResult<()> {
111-
rune::vm_write!(f, "{}", self.inner);
112-
VmResult::Ok(())
111+
rune::vm_write!(f, "{}", self.inner)
113112
}
114113
}
115114

@@ -189,8 +188,7 @@ pub struct StatusCode {
189188
impl StatusCode {
190189
#[rune::function(instance, protocol = STRING_DISPLAY)]
191190
fn string_display(&self, f: &mut Formatter) -> VmResult<()> {
192-
rune::vm_write!(f, "{}", self.inner);
193-
VmResult::Ok(())
191+
rune::vm_write!(f, "{}", self.inner)
194192
}
195193
}
196194

crates/rune-modules/src/json.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
//! ```
3131
3232
use rune::{ContextError, Module, vm_write, Any};
33-
use rune::runtime::{Bytes, Value, Formatter};
33+
use rune::runtime::{Bytes, Formatter, Value, VmResult};
3434
use rune::alloc::{Vec, String};
3535
use rune::alloc::fmt::TryWrite;
3636

@@ -64,14 +64,14 @@ struct Error {
6464
}
6565

6666
impl Error {
67-
#[rune::function(vm_result, protocol = STRING_DISPLAY)]
68-
pub(crate) fn display(&self, f: &mut Formatter) {
69-
vm_write!(f, "{}", self.error);
67+
#[rune::function(protocol = STRING_DISPLAY)]
68+
pub(crate) fn display(&self, f: &mut Formatter) -> VmResult<()> {
69+
vm_write!(f, "{}", self.error)
7070
}
7171

72-
#[rune::function(vm_result, protocol = STRING_DEBUG)]
73-
pub(crate) fn debug(&self, f: &mut Formatter) {
74-
vm_write!(f, "{:?}", self.error);
72+
#[rune::function(protocol = STRING_DEBUG)]
73+
pub(crate) fn debug(&self, f: &mut Formatter) -> VmResult<()> {
74+
vm_write!(f, "{:?}", self.error)
7575
}
7676
}
7777

crates/rune-modules/src/process.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ struct ExitStatus {
148148
impl ExitStatus {
149149
#[rune::function(protocol = STRING_DISPLAY)]
150150
fn string_display(&self, f: &mut Formatter) -> VmResult<()> {
151-
rune::vm_write!(f, "{}", self.status);
152-
VmResult::Ok(())
151+
rune::vm_write!(f, "{}", self.status)
153152
}
154153

155154
#[rune::function]

crates/rune-modules/src/toml.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,12 @@ pub mod de {
6767
impl Error {
6868
#[rune::function(protocol = STRING_DISPLAY)]
6969
pub(crate) fn display(&self, f: &mut Formatter) -> VmResult<()> {
70-
vm_write!(f, "{}", self.error);
71-
VmResult::Ok(())
70+
vm_write!(f, "{}", self.error)
7271
}
7372

7473
#[rune::function(protocol = STRING_DEBUG)]
7574
pub(crate) fn debug(&self, f: &mut Formatter) -> VmResult<()> {
76-
vm_write!(f, "{:?}", self.error);
77-
VmResult::Ok(())
75+
vm_write!(f, "{:?}", self.error)
7876
}
7977
}
8078

@@ -89,7 +87,7 @@ pub mod ser {
8987
//! Serializer types for the toml module.
9088
9189
use rune::{Any, Module, ContextError, vm_write};
92-
use rune::runtime::Formatter;
90+
use rune::runtime::{Formatter, VmResult};
9391
use rune::alloc::fmt::TryWrite;
9492

9593
pub fn module(_stdio: bool) -> Result<Module, ContextError> {
@@ -107,14 +105,14 @@ pub mod ser {
107105
}
108106

109107
impl Error {
110-
#[rune::function(vm_result, protocol = STRING_DISPLAY)]
111-
pub(crate) fn display(&self, f: &mut Formatter) {
112-
vm_write!(f, "{}", self.error);
108+
#[rune::function(protocol = STRING_DISPLAY)]
109+
pub(crate) fn display(&self, f: &mut Formatter) -> VmResult<()> {
110+
vm_write!(f, "{}", self.error)
113111
}
114112

115-
#[rune::function(vm_result, protocol = STRING_DEBUG)]
116-
pub(crate) fn debug(&self, f: &mut Formatter) {
117-
vm_write!(f, "{:?}", self.error);
113+
#[rune::function(protocol = STRING_DEBUG)]
114+
pub(crate) fn debug(&self, f: &mut Formatter) -> VmResult<()> {
115+
vm_write!(f, "{:?}", self.error)
118116
}
119117
}
120118

crates/rune/src/any.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use core::any;
22

3+
use crate::alloc::String;
34
use crate::compile::Named;
45
use crate::runtime::{AnyTypeInfo, TypeHash};
56

@@ -124,3 +125,5 @@ cfg_std! {
124125
}
125126

126127
crate::__internal_impl_any!(::std::error, anyhow::Error);
128+
129+
impl Any for String {}

crates/rune/src/compile/error.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::macros::{SyntheticId, SyntheticKind};
2020
use crate::parse::{Expectation, IntoExpectation, LexerMode};
2121
use crate::runtime::debug::DebugSignature;
2222
use crate::runtime::unit::EncodeError;
23-
use crate::runtime::{AccessError, RuntimeError, TypeInfo, TypeOf, VmError};
23+
use crate::runtime::{AccessError, AnyObjError, RuntimeError, TypeInfo, TypeOf, VmError};
2424
#[cfg(feature = "std")]
2525
use crate::source;
2626
use crate::{Hash, Item, ItemBuf, SourceId};
@@ -1307,6 +1307,13 @@ impl From<RuntimeError> for ErrorKind {
13071307
}
13081308
}
13091309

1310+
impl From<AnyObjError> for ErrorKind {
1311+
#[inline]
1312+
fn from(error: AnyObjError) -> Self {
1313+
Self::from(RuntimeError::from(error))
1314+
}
1315+
}
1316+
13101317
impl From<EncodeError> for ErrorKind {
13111318
#[inline]
13121319
fn from(error: EncodeError) -> Self {

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use crate::ast::{Span, Spanned};
77
use crate::compile::ir::{self};
88
use crate::compile::{self, WithSpan};
99
use crate::query::Used;
10-
use crate::runtime::{Inline, Mutable, Object, OwnedTuple, Value, ValueBorrowRef};
10+
use crate::runtime::{Inline, Object, OwnedTuple, Value, ValueBorrowRef};
11+
use crate::TypeHash;
1112

1213
/// The outcome of a constant evaluation.
1314
pub enum EvalOutcome {
@@ -139,18 +140,22 @@ fn eval_ir_binary(
139140

140141
return Ok(Value::from(out));
141142
}
142-
(ValueBorrowRef::Mutable(a), ValueBorrowRef::Mutable(b)) => {
143-
let out = 'out: {
144-
if let (Mutable::String(a), Mutable::String(b)) = (&*a, &*b) {
143+
(ValueBorrowRef::Any(a), ValueBorrowRef::Any(b)) => {
144+
let value = 'out: {
145+
if let (String::HASH, String::HASH) = (a.type_hash(), b.type_hash()) {
146+
let a = a.borrow_ref::<String>().with_span(span)?;
147+
let b = b.borrow_ref::<String>().with_span(span)?;
148+
145149
if let ir::IrBinaryOp::Add = ir.op {
146-
break 'out Mutable::String(add_strings(a, b).with_span(span)?);
150+
let string = add_strings(&a, &b).with_span(span)?;
151+
break 'out Value::new(string).with_span(span)?;
147152
}
148153
}
149154

150155
return Err(EvalOutcome::not_const(span));
151156
};
152157

153-
return Ok(Value::try_from(out).with_span(span)?);
158+
return Ok(value);
154159
}
155160
_ => (),
156161
}
@@ -366,15 +371,16 @@ fn eval_ir_template(
366371
return Err(EvalOutcome::not_const(ir));
367372
}
368373
},
369-
ValueBorrowRef::Mutable(value) => match &*value {
370-
Mutable::String(s) => {
371-
buf.try_push_str(s)?;
374+
ValueBorrowRef::Any(value) => match value.type_hash() {
375+
String::HASH => {
376+
let s = value.borrow_ref::<String>().with_span(ir)?;
377+
buf.try_push_str(&s)?;
372378
}
373379
_ => {
374380
return Err(EvalOutcome::not_const(ir));
375381
}
376382
},
377-
ValueBorrowRef::Any(..) => {
383+
ValueBorrowRef::Mutable(..) => {
378384
return Err(EvalOutcome::not_const(ir));
379385
}
380386
}

crates/rune/src/exported_macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ macro_rules! vm_panic {
5353
macro_rules! vm_write {
5454
($($tt:tt)*) => {
5555
match core::write!($($tt)*) {
56-
Ok(()) => (),
56+
Ok(()) => $crate::runtime::VmResult::Ok(()),
5757
Err(err) => {
5858
return $crate::runtime::VmResult::Err($crate::runtime::VmError::from(err));
5959
}

crates/rune/src/macros/format_args.rs

Copy file name to clipboard
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ use crate::ast::{self, Span, Spanned};
77
use crate::compile::{self, WithSpan};
88
use crate::macros::{quote, MacroContext, Quote, ToTokens, TokenStream};
99
use crate::parse::{Parse, Parser, Peek, Peeker};
10-
use crate::runtime::format;
11-
use crate::runtime::{Inline, Mutable, OwnedValue};
10+
use crate::runtime::{format, Inline};
1211

1312
/// A format specification: A format string followed by arguments to be
1413
/// formatted in accordance with that string.
@@ -49,14 +48,7 @@ impl FormatArgs {
4948
}
5049
}
5150

52-
let format = format.take_value().with_span(&self.format)?;
53-
54-
let OwnedValue::Mutable(Mutable::String(format)) = format else {
55-
return Err(compile::Error::msg(
56-
&self.format,
57-
"format argument must be a string",
58-
));
59-
};
51+
let format = format.into_any::<String>().with_span(&self.format)?;
6052

6153
let mut unused_pos = (0..pos.len()).try_collect::<BTreeSet<_>>()?;
6254
let mut unused_named = named

0 commit comments

Comments
 (0)
0