Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Mutable::Result and Mutable::Option #876

Merged
merged 5 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 31 additions & 26 deletions crates/rune-macros/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,32 +669,7 @@ where
let type_parameters =
quote!(#hash::parameters([#(<#generic_names as #type_hash_t>::HASH),*]));

let impl_type_of = is_generic.is_none().then(|| quote! {
#[automatically_derived]
#(#attrs)*
impl #impl_generics #type_hash_t for #ident #type_generics #where_clause {
const HASH: #hash = #make_hash;
}

#[automatically_derived]
#(#attrs)*
impl #impl_generics #type_of for #ident #type_generics #where_clause {
const PARAMETERS: #hash = #type_parameters;
const STATIC_TYPE_INFO: #any_type_info = <Self as #any_t>::ANY_TYPE_INFO;
}

#[automatically_derived]
#(#attrs)*
impl #impl_generics #maybe_type_of for #ident #type_generics #where_clause {
#[inline]
fn maybe_type_of() -> #alloc::Result<#meta::DocType> {
#meta::DocType::with_generics(
<Self as #type_hash_t>::HASH,
[#(<#generic_names as #maybe_type_of>::maybe_type_of()?),*]
)
}
}

let to_value_impl = quote! {
#[automatically_derived]
#(#attrs)*
impl #impl_generics #unsafe_to_ref for #ident #type_generics #where_clause {
Expand Down Expand Up @@ -738,6 +713,35 @@ where
#vm_result::Ok((shared, guard))
}
}
};

let impl_type_of = is_generic.is_none().then(|| {
quote! {
#[automatically_derived]
#(#attrs)*
impl #impl_generics #type_hash_t for #ident #type_generics #where_clause {
const HASH: #hash = #make_hash;
}

#[automatically_derived]
#(#attrs)*
impl #impl_generics #type_of for #ident #type_generics #where_clause {
const PARAMETERS: #hash = #type_parameters;
const STATIC_TYPE_INFO: #any_type_info = <Self as #any_t>::ANY_TYPE_INFO;
}

#[automatically_derived]
#(#attrs)*
impl #impl_generics #maybe_type_of for #ident #type_generics #where_clause {
#[inline]
fn maybe_type_of() -> #alloc::Result<#meta::DocType> {
#meta::DocType::with_generics(
<Self as #type_hash_t>::HASH,
[#(<#generic_names as #maybe_type_of>::maybe_type_of()?),*]
)
}
}
}
});

let impl_any = quote! {
Expand All @@ -759,6 +763,7 @@ where
quote! {
#install_with
#impl_named
#to_value_impl
#impl_type_of
#impl_any
#impl_non_generic
Expand Down
31 changes: 21 additions & 10 deletions crates/rune/src/cli/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use crate::cli::{
use crate::compile::FileSourceLoader;
use crate::doc::{TestKind, TestParams};
use crate::modules::capture_io::CaptureIo;
use crate::runtime::{Mutable, OwnedRepr, Value, Vm, VmError, VmResult};
use crate::{Diagnostics, Hash, Item, ItemBuf, Source, Sources, Unit};
use crate::runtime::{ReprRef, Value, Vm, VmError, VmResult};
use crate::{Diagnostics, Hash, Item, ItemBuf, Source, Sources, TypeHash, Unit};

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

self.outcome = match result {
VmResult::Ok(v) => match v.take_repr()? {
OwnedRepr::Mutable(Mutable::Result(result)) => match result {
Ok(..) => Outcome::Ok,
Err(error) => Outcome::Err(error),
},
OwnedRepr::Mutable(Mutable::Option(option)) => match option {
Some(..) => Outcome::Ok,
None => Outcome::None,
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>>()?;

match &*result {
Ok(..) => Outcome::Ok,
Err(error) => Outcome::Err(error.clone()),
}
}
Option::<Value>::HASH => {
let option = value.borrow_ref::<Option<Value>>()?;

match &*option {
Some(..) => Outcome::Ok,
None => Outcome::None,
}
}
_ => Outcome::Ok,
},
_ => Outcome::Ok,
},
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
42 changes: 0 additions & 42 deletions crates/rune/src/internal_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,48 +87,6 @@ macro_rules! cfg_std {
}
}

macro_rules! from_value_ref {
($ty:ty, $into_ref:ident, $into_mut:ident) => {
impl $crate::runtime::UnsafeToRef for $ty {
type Guard = $crate::runtime::RawAnyGuard;

unsafe fn unsafe_to_ref<'a>(
value: $crate::runtime::Value,
) -> $crate::runtime::VmResult<(&'a Self, Self::Guard)> {
let value = vm_try!(value.$into_ref());
let (value, guard) = $crate::runtime::Ref::into_raw(value);
$crate::runtime::VmResult::Ok((value.as_ref(), guard))
}
}

impl $crate::runtime::UnsafeToMut for $ty {
type Guard = $crate::runtime::RawAnyGuard;

unsafe fn unsafe_to_mut<'a>(
value: $crate::runtime::Value,
) -> $crate::runtime::VmResult<(&'a mut Self, Self::Guard)> {
let value = vm_try!(value.$into_mut());
let (mut value, guard) = $crate::runtime::Mut::into_raw(value);
$crate::runtime::VmResult::Ok((value.as_mut(), guard))
}
}

impl $crate::runtime::FromValue for $crate::runtime::Ref<$ty> {
fn from_value(value: Value) -> Result<Self, $crate::runtime::RuntimeError> {
let value = value.$into_ref()?;
Ok(value)
}
}

impl $crate::runtime::FromValue for $crate::runtime::Mut<$ty> {
fn from_value(value: Value) -> Result<Self, $crate::runtime::RuntimeError> {
let value = value.$into_mut()?;
Ok(value)
}
}
};
}

macro_rules! impl_builtin_type_of {
(
$(
Expand Down
8 changes: 2 additions & 6 deletions crates/rune/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ pub mod query;
pub mod runtime;
#[doc(inline)]
pub use self::runtime::{
from_const_value, from_value, to_const_value, to_value, FromValue, ToConstValue, ToValue,
TypeHash, Unit, Value, Vm,
from_const_value, from_value, to_const_value, to_value, FromValue, Mut, Ref, ToConstValue,
ToValue, TypeHash, Unit, Value, Vm,
};

mod shared;
Expand Down Expand Up @@ -682,7 +682,6 @@ mod tests;
rune_macros::binding! {
#[generic]
impl ::std::option::Option for Option<Value>;

#[generic]
impl ::std::result::Result for Result<Value, Value>;

Expand Down Expand Up @@ -763,6 +762,3 @@ impl_builtin_type_of! {

impl ::std::any::Type, crate::runtime::Type;
}

from_value_ref!(Result<Value, Value>, into_result_ref, into_result_mut);
from_value_ref!(Option<Value>, into_option_ref, into_option_mut);
2 changes: 1 addition & 1 deletion crates/rune/src/macros/format_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl FormatArgs {
}
}

let format = format.into_any::<String>().with_span(&self.format)?;
let format = format.downcast::<String>().with_span(&self.format)?;

let mut unused_pos = (0..pos.len()).try_collect::<BTreeSet<_>>()?;
let mut unused_named = named
Expand Down
9 changes: 3 additions & 6 deletions crates/rune/src/module/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,8 +733,7 @@ impl Module {
/// # Examples
///
/// ```
/// use rune::{Module, ContextError};
/// use rune::runtime::Ref;
/// use rune::{ContextError, Module, Ref};
///
/// /// This is a pretty neat function.
/// #[rune::function]
Expand All @@ -759,8 +758,7 @@ impl Module {
/// Registering instance functions:
///
/// ```
/// use rune::{Any, Module};
/// use rune::runtime::Ref;
/// use rune::{Any, Module, Ref};
///
/// #[derive(Any)]
/// struct MyBytes {
Expand Down Expand Up @@ -1063,8 +1061,7 @@ impl Module {
/// use std::sync::atomic::AtomicU32;
/// use std::sync::Arc;
///
/// use rune::{Any, Module};
/// use rune::runtime::Ref;
/// use rune::{Any, Module, Ref};
///
/// #[derive(Clone, Debug, Any)]
/// struct Client {
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/modules/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ fn cmp(this: &[u8], rhs: &[u8]) -> Ordering {
this.cmp(rhs)
}

/// Hash the string.
/// Hash the byte array.
///
/// # Examples
///
Expand Down
Loading