Skip to content

Commit

Permalink
rune: Rename and document methods on Value
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Nov 3, 2024
1 parent 9c69ad8 commit d50fa77
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 54 deletions.
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
2 changes: 1 addition & 1 deletion crates/rune/src/runtime/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ impl UnsafeToRef for [u8] {
type Guard = RawAnyGuard;

unsafe fn unsafe_to_ref<'a>(value: Value) -> VmResult<(&'a Self, Self::Guard)> {
let (value, guard) = Ref::into_raw(vm_try!(value.into_any_ref::<Bytes>()));
let (value, guard) = Ref::into_raw(vm_try!(value.into_ref::<Bytes>()));
VmResult::Ok((value.as_ref().as_slice(), guard))
}
}
Expand Down
18 changes: 9 additions & 9 deletions crates/rune/src/runtime/from_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ where
{
#[inline]
fn from_value(value: Value) -> Result<Self, RuntimeError> {
value.into_any()
value.downcast()
}
}

Expand All @@ -257,7 +257,7 @@ where
{
#[inline]
fn from_value(value: Value) -> Result<Self, RuntimeError> {
value.into_any_mut()
value.into_mut()
}
}

Expand All @@ -267,7 +267,7 @@ where
{
#[inline]
fn from_value(value: Value) -> Result<Self, RuntimeError> {
value.into_any_ref()
value.into_ref()
}
}

Expand All @@ -291,7 +291,7 @@ where
T: FromValue,
{
fn from_value(value: Value) -> Result<Self, RuntimeError> {
Ok(match value.into_any::<Option<Value>>()? {
Ok(match value.downcast::<Option<Value>>()? {
Some(some) => Some(T::from_value(some.clone())?),
None => None,
})
Expand Down Expand Up @@ -333,7 +333,7 @@ impl UnsafeToRef for str {
type Guard = RawAnyGuard;

unsafe fn unsafe_to_ref<'a>(value: Value) -> VmResult<(&'a Self, Self::Guard)> {
let string = vm_try!(value.into_any_ref::<String>());
let string = vm_try!(value.into_ref::<String>());
let (string, guard) = Ref::into_raw(string);
VmResult::Ok((string.as_ref().as_str(), guard))
}
Expand All @@ -343,7 +343,7 @@ impl UnsafeToMut for str {
type Guard = RawAnyGuard;

unsafe fn unsafe_to_mut<'a>(value: Value) -> VmResult<(&'a mut Self, Self::Guard)> {
let string = vm_try!(value.into_any_mut::<String>());
let string = vm_try!(value.into_mut::<String>());
let (mut string, guard) = Mut::into_raw(string);
VmResult::Ok((string.as_mut().as_mut_str(), guard))
}
Expand All @@ -356,7 +356,7 @@ where
{
#[inline]
fn from_value(value: Value) -> Result<Self, RuntimeError> {
Ok(match value.into_any::<Result<Value, Value>>()? {
Ok(match value.downcast::<Result<Value, Value>>()? {
Ok(ok) => Result::Ok(T::from_value(ok.clone())?),
Err(err) => Result::Err(E::from_value(err.clone())?),
})
Expand Down Expand Up @@ -435,7 +435,7 @@ cfg_std! {
T: FromValue,
{
fn from_value(value: Value) -> Result<Self, RuntimeError> {
let object = value.into_any::<$crate::runtime::Object>()?;
let object = value.downcast::<$crate::runtime::Object>()?;

let mut output = <$ty>::with_capacity(object.len());

Expand All @@ -462,7 +462,7 @@ macro_rules! impl_try_map {
T: FromValue,
{
fn from_value(value: Value) -> Result<Self, RuntimeError> {
let object = value.into_any::<$crate::runtime::Object>()?;
let object = value.downcast::<$crate::runtime::Object>()?;

let mut output = <$ty>::try_with_capacity(object.len())?;

Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/runtime/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ struct FnTupleVariant {
impl FromValue for SyncFunction {
#[inline]
fn from_value(value: Value) -> Result<Self, RuntimeError> {
value.into_any::<Function>()?.into_sync()
value.downcast::<Function>()?.into_sync()
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/runtime/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ where
{
#[inline]
fn from_value(value: Value) -> Result<Self, RuntimeError> {
let range = value.into_any::<Range>()?;
let range = value.downcast::<Range>()?;
let start = Idx::from_value(range.start)?;
let end = Idx::from_value(range.end)?;
Ok(ops::Range { start, end })
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/runtime/range_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ where
{
#[inline]
fn from_value(value: Value) -> Result<Self, RuntimeError> {
let range = value.into_any::<RangeFrom>()?;
let range = value.downcast::<RangeFrom>()?;
let start = Idx::from_value(range.start)?;
Ok(ops::RangeFrom { start })
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/runtime/range_full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl ToValue for ops::RangeFull {
impl FromValue for ops::RangeFull {
#[inline]
fn from_value(value: Value) -> Result<Self, RuntimeError> {
let RangeFull = value.into_any::<RangeFull>()?;
let RangeFull = value.downcast::<RangeFull>()?;
Ok(ops::RangeFull)
}
}
2 changes: 1 addition & 1 deletion crates/rune/src/runtime/range_inclusive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ where
{
#[inline]
fn from_value(value: Value) -> Result<Self, RuntimeError> {
let range = value.into_any::<RangeInclusive>()?;
let range = value.downcast::<RangeInclusive>()?;
let start = Idx::from_value(range.start)?;
let end = Idx::from_value(range.end)?;
Ok(start..=end)
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/runtime/range_to.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ where
{
#[inline]
fn from_value(value: Value) -> Result<Self, RuntimeError> {
let range = value.into_any::<RangeTo>()?;
let range = value.downcast::<RangeTo>()?;
let end = Idx::from_value(range.end)?;
Ok(ops::RangeTo { end })
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/runtime/range_to_inclusive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ where
{
#[inline]
fn from_value(value: Value) -> Result<Self, RuntimeError> {
let range = value.into_any::<RangeToInclusive>()?;
let range = value.downcast::<RangeToInclusive>()?;
let end = Idx::from_value(range.end)?;
Ok(ops::RangeToInclusive { end })
}
Expand Down
6 changes: 3 additions & 3 deletions crates/rune/src/runtime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ fn test_clone_take() -> Result<()> {
let v = Value::from(AnyObj::new(Thing(0))?);
let v2 = v.clone();
let v3 = v.clone();
assert_eq!(Thing(0), v2.into_any::<Thing>()?);
assert!(v3.into_any::<Thing>().is_err());
assert_eq!(Thing(0), v2.downcast::<Thing>()?);
assert!(v3.downcast::<Thing>().is_err());
let any = v.into_any_obj()?;
assert_eq!(any.type_hash(), Thing::HASH);
Ok(())
Expand Down Expand Up @@ -549,7 +549,7 @@ fn test_clone_issue() {
let shared = Value::try_from(Bytes::new()).unwrap();

let _ = {
let shared = shared.into_any_ref::<Bytes>().unwrap();
let shared = shared.into_ref::<Bytes>().unwrap();
let out = shared.try_clone().unwrap();
out
};
Expand Down
Loading

0 comments on commit d50fa77

Please sign in to comment.