diff --git a/crates/rune-modules/src/core.rs b/crates/rune-modules/src/core.rs deleted file mode 100644 index e48ce78c3..000000000 --- a/crates/rune-modules/src/core.rs +++ /dev/null @@ -1,14 +0,0 @@ -//! `std` core module for the [Rune Language]. -//! -//! [Rune Language]: https://rune-rs.github.io -//! -//! Note: **this has been deprecated**, all functions in this module have been -//! moved into the [`rune` crate][rune::modules]. - -use rune::{ContextError, Module}; - -/// Construct the `std::core` module. -#[deprecated = "all functions in this module have been included in the rune crate, see https://github.com/rune-rs/rune/issues/456"] -pub fn module(_stdio: bool) -> Result { - rune::modules::core::module() -} diff --git a/crates/rune-modules/src/fmt.rs b/crates/rune-modules/src/fmt.rs deleted file mode 100644 index c9f4b601c..000000000 --- a/crates/rune-modules/src/fmt.rs +++ /dev/null @@ -1,14 +0,0 @@ -//! `std::fmt` module for the [Rune Language]. -//! -//! [Rune Language]: https://rune-rs.github.io -//! -//! Note: **this has been deprecated**, all functions in this module have been -//! moved into the [`rune` crate][rune::modules]. - -use rune::{ContextError, Module}; - -/// Construct the supplemental `std::io` module. -#[deprecated = "all functions in this module have been included in the rune crate, see https://github.com/rune-rs/rune/issues/456"] -pub fn module(_stdio: bool) -> Result { - rune::modules::fmt::module() -} diff --git a/crates/rune-modules/src/io.rs b/crates/rune-modules/src/io.rs deleted file mode 100644 index b7480fca6..000000000 --- a/crates/rune-modules/src/io.rs +++ /dev/null @@ -1,14 +0,0 @@ -//! `std::io` module for the [Rune Language]. -//! -//! [Rune Language]: https://rune-rs.github.io -//! -//! Note: **this has been deprecated**, all functions in this module have been -//! moved into the [`rune` crate][rune::modules]. - -use rune::{ContextError, Module}; - -/// Construct the supplemental `std::io` module. -#[deprecated = "all functions in this module have been included in the rune crate, see https://github.com/rune-rs/rune/issues/456"] -pub fn module(stdio: bool) -> Result { - rune::modules::io::module(stdio) -} diff --git a/crates/rune-modules/src/lib.rs b/crates/rune-modules/src/lib.rs index 2095769a5..0d1b7f8e1 100644 --- a/crates/rune-modules/src/lib.rs +++ b/crates/rune-modules/src/lib.rs @@ -77,13 +77,8 @@ // support intra-doc links (yet): // https://github.com/livioribeiro/cargo-readme/issues/55 -macro_rules! modules { +macro_rules! entry { ($({$ident:ident, $name:literal $(, $module:ident)*}),* $(,)?) => { - $( - #[cfg(feature = $name)] - pub mod $ident; - )* - /// Construct a a default rune context with all enabled modules provided /// based on the [default rune /// context](rune::Context::with_default_modules). @@ -110,15 +105,41 @@ macro_rules! modules { } } -modules! { +#[cfg(feature = "base64")] +pub mod base64; + +#[cfg(feature = "fs")] +pub mod fs; + +#[cfg(feature = "http")] +pub mod http; + +#[cfg(feature = "json")] +pub mod json; + +#[cfg(feature = "process")] +pub mod process; + +#[cfg(feature = "rand")] +pub mod rand; + +#[cfg(feature = "signal")] +pub mod signal; + +#[cfg(feature = "test")] +pub mod test; + +#[cfg(feature = "time")] +pub mod time; + +#[cfg(feature = "toml")] +pub mod toml; + +entry! { {base64, "base64"}, - {core, "core"}, - {fmt, "fmt"}, {fs, "fs"}, {http, "http"}, - {io, "io"}, {json, "json"}, - {macros, "macros"}, {process, "process"}, {rand, "rand"}, {signal, "signal"}, diff --git a/crates/rune-modules/src/macros.rs b/crates/rune-modules/src/macros.rs deleted file mode 100644 index 24ef35216..000000000 --- a/crates/rune-modules/src/macros.rs +++ /dev/null @@ -1,14 +0,0 @@ -//! `std::macros` module for the [Rune Language]. -//! -//! [Rune Language]: https://rune-rs.github.io -//! -//! Note: **this has been deprecated**, all functions in this module have been -//! moved into the [`rune` crate][rune::modules]. - -use rune::{ContextError, Module}; - -/// Construct the supplemental `std::macros` module. -#[deprecated = "all functions in this module have been included in the rune crate, see https://github.com/rune-rs/rune/issues/456"] -pub fn module(_stdio: bool) -> Result { - rune::modules::macros::module() -} diff --git a/crates/rune-modules/src/time.rs b/crates/rune-modules/src/time.rs index 428361932..6260844ce 100644 --- a/crates/rune-modules/src/time.rs +++ b/crates/rune-modules/src/time.rs @@ -48,9 +48,9 @@ struct Duration { impl Duration { /// Construct a duration from the given number of seconds. - /// + /// /// # Examples - /// + /// /// ```rune /// use time::Duration; /// @@ -65,9 +65,9 @@ impl Duration { } /// Sleep for the given [`Duration`]. -/// +/// /// # Examples -/// +/// /// ```rune,no_run /// use time::Duration; /// diff --git a/crates/rune/src/module/module.rs b/crates/rune/src/module/module.rs index 9e0a5e52d..b1261f661 100644 --- a/crates/rune/src/module/module.rs +++ b/crates/rune/src/module/module.rs @@ -501,17 +501,24 @@ impl Module { /// # Examples /// /// ``` - /// use rune::{Any, Module}; + /// use rune::{docstring, Any, Module}; /// /// let mut module = Module::default(); /// /// #[derive(Any)] /// struct MyType; /// - /// module.constant("TEN", 10).build()?.docs(["A global ten value."]); - /// module.constant("TEN", 10).build_associated::()?.docs(rune::docstring! { - /// /// Ten which looks like an associated constant. - /// }); + /// module.constant("TEN", 10) + /// .build()? + /// .docs(docstring! { + /// /// A global ten value. + /// }); + /// + /// module.constant("TEN", 10) + /// .build_associated::()? + /// .docs(docstring! { + /// /// Ten which looks like an associated constant. + /// }); /// # Ok::<_, rune::support::Error>(()) /// ``` pub fn constant(&mut self, name: N, value: V) -> ModuleConstantBuilder<'_, N, V> @@ -965,7 +972,7 @@ impl Module { /// # Examples /// /// ``` - /// use rune::Module; + /// use rune::{docstring, Module}; /// /// fn add_ten(value: i64) -> i64 { /// value + 10 @@ -975,14 +982,16 @@ impl Module { /// /// module.function("add_ten", add_ten) /// .build()? - /// .docs(["Adds 10 to any integer passed in."])?; + /// .docs(docstring! { + /// /// Adds 10 to any integer passed in. + /// }); /// # Ok::<_, rune::support::Error>(()) /// ``` /// /// Asynchronous function: /// /// ``` - /// use rune::{Any, Module}; + /// use rune::{docstring, Any, Module}; /// # async fn download(url: &str) -> Result { Ok(String::new()) } /// /// #[derive(Any)] @@ -996,8 +1005,11 @@ impl Module { /// /// let mut module = Module::default(); /// - /// module.function("download_quote", download_quote).build()? - /// .docs(["Download a random quote from the internet."]); + /// module.function("download_quote", download_quote) + /// .build()? + /// .docs(docstring! { + /// /// Download a random quote from the internet. + /// }); /// # Ok::<_, rune::support::Error>(()) /// ``` pub fn function(&mut self, name: N, f: F) -> ModuleFunctionBuilder<'_, F, A, N, K> @@ -1359,13 +1371,13 @@ impl Module { /// ``` /// use rune::Module; /// use rune::runtime::{Output, Memory, ToValue, VmResult, InstAddress}; - /// use rune::vm_try; + /// use rune::{docstring, vm_try}; /// /// fn sum(stack: &mut dyn Memory, addr: InstAddress, args: usize, out: Output) -> VmResult<()> { /// let mut number = 0; /// /// for value in vm_try!(stack.slice_at(addr, args)) { - /// number += vm_try!(value.as_integer()); + /// number += vm_try!(value.as_integer::()); /// } /// /// out.store(stack, number); @@ -1376,9 +1388,9 @@ impl Module { /// /// module.raw_function("sum", sum) /// .build()? - /// .docs([ - /// "Sum all numbers provided to the function." - /// ])?; + /// .docs(docstring! { + /// /// Sum all numbers provided to the function. + /// })?; /// /// # Ok::<_, rune::support::Error>(()) /// ``` diff --git a/crates/rune/src/module/module_constant_builder.rs b/crates/rune/src/module/module_constant_builder.rs index 04760078c..3b58c74e1 100644 --- a/crates/rune/src/module/module_constant_builder.rs +++ b/crates/rune/src/module/module_constant_builder.rs @@ -67,14 +67,18 @@ where /// # Examples /// /// ``` - /// use rune::{Any, Module}; + /// use rune::{docstring, Any, Module}; /// /// let mut module = Module::default(); /// /// #[derive(Any)] /// struct Thing; /// - /// module.constant("TEN", 10).build_associated::()?.docs(["Ten which is an associated constant."]); + /// module.constant("TEN", 10) + /// .build_associated::()? + /// .docs(docstring! { + /// /// Ten which is an associated constant. + /// }); /// # Ok::<_, rune::support::Error>(()) /// ``` pub fn build_associated(self) -> Result, ContextError> diff --git a/crates/rune/src/modules/any.rs b/crates/rune/src/modules/any.rs index b61a97b29..a3d1e38f2 100644 --- a/crates/rune/src/modules/any.rs +++ b/crates/rune/src/modules/any.rs @@ -4,7 +4,7 @@ use crate as rune; use crate::alloc::fmt::TryWrite; use crate::alloc::String; use crate::runtime::{Formatter, Type, Value, VmResult}; -use crate::{ContextError, Module}; +use crate::{docstring, ContextError, Module}; /// Dynamic typing and type reflection. /// @@ -16,8 +16,10 @@ use crate::{ContextError, Module}; pub fn module() -> Result { let mut m = Module::from_meta(self::module_meta)?; - m.ty::()? - .docs(["Represents a type in the Rune type system."])?; + m.ty::()?.docs(docstring! { + /// Represents a type in the Rune type system. + })?; + m.function_meta(type_of_val)?; m.function_meta(type_name_of_val)?; m.function_meta(format_type)?; diff --git a/crates/rune/src/modules/ops/generator.rs b/crates/rune/src/modules/ops/generator.rs index 3765e4fb7..343e90c22 100644 --- a/crates/rune/src/modules/ops/generator.rs +++ b/crates/rune/src/modules/ops/generator.rs @@ -45,8 +45,9 @@ pub fn module() -> Result { } { - m.generator_state(["GeneratorState"])? - .docs(["Enum indicating the state of a generator."])?; + m.generator_state(["GeneratorState"])?.docs(docstring! { + /// Enum indicating the state of a generator. + })?; m.function_meta(generator_state_partial_eq)?; m.implement_trait::(rune::item!(::std::cmp::PartialEq))?; diff --git a/crates/rune/src/modules/tuple.rs b/crates/rune/src/modules/tuple.rs index c7e96105c..4cc1590f7 100644 --- a/crates/rune/src/modules/tuple.rs +++ b/crates/rune/src/modules/tuple.rs @@ -5,7 +5,7 @@ use core::cmp::Ordering; use crate as rune; use crate::runtime::slice::Iter; use crate::runtime::{EnvProtocolCaller, Hasher, Ref, Tuple, Value, Vec, VmResult}; -use crate::{ContextError, Module}; +use crate::{docstring, ContextError, Module}; /// The [`Tuple`] fixed collection. /// @@ -42,7 +42,9 @@ use crate::{ContextError, Module}; #[rune::module(::std::tuple)] pub fn module() -> Result { let mut m = Module::from_meta(self::module_meta)?; - m.ty::()?.docs(["The tuple type."])?; + m.ty::()?.docs(docstring! { + /// The tuple type. + })?; m.function_meta(len)?; m.function_meta(is_empty)?; m.function_meta(get)?; diff --git a/crates/rune/src/runtime/const_value.rs b/crates/rune/src/runtime/const_value.rs index f9a02e41f..92775afb5 100644 --- a/crates/rune/src/runtime/const_value.rs +++ b/crates/rune/src/runtime/const_value.rs @@ -50,11 +50,11 @@ impl IntoConstValue for &ConstValue { /// # Examples /// /// ``` -/// let value = rune::to_const_value((1u32, 2u64))?; -/// let (a, b) = rune::from_const_value::<(1u32, 2u64)>(value)?; +/// let value = rune::to_const_value((i32::MIN, u64::MAX))?; +/// let (a, b) = rune::from_const_value::<(i32, u64)>(value)?; /// -/// assert_eq!(a, 1); -/// assert_eq!(b, 2); +/// assert_eq!(a, i32::MIN); +/// assert_eq!(b, u64::MAX); /// # Ok::<_, rune::support::Error>(()) /// ``` pub fn from_const_value(value: impl IntoConstValue) -> Result @@ -69,11 +69,11 @@ where /// # Examples /// /// ``` -/// let value = rune::to_const_value((1u32, 2u64))?; -/// let (a, b) = rune::from_const_value::<(1u32, 2u64)>(value)?; +/// let value = rune::to_const_value((i32::MIN, u64::MAX))?; +/// let (a, b) = rune::from_const_value::<(i32, u64)>(value)?; /// -/// assert_eq!(a, 1); -/// assert_eq!(b, 2); +/// assert_eq!(a, i32::MIN); +/// assert_eq!(b, u64::MAX); /// # Ok::<_, rune::support::Error>(()) /// ``` pub fn to_const_value(value: impl ToConstValue) -> Result { diff --git a/crates/rune/src/runtime/inst.rs b/crates/rune/src/runtime/inst.rs index 6569a6d27..aaef48aea 100644 --- a/crates/rune/src/runtime/inst.rs +++ b/crates/rune/src/runtime/inst.rs @@ -1234,7 +1234,7 @@ impl Output { /// let mut number = 0; /// /// for value in vm_try!(stack.slice_at(addr, args)) { - /// number += vm_try!(value.as_integer()); + /// number += vm_try!(value.as_integer::()); /// } /// /// out.store(stack, number); diff --git a/crates/rune/src/runtime/stack.rs b/crates/rune/src/runtime/stack.rs index f07ce9b80..fcc070391 100644 --- a/crates/rune/src/runtime/stack.rs +++ b/crates/rune/src/runtime/stack.rs @@ -69,7 +69,7 @@ pub trait Memory { /// let mut number = 0; /// /// for value in vm_try!(stack.slice_at(addr, args)) { - /// number += vm_try!(value.as_integer()); + /// number += vm_try!(value.as_integer::()); /// } /// /// out.store(stack, number); @@ -108,7 +108,7 @@ pub trait Memory { /// /// fn add_one(stack: &mut dyn Memory, addr: InstAddress, args: usize, out: Output) -> VmResult<()> { /// let mut value = vm_try!(stack.at_mut(addr)); - /// let number = vm_try!(value.as_integer()); + /// let number = vm_try!(value.as_integer::()); /// *value = vm_try!(rune::to_value(number + 1)); /// out.store(stack, ()); /// VmResult::Ok(()) @@ -226,7 +226,7 @@ impl Stack { /// use rune::runtime::{Output, Stack, VmResult, InstAddress}; /// /// fn add_one(stack: &mut Stack, addr: InstAddress, args: usize, out: Output) -> VmResult<()> { - /// let value = vm_try!(vm_try!(stack.at(addr)).as_integer()); + /// let value = vm_try!(vm_try!(stack.at(addr)).as_integer::()); /// out.store(stack, value + 1); /// VmResult::Ok(()) /// } @@ -249,7 +249,7 @@ impl Stack { /// /// fn add_one(stack: &mut Stack, addr: InstAddress, args: usize, out: Output) -> VmResult<()> { /// let mut value = vm_try!(stack.at_mut(addr)); - /// let number = vm_try!(value.as_integer()); + /// let number = vm_try!(value.as_integer::()); /// *value = vm_try!(rune::to_value(number + 1)); /// out.store(stack, ()); /// VmResult::Ok(()) @@ -275,7 +275,7 @@ impl Stack { /// let mut number = 0; /// /// for value in vm_try!(stack.slice_at(addr, args)) { - /// number += vm_try!(value.as_integer()); + /// number += vm_try!(value.as_integer::()); /// } /// /// out.store(stack, number); @@ -303,7 +303,7 @@ impl Stack { /// /// fn sum(stack: &mut Stack, addr: InstAddress, args: usize, out: Output) -> VmResult<()> { /// for value in vm_try!(stack.slice_at_mut(addr, args)) { - /// let number = vm_try!(value.as_integer()); + /// let number = vm_try!(value.as_integer::()); /// *value = vm_try!(rune::to_value(number + 1)); /// } /// diff --git a/crates/rune/src/runtime/value.rs b/crates/rune/src/runtime/value.rs index b302072e1..4687ea3bf 100644 --- a/crates/rune/src/runtime/value.rs +++ b/crates/rune/src/runtime/value.rs @@ -1598,8 +1598,8 @@ impl Value { /// ``` /// let value = rune::to_value(u32::MAX)?; /// - /// assert_eq!(value.impl_integer::()?, u32::MAX as u64); - /// assert!(value.impl_integer::().is_err()); + /// assert_eq!(value.as_integer::()?, u32::MAX as u64); + /// assert!(value.as_integer::().is_err()); /// /// # Ok::<(), rune::support::Error>(()) /// ``` diff --git a/crates/rune/src/tests/quote.rs b/crates/rune/src/tests/quote.rs index de9c75bf1..4560ea2cf 100644 --- a/crates/rune/src/tests/quote.rs +++ b/crates/rune/src/tests/quote.rs @@ -132,7 +132,7 @@ fn test_synthetic() -> Result<()> { assert_quote!(cx, [Number(NumberSource::Synthetic(..))], quote!(0)); assert_quote!(cx, [Number(NumberSource::Synthetic(..))], quote!(42.0)); assert_quote!(cx, [Char(CopySource::Inline('a'))], quote!('a')); - assert_quote!(cx, [Byte(CopySource::Inline(b'a'))], quote!(b'a')); + assert_quote!(cx, [Number(NumberSource::Synthetic(..))], quote!(b'a')); Ok(()) })?; diff --git a/crates/rune/src/tests/type_name_native.rs b/crates/rune/src/tests/type_name_native.rs index ce5570907..66a55d160 100644 --- a/crates/rune/src/tests/type_name_native.rs +++ b/crates/rune/src/tests/type_name_native.rs @@ -75,22 +75,6 @@ fn test_field_fn() { (t1, ), String => pub fn main(val) { std::any::type_name_of_val(val.x) } }, - "::std::i64" + "::std::u64" ); } - -// Not sure what the right return should be here - it returns the field name, but it probably should return ::std::i64? -// #[test] -// fn test_field_fn_ref() { -// assert_eq!( -// rune_n! { -// make_native_module().expect("failed making native module"), -// (), -// String => -// pub fn main() { -// std::any::type_name_of_val(native_crate::NativeStruct::x) -// } -// }, -// "::std::i64" -// ); -// } diff --git a/crates/rune/src/tests/vm_arithmetic.rs b/crates/rune/src/tests/vm_arithmetic.rs index e4121d7f3..e1bc1be82 100644 --- a/crates/rune/src/tests/vm_arithmetic.rs +++ b/crates/rune/src/tests/vm_arithmetic.rs @@ -140,6 +140,17 @@ fn test_bit_ops_i64() { error_test!(0b1 >> 64 = Underflow); } +#[test] +fn test_bit_ops_u64() { + op_tests!(u64, 0b1100 & 0b0110 = 0b1100 & 0b0110); + op_tests!(u64, 0b1100 ^ 0b0110 = 0b1100 ^ 0b0110); + op_tests!(u64, 0b1100 | 0b0110 = 0b1100 | 0b0110); + op_tests!(u64, 0b1100 << 2 = 0b1100 << 2); + op_tests!(u64, 0b1100 >> 2 = 0b1100 >> 2); + error_test!(0b1 << 64 = Overflow); + error_test!(0b1 >> 64 = Underflow); +} + #[test] fn test_bit_ops_u8() { op_tests!(u8, 0b1100u8 & 0b0110u8 = 0b1100u8 & 0b0110u8); @@ -147,28 +158,57 @@ fn test_bit_ops_u8() { op_tests!(u8, 0b1100u8 | 0b0110u8 = 0b1100u8 | 0b0110u8); op_tests!(u8, 0b1100u8 << 2 = 0b1100u8 << 2); op_tests!(u8, 0b1100u8 >> 2 = 0b1100u8 >> 2); - error_test!(0b1u8 << 8 = Overflow); - error_test!(0b1u8 >> 8 = Underflow); +} + +#[test] +fn test_bit_ops_i8() { + op_tests!(i8, 0b1100i8 & 0b0110i8 = 0b1100i8 & 0b0110i8); + op_tests!(i8, 0b1100i8 ^ 0b0110i8 = 0b1100i8 ^ 0b0110i8); + op_tests!(i8, 0b1100i8 | 0b0110i8 = 0b1100i8 | 0b0110i8); + op_tests!(i8, 0b1100i8 << 2 = 0b1100i8 << 2); + op_tests!(i8, 0b1100i8 >> 2 = 0b1100i8 >> 2); } #[test] fn test_bitwise_not_i64() { let out: i64 = rune!( pub fn main() { - let a = 0b10100; + let a = 0b10100i64; !a } ); - assert_eq!(out, !0b10100); + assert_eq!(out, !0b10100i64); +} + +#[test] +fn test_bitwise_not_u64() { + let out: u64 = rune!( + pub fn main() { + let a = 0b10100u64; + !a + } + ); + assert_eq!(out, !0b10100u64); } #[test] fn test_bitwise_not_u8() { - let out: u8 = rune!( + let out: u64 = rune!( pub fn main() { let a = 0b10100u8; !a } ); - assert_eq!(out, !0b10100u8); + assert_eq!(out, !0b10100u64); +} + +#[test] +fn test_bitwise_not_i8() { + let out: i64 = rune!( + pub fn main() { + let a = 0b10100i8; + !a + } + ); + assert_eq!(out, !0b10100i64); } diff --git a/crates/rune/tests/general.rn b/crates/rune/tests/general.rn index f1031fd4c..30abbb2b9 100644 --- a/crates/rune/tests/general.rn +++ b/crates/rune/tests/general.rn @@ -151,7 +151,7 @@ fn test_is() { assert!({} is Tuple); assert!(#{} is Object); assert!(() is Tuple); - assert!((1,) is Tuple); + assert!((1, ) is Tuple); assert!((1, 2) is Tuple); assert!(foo() is Tuple); assert!(true is bool); diff --git a/scripts/book/types/types.rn b/scripts/book/types/types.rn index 9c0dd1f87..5da95e0dd 100644 --- a/scripts/book/types/types.rn +++ b/scripts/book/types/types.rn @@ -2,7 +2,7 @@ assert!(() is Tuple, "tuples should be tuples"); assert!((1, 2) is Tuple, "tuples should be tuples"); assert!(true is bool, "bools should be bools"); assert!('a' is char, "chars should be chars"); -assert!(b'a' is u8, "bytes should be bytes"); +assert!(b'a' is u64, "bytes should be unsigned integers"); assert!(42 is i64, "integers should be integers"); assert!(42.1 is f64, "floats should be floats"); assert!("hello" is String, "strings should be strings");