Skip to content

Commit

Permalink
rune: Add some convenience methods for &str and &[u8]
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Oct 28, 2024
1 parent f0edb2f commit 2bddc1a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
36 changes: 36 additions & 0 deletions crates/rune/src/runtime/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1905,6 +1905,42 @@ impl TryFrom<Mutable> for Value {
}
}

impl TryFrom<&str> for Value {
type Error = alloc::Error;

#[inline]
fn try_from(value: &str) -> Result<Self, Self::Error> {
Value::new(String::try_from(value)?)
}
}

impl IntoOutput for &str {
type Output = String;

#[inline]
fn into_output(self) -> VmResult<Self::Output> {
VmResult::Ok(vm_try!(String::try_from(self)))
}
}

impl TryFrom<&[u8]> for Value {
type Error = alloc::Error;

#[inline]
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
Value::new(Bytes::try_from(value)?)
}
}

impl IntoOutput for &[u8] {
type Output = Bytes;

#[inline]
fn into_output(self) -> VmResult<Self::Output> {
VmResult::Ok(vm_try!(Bytes::try_from(self)))
}
}

impl IntoOutput for Mutable {
type Output = Mutable;

Expand Down
34 changes: 16 additions & 18 deletions crates/rune/src/runtime/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@ use crate::alloc::prelude::*;
use crate::alloc::{self, String};
use crate::hash::{Hash, IntoHash, ToTypeHash};
use crate::modules::{option, result};
use crate::runtime::future::SelectFuture;
use crate::runtime::unit::{UnitFn, UnitStorage};
use crate::runtime::{
self, Args, Awaited, BorrowMut, Bytes, Call, ControlFlow, DynArgs, DynGuardedArgs, EmptyStruct,
Format, FormatSpec, Formatter, FromValue, Function, Future, Generator, GuardedArgs, Inline,
Inst, InstAddress, InstAssignOp, InstOp, InstRange, InstTarget, InstValue, InstVariant,
Mutable, Object, Output, OwnedTuple, Pair, Panic, Protocol, Range, RangeFrom, RangeFull,
RangeInclusive, RangeTo, RangeToInclusive, RuntimeContext, Select, Stack, Stream, Struct, Type,
TypeCheck, TypeOf, Unit, Value, ValueBorrowRef, ValueMut, ValueRef, Variant, VariantData, Vec,
VmError, VmErrorKind, VmExecution, VmHalt, VmIntegerRepr, VmResult, VmSendExecution,
};
use crate::runtime::{budget, ProtocolCaller};

use super::{VmDiagnostics, VmDiagnosticsObj};
use super::{
budget, static_type, Args, Awaited, BorrowMut, Bytes, Call, ControlFlow, DynArgs,
DynGuardedArgs, EmptyStruct, Format, FormatSpec, Formatter, FromValue, Function, Future,
Generator, GuardedArgs, Inline, Inst, InstAddress, InstAssignOp, InstOp, InstRange, InstTarget,
InstValue, InstVariant, Mutable, Object, Output, OwnedTuple, Pair, Panic, Protocol,
ProtocolCaller, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive,
RuntimeContext, Select, SelectFuture, Stack, Stream, Struct, Type, TypeCheck, TypeOf, Unit,
UnitFn, UnitStorage, Value, ValueBorrowRef, ValueMut, ValueRef, Variant, VariantData, Vec,
VmDiagnostics, VmDiagnosticsObj, VmError, VmErrorKind, VmExecution, VmHalt, VmIntegerRepr,
VmResult, VmSendExecution,
};

/// Helper to take a value, replacing the old one with empty.
#[inline(always)]
Expand Down Expand Up @@ -1238,9 +1236,9 @@ impl Vm {
macro_rules! convert {
($from:ty, $value:expr) => {
match ty.into_hash() {
runtime::static_type::FLOAT_HASH => Value::from($value as f64),
runtime::static_type::BYTE_HASH => Value::from($value as u8),
runtime::static_type::INTEGER_HASH => Value::from($value as i64),
static_type::FLOAT_HASH => Value::from($value as f64),
static_type::BYTE_HASH => Value::from($value as u8),
static_type::INTEGER_HASH => Value::from($value as i64),
ty => {
return err(VmErrorKind::UnsupportedAs {
value: <$from as TypeOf>::type_info(),
Expand Down Expand Up @@ -2931,14 +2929,14 @@ impl Vm {
#[cfg_attr(feature = "bench", inline(never))]
fn op_string(&mut self, slot: usize, out: Output) -> VmResult<()> {
let string = vm_try!(self.unit.lookup_string(slot));
vm_try!(out.store(&mut self.stack, || String::try_from(string.as_str())));
vm_try!(out.store(&mut self.stack, string.as_str()));
VmResult::Ok(())
}

#[cfg_attr(feature = "bench", inline(never))]
fn op_bytes(&mut self, slot: usize, out: Output) -> VmResult<()> {
let bytes = vm_try!(self.unit.lookup_bytes(slot));
vm_try!(out.store(&mut self.stack, || Bytes::try_from(bytes)));
vm_try!(out.store(&mut self.stack, bytes));
VmResult::Ok(())
}

Expand Down

0 comments on commit 2bddc1a

Please sign in to comment.