Skip to content

Commit

Permalink
rune: Remove a number of static_type's
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Nov 1, 2024
1 parent bfc24de commit 70cec66
Show file tree
Hide file tree
Showing 19 changed files with 150 additions and 98 deletions.
10 changes: 1 addition & 9 deletions crates/rune/src/compile/named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::fmt;
use core::marker::PhantomData;

use crate as rune;
use crate::alloc::{Box, String};
use crate::alloc::String;
use crate::module::InstallWith;
use crate::{item, Item};

Expand Down Expand Up @@ -51,14 +51,6 @@ impl Named for String {
const ITEM: &'static Item = item!(::std::string::String);
}

impl Named for &str {
const ITEM: &'static Item = item!(::std::string::String);
}

impl Named for Box<str> {
const ITEM: &'static Item = item!(::std::string::String);
}

impl InstallWith for String {}

impl Named for i64 {
Expand Down
10 changes: 5 additions & 5 deletions crates/rune/src/modules/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub fn module() -> Result<Module, ContextError> {
}

split!(Function);
split!(Box<str>);
split!(String);
split!(char);
Ok(m)
}
Expand Down Expand Up @@ -910,7 +910,7 @@ fn split(this: Ref<str>, value: Value) -> VmResult<Value> {

vm_try!(rune::to_value(Split::new(
this,
vm_try!(Box::try_from(s.as_str()))
vm_try!(String::try_from(s.as_str()))
)))
}
_ => {
Expand Down Expand Up @@ -1476,9 +1476,9 @@ trait Pattern: 'static + TryClone + Named + FromValue + ToValue + MaybeTypeOf +
fn is_empty(&self) -> bool;
}

impl Pattern for Box<str> {
impl Pattern for String {
fn test(&self, tail: &str) -> VmResult<(bool, usize)> {
if tail.starts_with(self.as_ref()) {
if tail.starts_with(self.as_str()) {
VmResult::Ok((true, self.len()))
} else {
let Some(c) = tail.chars().next() else {
Expand All @@ -1491,7 +1491,7 @@ impl Pattern for Box<str> {

#[inline]
fn is_empty(&self) -> bool {
self.as_ref().is_empty()
String::is_empty(self)
}
}

Expand Down
1 change: 0 additions & 1 deletion crates/rune/src/runtime/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use super::{IntoOutput, RawAnyGuard, Ref, UnsafeToRef, Value, VmResult};

/// A vector of bytes.
#[derive(Any, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[rune(static_type = BYTES)]
#[rune(item = ::std::bytes)]
pub struct Bytes {
bytes: Vec<u8>,
Expand Down
6 changes: 3 additions & 3 deletions crates/rune/src/runtime/const_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ impl ConstValueKind {

match self {
ConstValueKind::Inline(value) => value.type_info(),
ConstValueKind::String(..) => TypeInfo::static_type(runtime::static_type::STRING),
ConstValueKind::Bytes(..) => TypeInfo::static_type(runtime::static_type::BYTES),
ConstValueKind::Vec(..) => TypeInfo::static_type(runtime::static_type::VEC),
ConstValueKind::String(..) => TypeInfo::any::<String>(),
ConstValueKind::Bytes(..) => TypeInfo::any::<Bytes>(),
ConstValueKind::Vec(..) => TypeInfo::any::<runtime::Vec>(),
ConstValueKind::Tuple(..) => TypeInfo::static_type(runtime::static_type::TUPLE),
ConstValueKind::Object(..) => TypeInfo::static_type(runtime::static_type::OBJECT),
ConstValueKind::Option(..) => TypeInfo::static_type(runtime::static_type::OPTION),
Expand Down
1 change: 0 additions & 1 deletion crates/rune/src/runtime/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use super::{
/// ```
#[derive(Debug, Clone, TryClone, Any)]
#[try_clone(crate)]
#[rune(static_type = CONTROL_FLOW)]
#[rune(item = ::std::ops)]
pub enum ControlFlow {
/// Move on to the next phase of the operation as normal.
Expand Down
1 change: 0 additions & 1 deletion crates/rune/src/runtime/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ impl fmt::Display for AlignmentFromStrError {

/// A format specification, wrapping an inner value.
#[derive(Any, Debug, Clone, TryClone)]
#[rune(static_type = FORMAT)]
#[rune(item = ::std::fmt)]
pub struct Format {
/// The value being formatted.
Expand Down
1 change: 0 additions & 1 deletion crates/rune/src/runtime/generator_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ use crate::Any;
/// # Ok::<_, rune::support::Error>(())
/// ```
#[derive(Any, Debug, TryClone)]
#[rune(static_type = GENERATOR_STATE)]
#[rune(item = ::std::ops::generator)]
pub enum GeneratorState {
/// The generator yielded.
Expand Down
3 changes: 1 addition & 2 deletions crates/rune/src/runtime/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ use super::StepsBetween;
/// ```
#[derive(Any, Clone, TryClone)]
#[try_clone(crate)]
#[rune(constructor, static_type = RANGE)]
#[rune(item = ::std::ops)]
#[rune(crate, constructor, item = ::std::ops)]
pub struct Range {
/// The start value of the range.
#[rune(get, set)]
Expand Down
3 changes: 1 addition & 2 deletions crates/rune/src/runtime/range_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ use crate::Any;
/// ```
#[derive(Any, Clone, TryClone)]
#[try_clone(crate)]
#[rune(constructor, static_type = RANGE_FROM)]
#[rune(item = ::std::ops)]
#[rune(crate, constructor, item = ::std::ops)]
pub struct RangeFrom {
/// The start value of the range.
#[rune(get, set)]
Expand Down
3 changes: 1 addition & 2 deletions crates/rune/src/runtime/range_full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ use crate::Any;
/// ```
#[derive(Any, Default, Clone, TryClone)]
#[try_clone(crate)]
#[rune(constructor, static_type = RANGE_FULL)]
#[rune(item = ::std::ops)]
#[rune(crate, constructor, item = ::std::ops)]
pub struct RangeFull;

impl RangeFull {
Expand Down
3 changes: 1 addition & 2 deletions crates/rune/src/runtime/range_inclusive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ use super::StepsBetween;
/// ```
#[derive(Any, Clone, TryClone)]
#[try_clone(crate)]
#[rune(constructor, static_type = RANGE_INCLUSIVE)]
#[rune(item = ::std::ops)]
#[rune(crate, constructor, item = ::std::ops)]
pub struct RangeInclusive {
/// The start value of the range.
#[rune(get, set)]
Expand Down
3 changes: 1 addition & 2 deletions crates/rune/src/runtime/range_to.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ use crate::Any;
#[derive(Any, Clone, TryClone)]
#[try_clone(crate)]
#[rune(crate)]
#[rune(constructor, static_type = RANGE_TO)]
#[rune(item = ::std::ops)]
#[rune(constructor, item = ::std::ops)]
pub struct RangeTo {
/// The end value of the range.
#[rune(get, set)]
Expand Down
3 changes: 1 addition & 2 deletions crates/rune/src/runtime/range_to_inclusive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ use crate::Any;
/// # Ok::<_, rune::support::Error>(())
/// ```
#[derive(Any, Clone, TryClone)]
#[rune(constructor, static_type = RANGE_TO_INCLUSIVE)]
#[rune(item = ::std::ops)]
#[rune(constructor, item = ::std::ops)]
pub struct RangeToInclusive {
/// The end value of the range.
#[rune(get, set)]
Expand Down
117 changes: 80 additions & 37 deletions crates/rune/src/runtime/static_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,63 @@ impl hash::Hash for StaticType {
}
}

macro_rules! any_type {
(
$(
$(#[$($meta:meta)*])*
$path:path {
$(
$(#[$($impl_meta:meta)*])*
impl $(<$($p:ident),*>)? for $ty:ty;
)*
}
)*
) => {
$(
$(
$(#[$($impl_meta)*])*
impl $(<$($p,)*>)* $crate::TypeHash for $ty {
const HASH: $crate::Hash = ::rune_macros::hash!($path);
}

$(#[$($impl_meta)*])*
impl $(<$($p,)*>)* $crate::runtime::TypeOf for $ty
where
$(
$($p: $crate::runtime::MaybeTypeOf,)*
)*
{
const STATIC_TYPE_INFO: $crate::runtime::StaticTypeInfo = $crate::runtime::StaticTypeInfo::any_type_info(
$crate::runtime::AnyTypeInfo::new(
{
fn full_name(f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
write!(f, "{}", ::rune_macros::item!($path))
}

full_name
},
<Self as $crate::TypeHash>::HASH,
)
);
}

$(#[$($impl_meta)*])*
impl $(<$($p,)*>)* $crate::runtime::MaybeTypeOf for $ty
where
$(
$($p: $crate::runtime::MaybeTypeOf,)*
)*
{
#[inline]
fn maybe_type_of() -> $crate::alloc::Result<$crate::compile::meta::DocType> {
Ok($crate::compile::meta::DocType::new(<$ty as $crate::TypeHash>::HASH))
}
}
)*
)*
}
}

macro_rules! static_type {
(
$(
Expand Down Expand Up @@ -77,19 +134,19 @@ macro_rules! static_type {
}
}

static_type! {
any_type! {
/// The specialized type information for a bool type.
pub(crate) static [BOOL, BOOL_HASH] = ::std::bool {
::std::bool {
impl for bool;
}

/// The specialized type information for a char type.
pub(crate) static [CHAR, CHAR_HASH] = ::std::char {
::std::char {
impl for char;
}

/// The specialized type information for a integer type.
pub(crate) static [SIGNED, SIGNED_HASH] = ::std::i64 {
::std::i64 {
impl for i8;
impl for i16;
impl for i32;
Expand All @@ -99,7 +156,7 @@ static_type! {
}

/// The specialized type information for an unsigned integer type.
pub(crate) static [UNSIGNED, UNSIGNED_HASH] = ::std::u64 {
::std::u64 {
impl for u8;
impl for u16;
impl for u32;
Expand All @@ -109,13 +166,26 @@ static_type! {
}

/// The specialized type information for a float type.
pub(crate) static [FLOAT, FLOAT_HASH] = ::std::f64 {
::std::f64 {
impl for f32;
impl for f64;
}

::std::ops::ControlFlow {
impl<C, B> for ControlFlow<C, B>;
}

/// The specialized type information for the [`Bytes`] type.
::std::bytes::Bytes {
impl for [u8];
}

::std::cmp::Ordering {
impl for Ordering;
}

/// The specialized type information for a float type.
pub(crate) static [STRING, STRING_HASH] = ::std::string::String {
::std::string::String {
#[cfg(feature = "alloc")]
#[cfg_attr(rune_docsrs, doc(cfg(feature = "alloc")))]
impl for ::rust_alloc::string::String;
Expand All @@ -124,21 +194,18 @@ static_type! {
impl for str;
}

/// The specialized type information for the [`Bytes`] type.
pub(crate) static [BYTES, BYTES_HASH] = ::std::bytes::Bytes {
impl for [u8];
}

/// The specialized type information for the [`Vec`] type.
pub(crate) static [VEC, VEC_HASH] = ::std::vec::Vec {
::std::vec::Vec {
impl for [rt::Value];
#[cfg(feature = "alloc")]
#[cfg_attr(rune_docsrs, doc(cfg(feature = "alloc")))]
impl<T> for ::rust_alloc::vec::Vec<T>;
impl<T> for alloc::Vec<T>;
impl<T> for rt::VecTuple<T>;
}
}

static_type! {
/// The specialized type information for the [`Tuple`] type.
pub(crate) static [TUPLE, TUPLE_HASH] = ::std::tuple::Tuple {
impl for rt::OwnedTuple;
Expand All @@ -159,28 +226,10 @@ static_type! {
impl<T> for ::std::collections::HashMap<alloc::String, T>;
}

pub(crate) static [RANGE_FROM, RANGE_FROM_HASH] = ::std::ops::RangeFrom {}

pub(crate) static [RANGE_FULL, RANGE_FULL_HASH] = ::std::ops::RangeFull {}

pub(crate) static [RANGE_INCLUSIVE, RANGE_INCLUSIVE_HASH] = ::std::ops::RangeInclusive {}

pub(crate) static [RANGE_TO_INCLUSIVE, RANGE_TO_INCLUSIVE_HASH] = ::std::ops::RangeToInclusive {}

pub(crate) static [RANGE_TO, RANGE_TO_HASH] = ::std::ops::RangeTo {}

pub(crate) static [RANGE, RANGE_HASH] = ::std::ops::Range {}

pub(crate) static [CONTROL_FLOW, CONTROL_FLOW_HASH] = ::std::ops::ControlFlow {
impl<C, B> for ControlFlow<C, B>;
}

pub(crate) static [FUTURE, FUTURE_HASH] = ::std::future::Future {}

pub(crate) static [GENERATOR, GENERATOR_HASH] = ::std::ops::generator::Generator {}

pub(crate) static [GENERATOR_STATE, GENERATOR_STATE_HASH] = ::std::ops::generator::GeneratorState {}

pub(crate) static [STREAM, STREAM_HASH] = ::std::stream::Stream {}

pub(crate) static [RESULT, RESULT_HASH] = ::std::result::Result {
Expand All @@ -193,12 +242,6 @@ static_type! {

pub(crate) static [FUNCTION, FUNCTION_HASH] = ::std::ops::Function {}

pub(crate) static [FORMAT, FORMAT_HASH] = ::std::fmt::Format {}

pub(crate) static [ORDERING, ORDERING_HASH] = ::std::cmp::Ordering {
impl for Ordering;
}

pub(crate) static [TYPE, TYPE_HASH] = ::std::any::Type {
impl for rt::Type;
}
Expand Down
Loading

0 comments on commit 70cec66

Please sign in to comment.