Skip to content

Commit 4e995e8

Browse files
committed
Simplify implementations
1 parent 3344a95 commit 4e995e8

File tree

9 files changed

+229
-343
lines changed

9 files changed

+229
-343
lines changed

crates/rune/src/modules/ops.rs

Lines changed: 3 additions & 259 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use core::cmp::Ordering;
44

55
use crate as rune;
66
use crate::runtime::{
7-
ControlFlow, EnvProtocolCaller, Function, Generator, GeneratorState, Iterator, Protocol, Range,
7+
ControlFlow, EnvProtocolCaller, Function, Generator, GeneratorState, Iterator, Range,
88
RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive, Value, Vm, VmResult,
99
};
1010
use crate::{ContextError, Module};
@@ -16,44 +16,6 @@ pub fn module() -> Result<Module, ContextError> {
1616

1717
{
1818
m.ty::<RangeFrom>()?;
19-
m.type_meta::<RangeFrom>()?
20-
.make_named_struct(&["start"])?
21-
.docs([
22-
"Type for a from range expression `start..`.",
23-
"",
24-
"# Examples",
25-
"",
26-
"```rune",
27-
"let range = 0..;",
28-
"",
29-
"assert!(!range.contains(-10));",
30-
"assert!(range.contains(5));",
31-
"assert!(range.contains(10));",
32-
"assert!(range.contains(20));",
33-
"",
34-
"assert!(range is std::ops::RangeFrom);",
35-
"```",
36-
"",
37-
"Ranges can contain any type:",
38-
"",
39-
"```rune",
40-
"let range = 'a'..;",
41-
"assert_eq!(range.start, 'a');",
42-
"range.start = 'b';",
43-
"assert_eq!(range.start, 'b');",
44-
"```",
45-
"",
46-
"Certain ranges can be used as iterators:",
47-
"",
48-
"```rune",
49-
"let range = 'a'..;",
50-
"assert_eq!(range.iter().take(5).collect::<Vec>(), ['a', 'b', 'c', 'd', 'e']);",
51-
"```",
52-
]);
53-
m.field_function(Protocol::GET, "start", |r: &RangeFrom| r.start.clone())?;
54-
m.field_function(Protocol::SET, "start", |r: &mut RangeFrom, value: Value| {
55-
r.start = value;
56-
})?;
5719
m.function_meta(RangeFrom::iter__meta)?;
5820
m.function_meta(RangeFrom::contains__meta)?;
5921
m.function_meta(RangeFrom::into_iter__meta)?;
@@ -65,80 +27,11 @@ pub fn module() -> Result<Module, ContextError> {
6527

6628
{
6729
m.ty::<RangeFull>()?;
68-
m.type_meta::<RangeFull>()?.make_empty_struct()?.docs([
69-
"Type for a full range expression `..`.",
70-
"",
71-
"# Examples",
72-
"",
73-
"```rune",
74-
"let range = ..;",
75-
"",
76-
"assert!(range.contains(-10));",
77-
"assert!(range.contains(5));",
78-
"assert!(range.contains(10));",
79-
"assert!(range.contains(20));",
80-
"",
81-
"assert!(range is std::ops::RangeFull);",
82-
"```",
83-
]);
8430
m.function_meta(RangeFull::contains)?;
8531
}
8632

8733
{
8834
m.ty::<RangeInclusive>()?;
89-
m.type_meta::<RangeInclusive>()?
90-
.make_named_struct(&["start", "end"])?
91-
.docs([
92-
"Type for an inclusive range expression `start..=end`.",
93-
"",
94-
"# Examples",
95-
"",
96-
"```rune",
97-
"let range = 0..=10;",
98-
"",
99-
"assert!(!range.contains(-10));",
100-
"assert!(range.contains(5));",
101-
"assert!(range.contains(10));",
102-
"assert!(!range.contains(20));",
103-
"",
104-
"assert!(range is std::ops::RangeInclusive);",
105-
"```",
106-
"",
107-
"Ranges can contain any type:",
108-
"",
109-
"```rune",
110-
"let range = 'a'..='f';",
111-
"assert_eq!(range.start, 'a');",
112-
"range.start = 'b';",
113-
"assert_eq!(range.start, 'b');",
114-
"assert_eq!(range.end, 'f');",
115-
"range.end = 'g';",
116-
"assert_eq!(range.end, 'g');",
117-
"```",
118-
"",
119-
"Certain ranges can be used as iterators:",
120-
"",
121-
"```rune",
122-
"let range = 'a'..='e';",
123-
"assert_eq!(range.iter().collect::<Vec>(), ['a', 'b', 'c', 'd', 'e']);",
124-
"```",
125-
]);
126-
m.field_function(Protocol::GET, "start", |r: &RangeInclusive| r.start.clone())?;
127-
m.field_function(
128-
Protocol::SET,
129-
"start",
130-
|r: &mut RangeInclusive, value: Value| {
131-
r.start = value;
132-
},
133-
)?;
134-
m.field_function(Protocol::GET, "end", |r: &RangeInclusive| r.end.clone())?;
135-
m.field_function(
136-
Protocol::SET,
137-
"end",
138-
|r: &mut RangeInclusive, value: Value| {
139-
r.end = value;
140-
},
141-
)?;
14235
m.function_meta(RangeInclusive::iter__meta)?;
14336
m.function_meta(RangeInclusive::contains__meta)?;
14437
m.function_meta(RangeInclusive::into_iter__meta)?;
@@ -150,40 +43,6 @@ pub fn module() -> Result<Module, ContextError> {
15043

15144
{
15245
m.ty::<RangeToInclusive>()?;
153-
m.type_meta::<RangeToInclusive>()?
154-
.make_named_struct(&["end"])?
155-
.docs([
156-
"Type for an inclusive range expression `..=end`.",
157-
"",
158-
"# Examples",
159-
"",
160-
"```rune",
161-
"let range = ..=10;",
162-
"assert!(range.contains(-10));",
163-
"assert!(range.contains(5));",
164-
"assert!(range.contains(10));",
165-
"assert!(!range.contains(20));",
166-
"",
167-
"assert!(range is std::ops::RangeToInclusive);",
168-
"```",
169-
"",
170-
"Ranges can contain any type:",
171-
"",
172-
"```rune",
173-
"let range = ..='f';",
174-
"assert_eq!(range.end, 'f');",
175-
"range.end = 'g';",
176-
"assert_eq!(range.end, 'g');",
177-
"```",
178-
]);
179-
m.field_function(Protocol::GET, "end", |r: &RangeToInclusive| r.end.clone())?;
180-
m.field_function(
181-
Protocol::SET,
182-
"end",
183-
|r: &mut RangeToInclusive, value: Value| {
184-
r.end = value;
185-
},
186-
)?;
18746
m.function_meta(RangeToInclusive::contains__meta)?;
18847
m.function_meta(RangeToInclusive::partial_eq__meta)?;
18948
m.function_meta(RangeToInclusive::eq__meta)?;
@@ -193,36 +52,6 @@ pub fn module() -> Result<Module, ContextError> {
19352

19453
{
19554
m.ty::<RangeTo>()?;
196-
m.type_meta::<RangeTo>()?
197-
.make_named_struct(&["end"])?
198-
.docs([
199-
"Type for an inclusive range expression `..end`.",
200-
"",
201-
"# Examples",
202-
"",
203-
"```rune",
204-
"let range = ..10;",
205-
"assert!(range.contains(-10));",
206-
"assert!(range.contains(5));",
207-
"assert!(!range.contains(10));",
208-
"assert!(!range.contains(20));",
209-
"",
210-
"assert!(range is std::ops::RangeTo);",
211-
"```",
212-
"",
213-
"Ranges can contain any type:",
214-
"",
215-
"```rune",
216-
"let range = ..'f';",
217-
"assert_eq!(range.end, 'f');",
218-
"range.end = 'g';",
219-
"assert_eq!(range.end, 'g');",
220-
"```",
221-
]);
222-
m.field_function(Protocol::GET, "end", |r: &RangeTo| r.end.clone())?;
223-
m.field_function(Protocol::SET, "end", |r: &mut RangeTo, value: Value| {
224-
r.end = value;
225-
})?;
22655
m.function_meta(RangeTo::contains__meta)?;
22756
m.function_meta(RangeTo::partial_eq__meta)?;
22857
m.function_meta(RangeTo::eq__meta)?;
@@ -232,50 +61,6 @@ pub fn module() -> Result<Module, ContextError> {
23261

23362
{
23463
m.ty::<Range>()?;
235-
m.type_meta::<Range>()?
236-
.make_named_struct(&["start", "end"])?
237-
.docs([
238-
"Type for a range expression `start..end`.",
239-
"",
240-
"# Examples",
241-
"",
242-
"```rune",
243-
"let range = 0..10;",
244-
"assert!(!range.contains(-10));",
245-
"assert!(range.contains(5));",
246-
"assert!(!range.contains(10));",
247-
"assert!(!range.contains(20));",
248-
"",
249-
"assert!(range is std::ops::Range);",
250-
"```",
251-
"",
252-
"Ranges can contain any type:",
253-
"",
254-
"```rune",
255-
"let range = 'a'..'f';",
256-
"assert_eq!(range.start, 'a');",
257-
"range.start = 'b';",
258-
"assert_eq!(range.start, 'b');",
259-
"assert_eq!(range.end, 'f');",
260-
"range.end = 'g';",
261-
"assert_eq!(range.end, 'g');",
262-
"```",
263-
"",
264-
"Certain ranges can be used as iterators:",
265-
"",
266-
"```rune",
267-
"let range = 'a'..'e';",
268-
"assert_eq!(range.iter().collect::<Vec>(), ['a', 'b', 'c', 'd']);",
269-
"```",
270-
]);
271-
m.field_function(Protocol::GET, "start", |r: &Range| r.start.clone())?;
272-
m.field_function(Protocol::SET, "start", |r: &mut Range, value: Value| {
273-
r.start = value;
274-
})?;
275-
m.field_function(Protocol::GET, "end", |r: &Range| r.end.clone())?;
276-
m.field_function(Protocol::SET, "end", |r: &mut Range, value: Value| {
277-
r.end = value;
278-
})?;
27964
m.function_meta(Range::iter__meta)?;
28065
m.function_meta(Range::into_iter__meta)?;
28166
m.function_meta(Range::contains__meta)?;
@@ -286,51 +71,10 @@ pub fn module() -> Result<Module, ContextError> {
28671
}
28772

28873
{
289-
m.ty::<ControlFlow>()?.docs([
290-
" Used to tell an operation whether it should exit early or go on as usual.",
291-
"",
292-
" This acts as the basis of the [`TRY`] protocol in Rune.",
293-
"",
294-
" [`TRY`]: crate::Protocol::TRY",
295-
"",
296-
"# Examples",
297-
"",
298-
"```rune",
299-
"use std::ops::ControlFlow;",
300-
"",
301-
"let c = ControlFlow::Continue(42);",
302-
"assert_eq!(c.0, 42);",
303-
"assert_eq!(c, ControlFlow::Continue(42));",
304-
"```",
305-
]);
74+
m.ty::<ControlFlow>()?;
30675
}
30776

308-
m.ty::<Function>()?.docs([
309-
"The type of a function in Rune.",
310-
"",
311-
"Functions can be called using call expression syntax, such as `<expr>()`.",
312-
"",
313-
"There are multiple different kind of things which can be coerced into a function in Rune:",
314-
"* Regular functions.",
315-
"* Closures (which might or might not capture their environment).",
316-
"* Built-in constructors for tuple types (tuple structs, tuple variants).",
317-
"",
318-
"# Examples",
319-
"",
320-
"```rune",
321-
"// Captures the constructor for the `Some(<value>)` tuple variant.",
322-
"let build_some = Some;",
323-
"assert_eq!(build_some(42), Some(42));",
324-
"",
325-
"fn build(value) {",
326-
" Some(value)",
327-
"}",
328-
"",
329-
"// Captures the function previously defined.",
330-
"let build_some = build;",
331-
"assert_eq!(build_some(42), Some(42));",
332-
"```",
333-
]);
77+
m.ty::<Function>()?;
33478

33579
{
33680
m.ty::<Generator<Vm>>()?.docs([

crates/rune/src/runtime/control_flow.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ use crate::Any;
1010
/// This acts as the basis of the [`TRY`] protocol in Rune.
1111
///
1212
/// [`TRY`]: crate::Protocol::TRY
13+
///
14+
/// # Examples
15+
///
16+
/// ```rune
17+
/// use std::ops::ControlFlow;
18+
///
19+
/// let c = ControlFlow::Continue(42);
20+
/// assert_eq!(c.0, 42);
21+
/// assert_eq!(c, ControlFlow::Continue(42));
22+
/// ```
1323
#[derive(Debug, Clone, Any)]
1424
#[rune(builtin)]
1525
pub enum ControlFlow {

crates/rune/src/runtime/function.rs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,44 @@ use core::future::Future;
44
use crate::no_std::prelude::*;
55
use crate::no_std::sync::Arc;
66

7-
use crate::compile::Named;
8-
use crate::module::{self, InstallWith};
7+
use crate as rune;
8+
use crate::module;
99
use crate::runtime::{
10-
Args, Call, ConstValue, FromValue, FunctionHandler, OwnedTuple, RawStr, Rtti, RuntimeContext,
11-
Stack, Unit, Value, VariantRtti, Vm, VmCall, VmErrorKind, VmHalt, VmResult,
10+
Args, Call, ConstValue, FromValue, FunctionHandler, OwnedTuple, Rtti, RuntimeContext, Stack,
11+
Unit, Value, VariantRtti, Vm, VmCall, VmErrorKind, VmHalt, VmResult,
1212
};
1313
use crate::shared::AssertSend;
14+
use crate::Any;
1415
use crate::Hash;
1516

16-
/// A callable non-sync function.
17+
/// The type of a function in Rune.
18+
///
19+
/// Functions can be called using call expression syntax, such as `<expr>()`.
20+
///
21+
/// There are multiple different kind of things which can be coerced into a
22+
/// function in Rune:
23+
/// * Regular functions.
24+
/// * Closures (which might or might not capture their environment).
25+
/// * Built-in constructors for tuple types (tuple structs, tuple variants).
26+
///
27+
/// # Examples
28+
///
29+
/// ```rune
30+
/// // Captures the constructor for the `Some(<value>)` tuple variant.
31+
/// let build_some = Some;
32+
/// assert_eq!(build_some(42), Some(42));
33+
///
34+
/// fn build(value) {
35+
/// Some(value)
36+
/// }
37+
///
38+
/// // Captures the function previously defined.
39+
/// let build_some = build;
40+
/// assert_eq!(build_some(42), Some(42));
41+
/// ```
42+
#[derive(Any)]
1743
#[repr(transparent)]
44+
#[rune(builtin)]
1845
pub struct Function(FunctionImpl<Value>);
1946

2047
impl Function {
@@ -909,12 +936,6 @@ impl FromValue for SyncFunction {
909936
}
910937
}
911938

912-
impl InstallWith for Function {}
913-
914-
impl Named for Function {
915-
const BASE_NAME: RawStr = RawStr::from_str("Function");
916-
}
917-
918939
from_value!(Function, into_function);
919940

920941
fn check_args(actual: usize, expected: usize) -> VmResult<()> {

0 commit comments

Comments
 (0)