Skip to content

Commit

Permalink
Clean up naming (#516)
Browse files Browse the repository at this point in the history
* Rework module compat
* Cleaner traits and structs
* Rename functions to be less obscure
  • Loading branch information
udoprog authored May 12, 2023
1 parent 5d27a92 commit 921051a
Show file tree
Hide file tree
Showing 56 changed files with 582 additions and 646 deletions.
4 changes: 2 additions & 2 deletions book/src/field_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ are:
| [`Protocol::SHR_ASSIGN`] | `#[rune(shr_assign)]` | The `>>=` operation. |
| [`Protocol::REM_ASSIGN`] | `#[rune(rem_assign)]` | The `%=` operation. |

The manual way to register these functions is to use the new `Module::field_fn`
The manual way to register these functions is to use the new `Module::field_function`
function. This clearly showcases that there's no relationship between the field
used and the function registered:

Expand All @@ -67,7 +67,7 @@ impl External {
}
let mut module = Module::new();
module.field_fn(Protocol::GET, "field", External::field_get)?;
module.field_function(Protocol::GET, "field", External::field_get)?;
```

Would allow for this in Rune:
Expand Down
9 changes: 4 additions & 5 deletions book/src/instance_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ be stabilized and documented in a future release.
## Defining instance functions in Rust

Native instance functions are added to a runtime environment using the
[`Module::inst_fn`] and [`Module::async_inst_fn`] functions. The type is
identified as the first argument of the instance function, and must be a type
registered in the module using [`Module::ty`].
[`Module::associated_function`] function. The type is identified as the first
argument of the instance function, and must be a type registered in the module
using [`Module::ty`].

```rust,noplaypen
{{#include ../../examples/examples/custom_instance_fn.rs}}
Expand All @@ -62,7 +62,6 @@ output: 11
For more examples on how modules can be used you can have a look at the source
for the [`rune-modules`] crate.

[`Module::inst_fn`]: https://docs.rs/rune/0/rune/struct.Module.html#method.inst_fn
[`Module::async_inst_fn`]: https://docs.rs/rune/0/rune/struct.Module.html#method.async_inst_fn
[`Module::associated_function`]: https://docs.rs/rune/0/rune/struct.Module.html#method.associated_function
[`Module::ty`]: https://docs.rs/rune/0/rune/struct.Module.html#method.ty
[`rune-modules`]: https://github.com/rune-rs/rune/tree/main/crates/rune-modules
2 changes: 1 addition & 1 deletion book/src/template_literals.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl StatusCode {
pub fn module() -> Result<Module, ContextError> {
let mut module = Module::new(["http"]);
module.inst_fn(Protocol::STRING_DISPLAY, StatusCode::display)?;
module.associated_function(Protocol::STRING_DISPLAY, StatusCode::display)?;
Ok(module)
}
```
Expand Down
6 changes: 3 additions & 3 deletions crates/rune-core/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl Hash {
/// Construct a hash to an instance function, where the instance is a
/// pre-determined type.
#[inline]
pub fn instance_function<N>(type_hash: Hash, name: N) -> Self
pub fn associated_function<N>(type_hash: Hash, name: N) -> Self
where
N: IntoHash,
{
Expand All @@ -114,7 +114,7 @@ impl Hash {

/// Construct a hash corresponding to a field function.
#[inline]
pub fn field_fn<N>(protocol: Protocol, type_hash: Hash, name: N) -> Self
pub fn field_function<N>(protocol: Protocol, type_hash: Hash, name: N) -> Self
where
N: IntoHash,
{
Expand All @@ -123,7 +123,7 @@ impl Hash {

/// Construct an index function.
#[inline]
pub fn index_fn(protocol: Protocol, type_hash: Hash, index: Hash) -> Self {
pub fn index_function(protocol: Protocol, type_hash: Hash, index: Hash) -> Self {
Self(INDEX_FUNCTION_HASH ^ ((type_hash.0 ^ protocol.hash.0) ^ index.0))
}

Expand Down
6 changes: 3 additions & 3 deletions crates/rune-macros/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ fn expand_enum_install_with(
}

let is_variant = quote! {
module.inst_fn(#protocol::IS_VARIANT, |this: &Self, index: usize| {
module.associated_function(#protocol::IS_VARIANT, |this: &Self, index: usize| {
match (this, index) {
#(#is_variant,)*
_ => false,
Expand All @@ -395,7 +395,7 @@ fn expand_enum_install_with(

for (field, matches) in field_fns {
installers.push(quote! {
module.field_fn(#protocol::GET, #field, |this: &Self| {
module.field_function(#protocol::GET, #field, |this: &Self| {
match this {
#(#matches,)*
_ => return #vm_result::__rune_macros__unsupported_object_field_get(<Self as #type_of>::type_info()),
Expand All @@ -406,7 +406,7 @@ fn expand_enum_install_with(

for (index, matches) in index_fns {
installers.push(quote! {
module.index_fn(#protocol::GET, #index, |this: &Self| {
module.index_function(#protocol::GET, #index, |this: &Self| {
match this {
#(#matches,)*
_ => return #vm_result::__rune_macros__unsupported_tuple_index_get(<Self as #type_of>::type_info()),
Expand Down
40 changes: 29 additions & 11 deletions crates/rune-macros/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ impl Context {
GenerateTarget::Named { field_ident, field_name } => {
if let Some(custom) = &protocol.custom {
quote_spanned! { field.span() =>
module.field_fn(#protocol_field, #field_name, #custom)?;
module.field_function(#protocol_field, #field_name, #custom)?;
}
} else {
quote_spanned! { field.span() =>
module.field_fn(#protocol_field, #field_name, |s: &mut Self, value: #ty| {
module.field_function(#protocol_field, #field_name, |s: &mut Self, value: #ty| {
s.#field_ident $op value;
})?;
}
Expand All @@ -190,11 +190,11 @@ impl Context {
GenerateTarget::Numbered { field_index } => {
if let Some(custom) = &protocol.custom {
quote_spanned! { field.span() =>
module.index_fn(#protocol_field, #field_index, #custom)?;
module.index_function(#protocol_field, #field_index, #custom)?;
}
} else {
quote_spanned! { field.span() =>
module.index_fn(#protocol_field, #field_index, |s: &mut Self, value: #ty| {
module.index_function(#protocol_field, #field_index, |s: &mut Self, value: #ty| {
s.#field_index $op value;
})?;
}
Expand Down Expand Up @@ -271,7 +271,7 @@ impl Context {
let protocol = g.tokens.protocol(PROTOCOL_GET);

quote_spanned! { g.field.span() =>
module.field_fn(#protocol, #field_name, |s: &Self| #access)?;
module.field_function(#protocol, #field_name, |s: &Self| #access)?;
}
}
GenerateTarget::Numbered { field_index } => {
Expand All @@ -284,7 +284,7 @@ impl Context {
let protocol = g.tokens.protocol(PROTOCOL_GET);

quote_spanned! { g.field.span() =>
module.index_fn(#protocol, #field_index, |s: &Self| #access)?;
module.index_function(#protocol, #field_index, |s: &Self| #access)?;
}
}
}
Expand All @@ -305,14 +305,14 @@ impl Context {
match target {
GenerateTarget::Named { field_ident, field_name } => {
quote_spanned! { g.field.span() =>
module.field_fn(#protocol, #field_name, |s: &mut Self, value: #ty| {
module.field_function(#protocol, #field_name, |s: &mut Self, value: #ty| {
s.#field_ident = value;
})?;
}
}
GenerateTarget::Numbered { field_index } => {
quote_spanned! { g.field.span() =>
module.index_fn(#protocol, #field_index, |s: &mut Self, value: #ty| {
module.index_function(#protocol, #field_index, |s: &mut Self, value: #ty| {
s.#field_index = value;
})?;
}
Expand Down Expand Up @@ -436,11 +436,11 @@ impl Context {
} else if meta.path == MODULE {
// Parse `#[rune(module = <path>)]`
meta.input.parse::<Token![=]>()?;
attr.module = Some(syn::Path::parse_mod_style(meta.input)?);
attr.module = Some(parse_path_compat(meta.input)?);
} else if meta.path == INSTALL_WITH {
// Parse `#[rune(install_with = <path>)]`
meta.input.parse::<Token![=]>()?;
attr.install_with = Some(syn::Path::parse_mod_style(meta.input)?);
attr.install_with = Some(parse_path_compat(meta.input)?);
} else {
return Err(syn::Error::new_spanned(
&meta.path,
Expand Down Expand Up @@ -518,7 +518,7 @@ impl Context {
};

input.parse::<Token![=]>()?;
Ok(Some(syn::Path::parse_mod_style(input)?))
Ok(Some(parse_path_compat(input)?))
}

/// Build an inner spanned decoder from an iterator.
Expand Down Expand Up @@ -706,6 +706,24 @@ impl Context {
}
}

fn parse_path_compat(input: ParseStream<'_>) -> syn::Result<syn::Path> {
if input.peek(syn::LitStr) {
let path = input
.parse::<syn::LitStr>()?
.parse_with(syn::Path::parse_mod_style)?;

return Err(syn::Error::new_spanned(
&path,
format_args!(
"String literals are no longer supported here, use a path like `{}`",
path.to_token_stream()
),
));
}

syn::Path::parse_mod_style(input)
}

fn path<const N: usize>(base: &syn::Path, path: [&'static str; N]) -> syn::Path {
let mut base = base.clone();

Expand Down
21 changes: 8 additions & 13 deletions crates/rune-macros/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,6 @@ impl Function {
(false, name, arguments)
};

let function = match (instance, self_type.is_some(), self.sig.asyncness.is_some()) {
(true, _, false) => "instance",
(true, _, true) => "async_instance",
(_, true, false) => "function_with",
(_, true, true) => "async_function_with",
(_, _, false) => "function",
(_, _, true) => "async_function",
};

if !instance && self_type.is_none() {
name = {
let mut out = syn::ExprArray {
Expand Down Expand Up @@ -330,6 +321,8 @@ impl Function {
}
}

let function = if instance { "instance" } else { "function" };

let meta_kind = syn::Ident::new(function, self.sig.span());
let mut stream = TokenStream::new();

Expand All @@ -348,10 +341,12 @@ impl Function {
let arguments = &self.arguments;
let docs = &self.docs;

let meta_kind = if let Some(self_type) = self_type {
quote!(#meta_kind::<#self_type, _, _, _>)
let build_with = if instance {
None
} else if let Some(self_type) = self_type {
Some(quote!(.build_associated::<#self_type>()))
} else {
meta_kind.into_token_stream()
Some(quote!(.build()))
};

let meta_vis = &self.vis;
Expand All @@ -364,7 +359,7 @@ impl Function {
#attr
#meta_vis fn #meta_fn() -> rune::__private::FunctionMetaData {
rune::__private::FunctionMetaData {
kind: rune::__private::FunctionMetaKind::#meta_kind(#name, #real_fn_path),
kind: rune::__private::FunctionMetaKind::#meta_kind(#name, #real_fn_path)#build_with,
name: #name_string,
docs: &#docs[..],
arguments: &#arguments[..],
Expand Down
2 changes: 1 addition & 1 deletion crates/rune-modules/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use rune::{Module, ContextError};
/// Construct the `fs` module.
pub fn module(_stdio: bool) -> Result<Module, ContextError> {
let mut module = Module::with_crate("fs");
module.async_function(["read_to_string"], read_to_string)?;
module.function(["read_to_string"], read_to_string)?;
Ok(module)
}

Expand Down
4 changes: 2 additions & 2 deletions crates/rune-modules/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ pub fn module(_stdio: bool) -> Result<Module, ContextError> {
module.function_meta(RequestBuilder::header)?;
module.function_meta(RequestBuilder::body_bytes)?;

module.inst_fn(Protocol::STRING_DISPLAY, Error::display)?;
module.inst_fn(Protocol::STRING_DISPLAY, StatusCode::display)?;
module.associated_function(Protocol::STRING_DISPLAY, Error::display)?;
module.associated_function(Protocol::STRING_DISPLAY, StatusCode::display)?;
Ok(module)
}

Expand Down
12 changes: 6 additions & 6 deletions crates/rune-modules/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ pub fn module(_stdio: bool) -> Result<Module, ContextError> {
module.ty::<Output>()?;

module.function(["Command", "new"], Command::new)?;
module.inst_fn("spawn", Command::spawn)?;
module.inst_fn("arg", Command::arg)?;
module.inst_fn("args", Command::args)?;
module.async_inst_fn("wait_with_output", Child::wait_with_output)?;
module.inst_fn(Protocol::STRING_DISPLAY, ExitStatus::display)?;
module.inst_fn("code", ExitStatus::code)?;
module.associated_function("spawn", Command::spawn)?;
module.associated_function("arg", Command::arg)?;
module.associated_function("args", Command::args)?;
module.associated_function("wait_with_output", Child::wait_with_output)?;
module.associated_function(Protocol::STRING_DISPLAY, ExitStatus::display)?;
module.associated_function("code", ExitStatus::code)?;
Ok(module)
}

Expand Down
8 changes: 4 additions & 4 deletions crates/rune-modules/src/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ pub fn module(_stdio: bool) -> Result<Module, ContextError> {
module.ty::<WyRand>()?;
module.function(["WyRand", "new"], WyRand::new)?;
module.function(["WyRand", "new_seed"], WyRand::new_seed)?;
module.inst_fn("int", WyRand::int)?;
module.inst_fn("int_range", WyRand::int_range)?;
module.associated_function("int", WyRand::int)?;
module.associated_function("int_range", WyRand::int_range)?;

module.ty::<Pcg64>()?;
module.function(["Pcg64", "new"], Pcg64::new)?;
module.function(["Pcg64", "new_seed"], Pcg64::new_seed)?;
module.inst_fn("int", Pcg64::int)?;
module.inst_fn("int_range", Pcg64::int_range)?;
module.associated_function("int", Pcg64::int)?;
module.associated_function("int_range", Pcg64::int_range)?;

module.function(["int"], int)?;
module.function(["int_range"], int_range)?;
Expand Down
4 changes: 2 additions & 2 deletions crates/rune-wasm/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ pub fn module() -> Result<Module, ContextError> {
let mut module = Module::with_crate("http");
module.ty::<Response>()?;
module.ty::<Error>()?;
module.async_function(["get"], get)?;
module.async_inst_fn("text", Response::text)?;
module.function(["get"], get)?;
module.associated_function("text", Response::text)?;
Ok(module)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rune-wasm/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn module() -> Result<Module, ContextError> {
let mut module = Module::with_crate("time");
module.ty::<Duration>()?;
module.function(["Duration", "from_secs"], Duration::from_secs)?;
module.async_function(["delay_for"], delay_for)?;
module.function(["delay_for"], delay_for)?;
Ok(module)
}

Expand Down
1 change: 1 addition & 0 deletions crates/rune/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/wip/
1 change: 1 addition & 0 deletions crates/rune/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ tokio = { version = "1.28.1", features = ["full"] }
static_assertions = "1.1.0"
checkers = "0.6.3"
futures-executor = "0.3.28"
trybuild = "1.0.80"

[package.metadata.docs.rs]
all-features = true
Expand Down
8 changes: 4 additions & 4 deletions crates/rune/src/compile/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ impl Context {
}

self.constants.insert(
Hash::instance_function(ty.hash, Protocol::INTO_TYPE_NAME),
Hash::associated_function(ty.hash, Protocol::INTO_TYPE_NAME),
ConstValue::String(ty.item.to_string()),
);

Expand All @@ -556,7 +556,7 @@ impl Context {
let hash = Hash::type_hash(&item);

self.constants.insert(
Hash::instance_function(hash, Protocol::INTO_TYPE_NAME),
Hash::associated_function(hash, Protocol::INTO_TYPE_NAME),
ConstValue::String(item.to_string()),
);

Expand Down Expand Up @@ -679,7 +679,7 @@ impl Context {
.with_function_parameters(assoc.name.function_parameters);

self.constants.insert(
Hash::instance_function(hash, Protocol::INTO_TYPE_NAME),
Hash::associated_function(hash, Protocol::INTO_TYPE_NAME),
ConstValue::String(item.to_string()),
);

Expand Down Expand Up @@ -758,7 +758,7 @@ impl Context {
})?;

self.constants.insert(
Hash::instance_function(hash, Protocol::INTO_TYPE_NAME),
Hash::associated_function(hash, Protocol::INTO_TYPE_NAME),
ConstValue::String(item.to_string()),
);

Expand Down
Loading

0 comments on commit 921051a

Please sign in to comment.