Skip to content

Commit

Permalink
modules: Clean up modules registration so fmt works
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Oct 30, 2024
1 parent d176c5b commit acd9236
Show file tree
Hide file tree
Showing 20 changed files with 148 additions and 138 deletions.
14 changes: 0 additions & 14 deletions crates/rune-modules/src/core.rs

This file was deleted.

14 changes: 0 additions & 14 deletions crates/rune-modules/src/fmt.rs

This file was deleted.

14 changes: 0 additions & 14 deletions crates/rune-modules/src/io.rs

This file was deleted.

43 changes: 32 additions & 11 deletions crates/rune-modules/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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"},
Expand Down
14 changes: 0 additions & 14 deletions crates/rune-modules/src/macros.rs

This file was deleted.

8 changes: 4 additions & 4 deletions crates/rune-modules/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ struct Duration {

impl Duration {
/// Construct a duration from the given number of seconds.
///
///
/// # Examples
///
///
/// ```rune
/// use time::Duration;
///
Expand All @@ -65,9 +65,9 @@ impl Duration {
}

/// Sleep for the given [`Duration`].
///
///
/// # Examples
///
///
/// ```rune,no_run
/// use time::Duration;
///
Expand Down
42 changes: 27 additions & 15 deletions crates/rune/src/module/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<MyType>()?.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::<MyType>()?
/// .docs(docstring! {
/// /// Ten which looks like an associated constant.
/// });
/// # Ok::<_, rune::support::Error>(())
/// ```
pub fn constant<N, V>(&mut self, name: N, value: V) -> ModuleConstantBuilder<'_, N, V>
Expand Down Expand Up @@ -965,7 +972,7 @@ impl Module {
/// # Examples
///
/// ```
/// use rune::Module;
/// use rune::{docstring, Module};
///
/// fn add_ten(value: i64) -> i64 {
/// value + 10
Expand All @@ -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<String, DownloadError> { Ok(String::new()) }
///
/// #[derive(Any)]
Expand All @@ -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<F, A, N, K>(&mut self, name: N, f: F) -> ModuleFunctionBuilder<'_, F, A, N, K>
Expand Down Expand Up @@ -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::<i64>());
/// }
///
/// out.store(stack, number);
Expand All @@ -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>(())
/// ```
Expand Down
8 changes: 6 additions & 2 deletions crates/rune/src/module/module_constant_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Thing>()?.docs(["Ten which is an associated constant."]);
/// module.constant("TEN", 10)
/// .build_associated::<Thing>()?
/// .docs(docstring! {
/// /// Ten which is an associated constant.
/// });
/// # Ok::<_, rune::support::Error>(())
/// ```
pub fn build_associated<T>(self) -> Result<ItemMut<'a>, ContextError>
Expand Down
8 changes: 5 additions & 3 deletions crates/rune/src/modules/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -16,8 +16,10 @@ use crate::{ContextError, Module};
pub fn module() -> Result<Module, ContextError> {
let mut m = Module::from_meta(self::module_meta)?;

m.ty::<Type>()?
.docs(["Represents a type in the Rune type system."])?;
m.ty::<Type>()?.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)?;
Expand Down
5 changes: 3 additions & 2 deletions crates/rune/src/modules/ops/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ pub fn module() -> Result<Module, ContextError> {
}

{
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::<GeneratorState>(rune::item!(::std::cmp::PartialEq))?;
Expand Down
6 changes: 4 additions & 2 deletions crates/rune/src/modules/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -42,7 +42,9 @@ use crate::{ContextError, Module};
#[rune::module(::std::tuple)]
pub fn module() -> Result<Module, ContextError> {
let mut m = Module::from_meta(self::module_meta)?;
m.ty::<Tuple>()?.docs(["The tuple type."])?;
m.ty::<Tuple>()?.docs(docstring! {
/// The tuple type.
})?;
m.function_meta(len)?;
m.function_meta(is_empty)?;
m.function_meta(get)?;
Expand Down
16 changes: 8 additions & 8 deletions crates/rune/src/runtime/const_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(value: impl IntoConstValue) -> Result<T, RuntimeError>
Expand All @@ -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<ConstValue, RuntimeError> {
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/runtime/inst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i64>());
/// }
///
/// out.store(stack, number);
Expand Down
Loading

0 comments on commit acd9236

Please sign in to comment.