Skip to content

Commit

Permalink
rune: Rename and deprecate types
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Nov 3, 2024
1 parent 6123430 commit 9c69ad8
Show file tree
Hide file tree
Showing 19 changed files with 403 additions and 428 deletions.
6 changes: 3 additions & 3 deletions crates/rune/src/cli/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::cli::{
use crate::compile::FileSourceLoader;
use crate::doc::{TestKind, TestParams};
use crate::modules::capture_io::CaptureIo;
use crate::runtime::{RefRepr, Value, Vm, VmError, VmResult};
use crate::runtime::{ReprRef, Value, Vm, VmError, VmResult};
use crate::{Diagnostics, Hash, Item, ItemBuf, Source, Sources, TypeHash, Unit};

mod cli {
Expand Down Expand Up @@ -534,8 +534,8 @@ impl TestCase {
capture_io.drain_into(&mut self.output)?;

self.outcome = match result {
VmResult::Ok(v) => match v.as_ref_repr()? {
RefRepr::Any(value) => match value.type_hash() {
VmResult::Ok(v) => match v.as_ref()? {
ReprRef::Any(value) => match value.type_hash() {
Result::<Value, Value>::HASH => {
let result = value.borrow_ref::<Result<Value, Value>>()?;

Expand Down
18 changes: 9 additions & 9 deletions crates/rune/src/compile/ir/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::ast::{Span, Spanned};
use crate::compile::ir::{self};
use crate::compile::{self, WithSpan};
use crate::query::Used;
use crate::runtime::{BorrowRefRepr, Inline, Object, OwnedTuple, Value};
use crate::runtime::{Inline, Object, OwnedTuple, ReprRef, Value};
use crate::TypeHash;

/// The outcome of a constant evaluation.
Expand Down Expand Up @@ -72,11 +72,11 @@ fn eval_ir_binary(
let a = eval_ir(&ir.lhs, interp, used)?;
let b = eval_ir(&ir.rhs, interp, used)?;

let a = a.borrow_ref_repr().with_span(ir)?;
let b = b.borrow_ref_repr().with_span(ir)?;
let a = a.as_ref().with_span(ir)?;
let b = b.as_ref().with_span(ir)?;

match (a, b) {
(BorrowRefRepr::Inline(a), BorrowRefRepr::Inline(b)) => {
(ReprRef::Inline(a), ReprRef::Inline(b)) => {
let out = 'out: {
match (a, b) {
(Inline::Signed(a), Inline::Signed(b)) => match ir.op {
Expand Down Expand Up @@ -140,7 +140,7 @@ fn eval_ir_binary(

return Ok(Value::from(out));
}
(BorrowRefRepr::Any(a), BorrowRefRepr::Any(b)) => {
(ReprRef::Any(a), ReprRef::Any(b)) => {
let value = 'out: {
if let (String::HASH, String::HASH) = (a.type_hash(), b.type_hash()) {
let a = a.borrow_ref::<String>().with_span(span)?;
Expand Down Expand Up @@ -353,10 +353,10 @@ fn eval_ir_template(
}
ir::IrTemplateComponent::Ir(ir) => {
let const_value = eval_ir(ir, interp, used)?;
let value = const_value.borrow_ref_repr().with_span(ir)?;
let value = const_value.as_ref().with_span(ir)?;

match value {
BorrowRefRepr::Inline(value) => match value {
ReprRef::Inline(value) => match value {
Inline::Signed(integer) => {
write!(buf, "{integer}")?;
}
Expand All @@ -371,7 +371,7 @@ fn eval_ir_template(
return Err(EvalOutcome::not_const(ir));
}
},
BorrowRefRepr::Any(value) => match value.type_hash() {
ReprRef::Any(value) => match value.type_hash() {
String::HASH => {
let s = value.borrow_ref::<String>().with_span(ir)?;
buf.try_push_str(&s)?;
Expand All @@ -380,7 +380,7 @@ fn eval_ir_template(
return Err(EvalOutcome::not_const(ir));
}
},
BorrowRefRepr::Mutable(..) => {
ReprRef::Mutable(..) => {
return Err(EvalOutcome::not_const(ir));
}
}
Expand Down
24 changes: 12 additions & 12 deletions crates/rune/src/compile/ir/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::compile::meta;
use crate::compile::{self, IrErrorKind, ItemId, ModId, WithSpan};
use crate::hir;
use crate::query::{Query, Used};
use crate::runtime::{self, ConstValue, Object, OwnedTuple, RefRepr, Value};
use crate::runtime::{self, ConstValue, Object, OwnedTuple, ReprRef, Value};
use crate::TypeHash;

/// The interpreter that executed [Ir][crate::ir::Ir].
Expand Down Expand Up @@ -200,22 +200,22 @@ impl ir::Scopes {
}
ir::IrTargetKind::Index(target, index) => {
let value = self.get_target(target)?;
let target = value.as_ref_repr().with_span(ir_target)?;
let target = value.as_ref().with_span(ir_target)?;

match target {
RefRepr::Inline(value) => {
ReprRef::Inline(value) => {
return Err(compile::Error::expected_type::<OwnedTuple>(
ir_target,
value.type_info(),
));
}
RefRepr::Mutable(value) => {
ReprRef::Mutable(value) => {
return Err(compile::Error::expected_type::<OwnedTuple>(
ir_target,
value.borrow_ref().with_span(ir_target)?.type_info(),
));
}
RefRepr::Any(value) => match value.type_hash() {
ReprRef::Any(value) => match value.type_hash() {
runtime::Vec::HASH => {
let vec = value.borrow_ref::<runtime::Vec>().with_span(ir_target)?;

Expand Down Expand Up @@ -270,22 +270,22 @@ impl ir::Scopes {
ir::IrTargetKind::Index(target, index) => {
let target = self.get_target(target)?;

match target.as_ref_repr().with_span(ir_target)? {
RefRepr::Inline(value) => {
match target.as_ref().with_span(ir_target)? {
ReprRef::Inline(value) => {
return Err(compile::Error::expected_type::<OwnedTuple>(
ir_target,
value.type_info(),
));
}
RefRepr::Mutable(current) => {
ReprRef::Mutable(current) => {
let mutable = current.borrow_mut().with_span(ir_target)?;

return Err(compile::Error::expected_type::<OwnedTuple>(
ir_target,
mutable.type_info(),
));
}
RefRepr::Any(any) => match any.type_hash() {
ReprRef::Any(any) => match any.type_hash() {
runtime::Vec::HASH => {
let mut vec = any.borrow_mut::<runtime::Vec>().with_span(ir_target)?;

Expand Down Expand Up @@ -347,16 +347,16 @@ impl ir::Scopes {
ir::IrTargetKind::Index(target, index) => {
let current = self.get_target(target)?;

match current.as_ref_repr().with_span(ir_target)? {
RefRepr::Mutable(value) => {
match current.as_ref().with_span(ir_target)? {
ReprRef::Mutable(value) => {
let value = value.borrow_ref().with_span(ir_target)?;

Err(compile::Error::expected_type::<OwnedTuple>(
ir_target,
value.type_info(),
))
}
RefRepr::Any(value) => match value.type_hash() {
ReprRef::Any(value) => match value.type_hash() {
runtime::Vec::HASH => {
let mut vec =
value.borrow_mut::<runtime::Vec>().with_span(ir_target)?;
Expand Down
18 changes: 9 additions & 9 deletions crates/rune/src/modules/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate as rune;
use crate::alloc::Vec;
use crate::runtime::{self, Future, Inline, RefRepr, SelectFuture, Value, VmErrorKind, VmResult};
use crate::runtime::{self, Future, Inline, ReprRef, SelectFuture, Value, VmErrorKind, VmResult};
use crate::{ContextError, Module, TypeHash};

/// Asynchronous computations.
Expand All @@ -25,20 +25,20 @@ where
let mut results = vm_try!(Vec::try_with_capacity(len));

for (index, value) in values.into_iter().enumerate() {
match vm_try!(value.as_ref_repr()) {
RefRepr::Inline(value) => {
match vm_try!(value.as_ref()) {
ReprRef::Inline(value) => {
return VmResult::err([
VmErrorKind::expected::<Future>(value.type_info()),
VmErrorKind::bad_argument(index),
]);
}
RefRepr::Mutable(value) => {
ReprRef::Mutable(value) => {
return VmResult::err([
VmErrorKind::expected::<Future>(vm_try!(value.borrow_ref()).type_info()),
VmErrorKind::bad_argument(index),
]);
}
RefRepr::Any(value) => match value.type_hash() {
ReprRef::Any(value) => match value.type_hash() {
Future::HASH => {
let future = vm_try!(Value::from(value.clone()).into_future());
futures.push(SelectFuture::new(index, future));
Expand Down Expand Up @@ -98,19 +98,19 @@ where
/// ```
#[rune::function(keep)]
async fn join(value: Value) -> VmResult<Value> {
match vm_try!(value.as_ref_repr()) {
RefRepr::Inline(value) => match value {
match vm_try!(value.as_ref()) {
ReprRef::Inline(value) => match value {
Inline::Unit => VmResult::Ok(Value::unit()),
value => VmResult::err([
VmErrorKind::bad_argument(0),
VmErrorKind::expected::<runtime::Vec>(value.type_info()),
]),
},
RefRepr::Mutable(value) => VmResult::err([
ReprRef::Mutable(value) => VmResult::err([
VmErrorKind::bad_argument(0),
VmErrorKind::expected::<runtime::Vec>(vm_try!(value.borrow_ref()).type_info()),
]),
RefRepr::Any(value) => match value.type_hash() {
ReprRef::Any(value) => match value.type_hash() {
runtime::Vec::HASH => {
let vec = vm_try!(value.borrow_ref::<runtime::Vec>());
let result = try_join_impl(vec.iter(), vec.len(), |vec| {
Expand Down
16 changes: 9 additions & 7 deletions crates/rune/src/modules/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::modules::collections::VecDeque;
use crate::modules::collections::{HashMap, HashSet};
use crate::runtime::range::RangeIter;
use crate::runtime::{
BorrowRefRepr, FromValue, Function, Inline, InstAddress, Object, Output, OwnedTuple, Protocol,
FromValue, Function, Inline, InstAddress, Object, Output, OwnedTuple, Protocol, ReprRef,
TypeHash, Value, Vec, VmErrorKind, VmResult,
};
use crate::shared::Caller;
Expand Down Expand Up @@ -473,17 +473,19 @@ pub fn module() -> Result<Module, ContextError> {
let mut string = String::new();

while let Some(value) = vm_try!(next.call((iter.clone(),))) {
match vm_try!(value.borrow_ref_repr()) {
BorrowRefRepr::Inline(Inline::Char(c)) => {
match vm_try!(value.as_ref()) {
ReprRef::Inline(Inline::Char(c)) => {
vm_try!(string.try_push(*c));
}
BorrowRefRepr::Inline(value) => {
ReprRef::Inline(value) => {
return VmResult::expected::<String>(value.type_info());
}
BorrowRefRepr::Mutable(value) => {
return VmResult::expected::<String>(value.type_info());
ReprRef::Mutable(value) => {
return VmResult::expected::<String>(
vm_try!(value.borrow_ref()).type_info(),
);
}
BorrowRefRepr::Any(value) => match value.type_hash() {
ReprRef::Any(value) => match value.type_hash() {
String::HASH => {
let s = vm_try!(value.borrow_ref::<String>());
vm_try!(string.try_push_str(&s));
Expand Down
22 changes: 11 additions & 11 deletions crates/rune/src/modules/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::alloc::{String, Vec};
use crate::compile::Named;
use crate::runtime::{
Bytes, Formatter, FromValue, Function, Hasher, Inline, MaybeTypeOf, Panic, Range, RangeFrom,
RangeFull, RangeInclusive, RangeTo, RangeToInclusive, Ref, RefRepr, ToValue, TypeOf, Value,
RangeFull, RangeInclusive, RangeTo, RangeToInclusive, Ref, ReprRef, ToValue, TypeOf, Value,
VmErrorKind, VmResult,
};
use crate::{Any, ContextError, Module, TypeHash};
Expand Down Expand Up @@ -888,19 +888,19 @@ fn shrink_to_fit(s: &mut String) -> VmResult<()> {
/// [`split_whitespace`]: str::split_whitespace
#[rune::function(instance, deprecated = "Use String::split instead")]
fn split(this: Ref<str>, value: Value) -> VmResult<Value> {
match vm_try!(value.as_ref_repr()) {
RefRepr::Inline(Inline::Char(c)) => {
match vm_try!(value.as_ref()) {
ReprRef::Inline(Inline::Char(c)) => {
VmResult::Ok(vm_try!(rune::to_value(Split::new(this, *c))))
}
RefRepr::Inline(value) => VmResult::err([
ReprRef::Inline(value) => VmResult::err([
VmErrorKind::expected::<String>(value.type_info()),
VmErrorKind::bad_argument(0),
]),
RefRepr::Mutable(value) => VmResult::err([
ReprRef::Mutable(value) => VmResult::err([
VmErrorKind::expected::<String>(vm_try!(value.borrow_ref()).type_info()),
VmErrorKind::bad_argument(0),
]),
RefRepr::Any(value) => match value.type_hash() {
ReprRef::Any(value) => match value.type_hash() {
String::HASH => {
let s = vm_try!(value.borrow_ref::<String>());

Expand Down Expand Up @@ -937,21 +937,21 @@ fn split(this: Ref<str>, value: Value) -> VmResult<Value> {
/// ```
#[rune::function(instance)]
fn split_once(this: &str, value: Value) -> VmResult<Option<(String, String)>> {
let outcome = match vm_try!(value.as_ref_repr()) {
RefRepr::Inline(Inline::Char(pat)) => this.split_once(*pat),
RefRepr::Inline(value) => {
let outcome = match vm_try!(value.as_ref()) {
ReprRef::Inline(Inline::Char(pat)) => this.split_once(*pat),
ReprRef::Inline(value) => {
return VmResult::err([
VmErrorKind::expected::<String>(value.type_info()),
VmErrorKind::bad_argument(0),
]);
}
RefRepr::Mutable(value) => {
ReprRef::Mutable(value) => {
return VmResult::err([
VmErrorKind::expected::<String>(vm_try!(value.borrow_ref()).type_info()),
VmErrorKind::bad_argument(0),
]);
}
RefRepr::Any(value) => match value.type_hash() {
ReprRef::Any(value) => match value.type_hash() {
String::HASH => {
let s = vm_try!(value.borrow_ref::<String>());
this.split_once(s.as_str())
Expand Down
22 changes: 10 additions & 12 deletions crates/rune/src/runtime/const_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use crate::runtime;
use crate::{Hash, TypeHash};

use super::{
BorrowRefRepr, Bytes, FromValue, Inline, Object, OwnedTuple, ToValue, Tuple, Type, TypeInfo,
Value, VmErrorKind,
Bytes, FromValue, Inline, Object, OwnedTuple, ReprRef, ToValue, Tuple, Type, TypeInfo, Value,
VmErrorKind,
};

/// Derive for the [`ToConstValue`](trait@ToConstValue) trait.
Expand Down Expand Up @@ -234,16 +234,14 @@ impl ConstValue {

/// Construct a constant value from a reference to a value..
pub(crate) fn from_value_ref(value: &Value) -> Result<ConstValue, RuntimeError> {
let inner = match value.borrow_ref_repr()? {
BorrowRefRepr::Inline(value) => ConstValueKind::Inline(*value),
BorrowRefRepr::Mutable(value) => match &*value {
value => {
return Err(RuntimeError::from(VmErrorKind::ConstNotSupported {
actual: value.type_info(),
}))
}
},
BorrowRefRepr::Any(value) => match value.type_hash() {
let inner = match value.as_ref()? {
ReprRef::Inline(value) => ConstValueKind::Inline(*value),
ReprRef::Mutable(value) => {
return Err(RuntimeError::from(VmErrorKind::ConstNotSupported {
actual: value.borrow_ref()?.type_info(),
}));
}
ReprRef::Any(value) => match value.type_hash() {
Option::<Value>::HASH => {
let option = value.borrow_ref::<Option<Value>>()?;

Expand Down
Loading

0 comments on commit 9c69ad8

Please sign in to comment.