Skip to content

Commit

Permalink
Clean up public API
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Feb 16, 2024
1 parent 066470a commit 5ee7e13
Show file tree
Hide file tree
Showing 17 changed files with 346 additions and 212 deletions.
9 changes: 8 additions & 1 deletion crates/rune/src/compile/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::parse::{Expectation, IntoExpectation, LexerMode};
use crate::query::MissingId;
use crate::runtime::debug::DebugSignature;
use crate::runtime::unit::EncodeError;
use crate::runtime::{AccessError, TypeInfo, TypeOf, ValueKind, VmError};
use crate::runtime::{AccessError, RuntimeError, TypeInfo, TypeOf, ValueKind, VmError};
#[cfg(feature = "std")]
use crate::source;
use crate::{Hash, SourceId};
Expand Down Expand Up @@ -1094,6 +1094,13 @@ impl From<VmError> for ErrorKind {
}
}

impl From<RuntimeError> for ErrorKind {
#[inline]
fn from(error: RuntimeError) -> Self {
ErrorKind::VmError(VmError::new(error.into_vm_error_kind()))
}
}

impl From<EncodeError> for ErrorKind {
#[inline]
fn from(source: EncodeError) -> Self {
Expand Down
6 changes: 3 additions & 3 deletions crates/rune/src/compile/ir/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ fn eval_ir_branches(

let value = eval_ir_condition(ir_condition, interp, used)?;

let output = if value.as_bool().into_result().with_span(ir_condition)? {
let output = if value.as_bool().with_span(ir_condition)? {
Some(eval_ir_scope(branch, interp, used)?)
} else {
None
Expand Down Expand Up @@ -195,7 +195,7 @@ fn eval_ir_condition(
let value = match ir {
ir::IrCondition::Ir(ir) => {
let value = eval_ir(ir, interp, used)?;
value.as_bool().into_result().with_span(ir)?
value.as_bool().with_span(ir)?
}
ir::IrCondition::Let(ir_let) => {
let value = eval_ir(&ir_let.ir, interp, used)?;
Expand Down Expand Up @@ -233,7 +233,7 @@ fn eval_ir_loop(

let value = eval_ir_condition(condition, interp, used)?;

if !value.as_bool().into_result().with_span(condition)? {
if !value.as_bool().with_span(condition)? {
break None;
}
}
Expand Down
16 changes: 8 additions & 8 deletions crates/rune/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
mod tests;

mod access;
pub(crate) use self::access::Access;
pub use self::access::{
AccessError, BorrowMut, BorrowRef, NotAccessibleMut, NotAccessibleRef, RawAccessGuard,
};
pub(crate) use self::access::{Access, AccessErrorKind};
pub use self::access::{AccessError, BorrowMut, BorrowRef, RawAccessGuard};

mod any_obj;
pub use self::any_obj::{AnyObj, AnyObjError, AnyObjVtable};
Expand Down Expand Up @@ -169,14 +167,16 @@ mod vm_error;
#[cfg(feature = "emit")]
pub(crate) use self::vm_error::VmErrorAt;
pub(crate) use self::vm_error::VmErrorKind;
pub use self::vm_error::{try_result, TryFromResult, VmError, VmIntegerRepr, VmResult};
pub use self::vm_error::{
try_result, RuntimeError, TryFromResult, VmError, VmIntegerRepr, VmResult,
};

mod vm_execution;
pub use self::vm_execution::{ExecutionState, VmExecution, VmSendExecution};
pub(crate) use self::vm_execution::ExecutionState;
pub use self::vm_execution::{VmExecution, VmSendExecution};

mod vm_halt;
pub(crate) use self::vm_halt::VmHalt;
pub use self::vm_halt::VmHaltInfo;
pub(crate) use self::vm_halt::{VmHalt, VmHaltInfo};

mod fmt;
pub use self::fmt::Formatter;
Expand Down
97 changes: 58 additions & 39 deletions crates/rune/src/runtime/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,78 +18,97 @@ const MOVED: isize = isize::MAX;
const MAX_USES: isize = isize::MIN;

/// An error raised while downcasting.
#[derive(Debug)]
#[derive(Debug, PartialEq)]
#[allow(missing_docs)]
#[non_exhaustive]
pub enum AccessError {
UnexpectedType { expected: RawStr, actual: RawStr },
NotAccessibleRef { error: NotAccessibleRef },
NotAccessibleMut { error: NotAccessibleMut },
NotAccessibleTake { error: NotAccessibleTake },
AnyObjError { error: AnyObjError },
pub struct AccessError {
kind: AccessErrorKind,
}

cfg_std! {
impl std::error::Error for AccessError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
AccessError::NotAccessibleRef { error, .. } => Some(error),
AccessError::NotAccessibleMut { error, .. } => Some(error),
AccessError::NotAccessibleTake { error, .. } => Some(error),
AccessError::AnyObjError { error, .. } => Some(error),
_ => None,
}
}
impl AccessError {
#[inline]
pub(crate) fn new(kind: AccessErrorKind) -> Self {
Self { kind }
}
}

impl fmt::Display for AccessError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
AccessError::UnexpectedType { expected, actual } => write!(
match &self.kind {
AccessErrorKind::UnexpectedType { expected, actual } => write!(
f,
"Expected data of type `{expected}`, but found `{actual}`",
),
AccessError::NotAccessibleRef { error } => error.fmt(f),
AccessError::NotAccessibleMut { error } => error.fmt(f),
AccessError::NotAccessibleTake { error } => error.fmt(f),
AccessError::AnyObjError { error } => error.fmt(f),
AccessErrorKind::NotAccessibleRef { error } => error.fmt(f),
AccessErrorKind::NotAccessibleMut { error } => error.fmt(f),
AccessErrorKind::NotAccessibleTake { error } => error.fmt(f),
AccessErrorKind::AnyObjError { error } => error.fmt(f),
}
}
}

cfg_std! {
impl std::error::Error for AccessError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self.kind {
AccessErrorKind::NotAccessibleRef { error, .. } => Some(error),
AccessErrorKind::NotAccessibleMut { error, .. } => Some(error),
AccessErrorKind::NotAccessibleTake { error, .. } => Some(error),
AccessErrorKind::AnyObjError { error, .. } => Some(error),
_ => None,
}
}
}
}

impl From<NotAccessibleRef> for AccessError {
#[inline]
fn from(error: NotAccessibleRef) -> Self {
AccessError::NotAccessibleRef { error }
AccessError::new(AccessErrorKind::NotAccessibleRef { error })
}
}

impl From<NotAccessibleMut> for AccessError {
#[inline]
fn from(error: NotAccessibleMut) -> Self {
AccessError::NotAccessibleMut { error }
AccessError::new(AccessErrorKind::NotAccessibleMut { error })
}
}

impl From<NotAccessibleTake> for AccessError {
#[inline]
fn from(error: NotAccessibleTake) -> Self {
AccessError::NotAccessibleTake { error }
AccessError::new(AccessErrorKind::NotAccessibleTake { error })
}
}

impl From<AnyObjError> for AccessError {
#[inline]
fn from(source: AnyObjError) -> Self {
AccessError::AnyObjError { error: source }
AccessError::new(AccessErrorKind::AnyObjError { error: source })
}
}

impl From<AccessErrorKind> for AccessError {
#[inline]
fn from(kind: AccessErrorKind) -> Self {
AccessError::new(kind)
}
}

#[derive(Debug, PartialEq)]
pub(crate) enum AccessErrorKind {
UnexpectedType { expected: RawStr, actual: RawStr },
NotAccessibleRef { error: NotAccessibleRef },
NotAccessibleMut { error: NotAccessibleMut },
NotAccessibleTake { error: NotAccessibleTake },
AnyObjError { error: AnyObjError },
}

/// Error raised when tried to access for shared access but it was not
/// accessible.
#[derive(Debug)]
pub struct NotAccessibleRef(Snapshot);
#[derive(Debug, PartialEq)]
pub(crate) struct NotAccessibleRef(Snapshot);

impl fmt::Display for NotAccessibleRef {
#[inline]
Expand All @@ -104,8 +123,8 @@ cfg_std! {

/// Error raised when tried to access for exclusive access but it was not
/// accessible.
#[derive(Debug)]
pub struct NotAccessibleMut(Snapshot);
#[derive(Debug, PartialEq)]
pub(crate) struct NotAccessibleMut(Snapshot);

impl fmt::Display for NotAccessibleMut {
#[inline]
Expand All @@ -122,8 +141,8 @@ cfg_std! {
///
/// This requires exclusive access, but it's a scenario we structure separately
/// for diagnostics purposes.
#[derive(Debug)]
pub struct NotAccessibleTake(Snapshot);
#[derive(Debug, PartialEq)]
pub(crate) struct NotAccessibleTake(Snapshot);

impl fmt::Display for NotAccessibleTake {
#[inline]
Expand All @@ -138,7 +157,7 @@ cfg_std! {

/// Snapshot that can be used to indicate how the value was being accessed at
/// the time of an error.
#[derive(Debug)]
#[derive(Debug, PartialEq)]
#[repr(transparent)]
struct Snapshot(isize);

Expand Down Expand Up @@ -345,7 +364,7 @@ impl<'a, T: ?Sized> BorrowRef<'a, T> {
/// use rune::alloc::try_vec;
///
/// let bytes = rune::to_value(Bytes::from_vec(try_vec![1, 2, 3, 4]))?;
/// let bytes = bytes.borrow_bytes_ref().into_result()?;
/// let bytes = bytes.borrow_bytes_ref()?;
///
/// let bytes: BorrowRef<[u8]> = BorrowRef::map(bytes, |bytes| &bytes[0..2]);
///
Expand All @@ -368,7 +387,7 @@ impl<'a, T: ?Sized> BorrowRef<'a, T> {
/// use rune::alloc::try_vec;
///
/// let bytes = rune::to_value(Bytes::from_vec(try_vec![1, 2, 3, 4]))?;
/// let bytes = bytes.borrow_bytes_ref().into_result()?;
/// let bytes = bytes.borrow_bytes_ref()?;
///
/// let Ok(bytes) = BorrowRef::try_map(bytes, |bytes| bytes.get(0..2)) else {
/// panic!("Conversion failed");
Expand Down Expand Up @@ -495,7 +514,7 @@ impl<'a, T: ?Sized> BorrowMut<'a, T> {
/// use rune::alloc::try_vec;
///
/// let bytes = rune::to_value(Bytes::from_vec(try_vec![1, 2, 3, 4]))?;
/// let bytes = bytes.borrow_bytes_mut().into_result()?;
/// let bytes = bytes.borrow_bytes_mut()?;
///
/// let mut bytes: BorrowMut<[u8]> = BorrowMut::map(bytes, |bytes| &mut bytes[0..2]);
///
Expand All @@ -522,7 +541,7 @@ impl<'a, T: ?Sized> BorrowMut<'a, T> {
/// use rune::alloc::try_vec;
///
/// let bytes = rune::to_value(Bytes::from_vec(try_vec![1, 2, 3, 4]))?;
/// let bytes = bytes.borrow_bytes_mut().into_result()?;
/// let bytes = bytes.borrow_bytes_mut()?;
///
/// let Ok(mut bytes) = BorrowMut::try_map(bytes, |bytes| bytes.get_mut(0..2)) else {
/// panic!("Conversion failed");
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/runtime/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ mod tests {
let shared = Value::try_from(Bytes::new())?;

let _ = {
let shared = shared.into_bytes_ref().into_result()?;
let shared = shared.into_bytes_ref()?;
let out = shared.try_clone()?;
out
};
Expand Down
8 changes: 4 additions & 4 deletions crates/rune/src/runtime/from_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ where

impl FromValue for AnyObj {
fn from_value(value: Value) -> VmResult<Self> {
value.into_any_obj()
VmResult::Ok(vm_try!(value.into_any_obj()))
}
}

Expand Down Expand Up @@ -481,7 +481,7 @@ macro_rules! impl_number {
impl FromValue for $ty {
#[inline]
fn from_value(value: Value) -> VmResult<Self> {
value.try_as_integer()
VmResult::Ok(vm_try!(value.try_as_integer()))
}
}
};
Expand All @@ -501,7 +501,7 @@ impl_number!(isize);
impl FromValue for f64 {
#[inline]
fn from_value(value: Value) -> VmResult<Self> {
value.as_float()
VmResult::Ok(vm_try!(value.as_float()))
}
}

Expand Down Expand Up @@ -570,6 +570,6 @@ impl_try_map!(alloc::HashMap<::rust_alloc::string::String, T>, ::rust_alloc::str
impl FromValue for Ordering {
#[inline]
fn from_value(value: Value) -> VmResult<Self> {
value.as_ordering()
VmResult::Ok(vm_try!(value.as_ordering()))
}
}
7 changes: 7 additions & 0 deletions crates/rune/src/runtime/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,10 @@ impl From<PanicReason> for Panic {
}
}
}

impl PartialEq for Panic {
#[inline]
fn eq(&self, _: &Self) -> bool {
true
}
}
2 changes: 1 addition & 1 deletion crates/rune/src/runtime/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::alloc::{self, Vec};
use crate::runtime::{InstAddress, Value};

/// An error raised when interacting with the stack.
#[derive(Debug)]
#[derive(Debug, PartialEq)]
#[non_exhaustive]
pub struct StackError;

Expand Down
23 changes: 7 additions & 16 deletions crates/rune/src/runtime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ struct Foo(isize);
#[test]
fn test_take() -> Result<()> {
let thing = Value::try_from(AnyObj::new(Foo(0))?)?;
let _ = thing.into_any_obj().into_result()?;
let _ = thing.into_any_obj()?;
Ok(())
}

#[test]
fn test_clone_take() -> Result<()> {
let thing = Value::try_from(AnyObj::new(Foo(0))?)?;
let thing2 = thing.clone();
assert_eq!(Foo(0), thing2.into_any::<Foo>().into_result()?);
assert_eq!(Foo(0), thing2.into_any::<Foo>()?);
assert!(thing.into_any_obj().is_err());
Ok(())
}
Expand All @@ -33,10 +33,7 @@ fn test_from_ref() -> Result<()> {
unsafe {
let (value, guard) = Value::from_ref(&value)?;
assert!(value.clone().into_any_mut::<Thing>().is_err());
assert_eq!(
10u32,
value.clone().into_any_ref::<Thing>().into_result()?.0
);
assert_eq!(10u32, value.clone().into_any_ref::<Thing>()?.0);

drop(guard);

Expand All @@ -56,16 +53,10 @@ fn test_from_mut() -> Result<()> {

unsafe {
let (value, guard) = Value::from_mut(&mut value)?;
value.clone().into_any_mut::<Thing>().into_result()?.0 = 20;

assert_eq!(
20u32,
value.clone().into_any_mut::<Thing>().into_result()?.0
);
assert_eq!(
20u32,
value.clone().into_any_ref::<Thing>().into_result()?.0
);
value.clone().into_any_mut::<Thing>()?.0 = 20;

assert_eq!(20u32, value.clone().into_any_mut::<Thing>()?.0);
assert_eq!(20u32, value.clone().into_any_ref::<Thing>()?.0);

drop(guard);

Expand Down
Loading

0 comments on commit 5ee7e13

Please sign in to comment.