From 7034356618ed596adc96c64bb89380304f11ca4a Mon Sep 17 00:00:00 2001 From: John-John Tedro Date: Wed, 23 Aug 2023 13:40:46 +0200 Subject: [PATCH] Simplify implementations --- crates/rune/src/modules/ops.rs | 262 +----------------- crates/rune/src/runtime/control_flow.rs | 12 +- crates/rune/src/runtime/function.rs | 43 ++- crates/rune/src/runtime/range.rs | 50 +++- crates/rune/src/runtime/range_from.rs | 47 +++- crates/rune/src/runtime/range_full.rs | 29 +- crates/rune/src/runtime/range_inclusive.rs | 51 +++- crates/rune/src/runtime/range_to.rs | 40 ++- crates/rune/src/runtime/range_to_inclusive.rs | 40 ++- 9 files changed, 230 insertions(+), 344 deletions(-) diff --git a/crates/rune/src/modules/ops.rs b/crates/rune/src/modules/ops.rs index a892ed9bd..a5628cd58 100644 --- a/crates/rune/src/modules/ops.rs +++ b/crates/rune/src/modules/ops.rs @@ -4,7 +4,7 @@ use core::cmp::Ordering; use crate as rune; use crate::runtime::{ - ControlFlow, EnvProtocolCaller, Function, Generator, GeneratorState, Iterator, Protocol, Range, + ControlFlow, EnvProtocolCaller, Function, Generator, GeneratorState, Iterator, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive, Value, Vm, VmResult, }; use crate::{ContextError, Module}; @@ -16,44 +16,6 @@ pub fn module() -> Result { { m.ty::()?; - m.type_meta::()? - .make_named_struct(&["start"])? - .docs([ - "Type for a from range expression `start..`.", - "", - "# Examples", - "", - "```rune", - "let range = 0..;", - "", - "assert!(!range.contains(-10));", - "assert!(range.contains(5));", - "assert!(range.contains(10));", - "assert!(range.contains(20));", - "", - "assert!(range is std::ops::RangeFrom);", - "```", - "", - "Ranges can contain any type:", - "", - "```rune", - "let range = 'a'..;", - "assert_eq!(range.start, 'a');", - "range.start = 'b';", - "assert_eq!(range.start, 'b');", - "```", - "", - "Certain ranges can be used as iterators:", - "", - "```rune", - "let range = 'a'..;", - "assert_eq!(range.iter().take(5).collect::(), ['a', 'b', 'c', 'd', 'e']);", - "```", - ]); - m.field_function(Protocol::GET, "start", |r: &RangeFrom| r.start.clone())?; - m.field_function(Protocol::SET, "start", |r: &mut RangeFrom, value: Value| { - r.start = value; - })?; m.function_meta(RangeFrom::iter__meta)?; m.function_meta(RangeFrom::contains__meta)?; m.function_meta(RangeFrom::into_iter__meta)?; @@ -65,80 +27,11 @@ pub fn module() -> Result { { m.ty::()?; - m.type_meta::()?.make_empty_struct()?.docs([ - "Type for a full range expression `..`.", - "", - "# Examples", - "", - "```rune", - "let range = ..;", - "", - "assert!(range.contains(-10));", - "assert!(range.contains(5));", - "assert!(range.contains(10));", - "assert!(range.contains(20));", - "", - "assert!(range is std::ops::RangeFull);", - "```", - ]); m.function_meta(RangeFull::contains)?; } { m.ty::()?; - m.type_meta::()? - .make_named_struct(&["start", "end"])? - .docs([ - "Type for an inclusive range expression `start..=end`.", - "", - "# Examples", - "", - "```rune", - "let range = 0..=10;", - "", - "assert!(!range.contains(-10));", - "assert!(range.contains(5));", - "assert!(range.contains(10));", - "assert!(!range.contains(20));", - "", - "assert!(range is std::ops::RangeInclusive);", - "```", - "", - "Ranges can contain any type:", - "", - "```rune", - "let range = 'a'..='f';", - "assert_eq!(range.start, 'a');", - "range.start = 'b';", - "assert_eq!(range.start, 'b');", - "assert_eq!(range.end, 'f');", - "range.end = 'g';", - "assert_eq!(range.end, 'g');", - "```", - "", - "Certain ranges can be used as iterators:", - "", - "```rune", - "let range = 'a'..='e';", - "assert_eq!(range.iter().collect::(), ['a', 'b', 'c', 'd', 'e']);", - "```", - ]); - m.field_function(Protocol::GET, "start", |r: &RangeInclusive| r.start.clone())?; - m.field_function( - Protocol::SET, - "start", - |r: &mut RangeInclusive, value: Value| { - r.start = value; - }, - )?; - m.field_function(Protocol::GET, "end", |r: &RangeInclusive| r.end.clone())?; - m.field_function( - Protocol::SET, - "end", - |r: &mut RangeInclusive, value: Value| { - r.end = value; - }, - )?; m.function_meta(RangeInclusive::iter__meta)?; m.function_meta(RangeInclusive::contains__meta)?; m.function_meta(RangeInclusive::into_iter__meta)?; @@ -150,40 +43,6 @@ pub fn module() -> Result { { m.ty::()?; - m.type_meta::()? - .make_named_struct(&["end"])? - .docs([ - "Type for an inclusive range expression `..=end`.", - "", - "# Examples", - "", - "```rune", - "let range = ..=10;", - "assert!(range.contains(-10));", - "assert!(range.contains(5));", - "assert!(range.contains(10));", - "assert!(!range.contains(20));", - "", - "assert!(range is std::ops::RangeToInclusive);", - "```", - "", - "Ranges can contain any type:", - "", - "```rune", - "let range = ..='f';", - "assert_eq!(range.end, 'f');", - "range.end = 'g';", - "assert_eq!(range.end, 'g');", - "```", - ]); - m.field_function(Protocol::GET, "end", |r: &RangeToInclusive| r.end.clone())?; - m.field_function( - Protocol::SET, - "end", - |r: &mut RangeToInclusive, value: Value| { - r.end = value; - }, - )?; m.function_meta(RangeToInclusive::contains__meta)?; m.function_meta(RangeToInclusive::partial_eq__meta)?; m.function_meta(RangeToInclusive::eq__meta)?; @@ -193,36 +52,6 @@ pub fn module() -> Result { { m.ty::()?; - m.type_meta::()? - .make_named_struct(&["end"])? - .docs([ - "Type for an inclusive range expression `..end`.", - "", - "# Examples", - "", - "```rune", - "let range = ..10;", - "assert!(range.contains(-10));", - "assert!(range.contains(5));", - "assert!(!range.contains(10));", - "assert!(!range.contains(20));", - "", - "assert!(range is std::ops::RangeTo);", - "```", - "", - "Ranges can contain any type:", - "", - "```rune", - "let range = ..'f';", - "assert_eq!(range.end, 'f');", - "range.end = 'g';", - "assert_eq!(range.end, 'g');", - "```", - ]); - m.field_function(Protocol::GET, "end", |r: &RangeTo| r.end.clone())?; - m.field_function(Protocol::SET, "end", |r: &mut RangeTo, value: Value| { - r.end = value; - })?; m.function_meta(RangeTo::contains__meta)?; m.function_meta(RangeTo::partial_eq__meta)?; m.function_meta(RangeTo::eq__meta)?; @@ -232,50 +61,6 @@ pub fn module() -> Result { { m.ty::()?; - m.type_meta::()? - .make_named_struct(&["start", "end"])? - .docs([ - "Type for a range expression `start..end`.", - "", - "# Examples", - "", - "```rune", - "let range = 0..10;", - "assert!(!range.contains(-10));", - "assert!(range.contains(5));", - "assert!(!range.contains(10));", - "assert!(!range.contains(20));", - "", - "assert!(range is std::ops::Range);", - "```", - "", - "Ranges can contain any type:", - "", - "```rune", - "let range = 'a'..'f';", - "assert_eq!(range.start, 'a');", - "range.start = 'b';", - "assert_eq!(range.start, 'b');", - "assert_eq!(range.end, 'f');", - "range.end = 'g';", - "assert_eq!(range.end, 'g');", - "```", - "", - "Certain ranges can be used as iterators:", - "", - "```rune", - "let range = 'a'..'e';", - "assert_eq!(range.iter().collect::(), ['a', 'b', 'c', 'd']);", - "```", - ]); - m.field_function(Protocol::GET, "start", |r: &Range| r.start.clone())?; - m.field_function(Protocol::SET, "start", |r: &mut Range, value: Value| { - r.start = value; - })?; - m.field_function(Protocol::GET, "end", |r: &Range| r.end.clone())?; - m.field_function(Protocol::SET, "end", |r: &mut Range, value: Value| { - r.end = value; - })?; m.function_meta(Range::iter__meta)?; m.function_meta(Range::into_iter__meta)?; m.function_meta(Range::contains__meta)?; @@ -286,51 +71,10 @@ pub fn module() -> Result { } { - m.ty::()?.docs([ - " Used to tell an operation whether it should exit early or go on as usual.", - "", - " This acts as the basis of the [`TRY`] protocol in Rune.", - "", - " [`TRY`]: crate::Protocol::TRY", - "", - "# Examples", - "", - "```rune", - "use std::ops::ControlFlow;", - "", - "let c = ControlFlow::Continue(42);", - "assert_eq!(c.0, 42);", - "assert_eq!(c, ControlFlow::Continue(42));", - "```", - ]); + m.ty::()?; } - m.ty::()?.docs([ - "The type of a function in Rune.", - "", - "Functions can be called using call expression syntax, such as `()`.", - "", - "There are multiple different kind of things which can be coerced into a function in Rune:", - "* Regular functions.", - "* Closures (which might or might not capture their environment).", - "* Built-in constructors for tuple types (tuple structs, tuple variants).", - "", - "# Examples", - "", - "```rune", - "// Captures the constructor for the `Some()` tuple variant.", - "let build_some = Some;", - "assert_eq!(build_some(42), Some(42));", - "", - "fn build(value) {", - " Some(value)", - "}", - "", - "// Captures the function previously defined.", - "let build_some = build;", - "assert_eq!(build_some(42), Some(42));", - "```", - ]); + m.ty::()?; { m.ty::>()?.docs([ diff --git a/crates/rune/src/runtime/control_flow.rs b/crates/rune/src/runtime/control_flow.rs index cf0b75b2a..51b7f698c 100644 --- a/crates/rune/src/runtime/control_flow.rs +++ b/crates/rune/src/runtime/control_flow.rs @@ -9,7 +9,17 @@ use crate::Any; /// /// This acts as the basis of the [`TRY`] protocol in Rune. /// -/// [`TRY`]: crate::Protocol::TRY +/// [`TRY`]: crate::runtime::Protocol::TRY +/// +/// # Examples +/// +/// ```rune +/// use std::ops::ControlFlow; +/// +/// let c = ControlFlow::Continue(42); +/// assert_eq!(c.0, 42); +/// assert_eq!(c, ControlFlow::Continue(42)); +/// ``` #[derive(Debug, Clone, Any)] #[rune(builtin)] pub enum ControlFlow { diff --git a/crates/rune/src/runtime/function.rs b/crates/rune/src/runtime/function.rs index d98443a2c..a7a21e8cc 100644 --- a/crates/rune/src/runtime/function.rs +++ b/crates/rune/src/runtime/function.rs @@ -4,17 +4,44 @@ use core::future::Future; use crate::no_std::prelude::*; use crate::no_std::sync::Arc; -use crate::compile::Named; -use crate::module::{self, InstallWith}; +use crate as rune; +use crate::module; use crate::runtime::{ - Args, Call, ConstValue, FromValue, FunctionHandler, OwnedTuple, RawStr, Rtti, RuntimeContext, - Stack, Unit, Value, VariantRtti, Vm, VmCall, VmErrorKind, VmHalt, VmResult, + Args, Call, ConstValue, FromValue, FunctionHandler, OwnedTuple, Rtti, RuntimeContext, Stack, + Unit, Value, VariantRtti, Vm, VmCall, VmErrorKind, VmHalt, VmResult, }; use crate::shared::AssertSend; +use crate::Any; use crate::Hash; -/// A callable non-sync function. +/// The type of a function in Rune. +/// +/// Functions can be called using call expression syntax, such as `()`. +/// +/// There are multiple different kind of things which can be coerced into a +/// function in Rune: +/// * Regular functions. +/// * Closures (which might or might not capture their environment). +/// * Built-in constructors for tuple types (tuple structs, tuple variants). +/// +/// # Examples +/// +/// ```rune +/// // Captures the constructor for the `Some()` tuple variant. +/// let build_some = Some; +/// assert_eq!(build_some(42), Some(42)); +/// +/// fn build(value) { +/// Some(value) +/// } +/// +/// // Captures the function previously defined. +/// let build_some = build; +/// assert_eq!(build_some(42), Some(42)); +/// ``` +#[derive(Any)] #[repr(transparent)] +#[rune(builtin)] pub struct Function(FunctionImpl); impl Function { @@ -909,12 +936,6 @@ impl FromValue for SyncFunction { } } -impl InstallWith for Function {} - -impl Named for Function { - const BASE_NAME: RawStr = RawStr::from_str("Function"); -} - from_value!(Function, into_function); fn check_args(actual: usize, expected: usize) -> VmResult<()> { diff --git a/crates/rune/src/runtime/range.rs b/crates/rune/src/runtime/range.rs index f0e3f8f0f..285be267b 100644 --- a/crates/rune/src/runtime/range.rs +++ b/crates/rune/src/runtime/range.rs @@ -3,18 +3,47 @@ use core::fmt; use core::ops; use crate as rune; -use crate::compile::Named; -use crate::module::InstallWith; use crate::runtime::{ - EnvProtocolCaller, FromValue, Iterator, ProtocolCaller, RawStr, ToValue, Value, VmErrorKind, - VmResult, + EnvProtocolCaller, FromValue, Iterator, ProtocolCaller, ToValue, Value, VmErrorKind, VmResult, }; +use crate::Any; -/// Struct representing a dynamic anonymous object. +/// Type for a range expression `start..end`. /// /// # Examples /// +/// ```rune +/// let range = 0..10; +/// assert!(!range.contains(-10)); +/// assert!(range.contains(5)); +/// assert!(!range.contains(10)); +/// assert!(!range.contains(20)); +/// +/// assert!(range is std::ops::Range); +/// ``` +/// +/// Ranges can contain any type: +/// +/// ```rune +/// let range = 'a'..'f'; +/// assert_eq!(range.start, 'a'); +/// range.start = 'b'; +/// assert_eq!(range.start, 'b'); +/// assert_eq!(range.end, 'f'); +/// range.end = 'g'; +/// assert_eq!(range.end, 'g'); +/// ``` +/// +/// Certain ranges can be used as iterators: +/// +/// ```rune +/// let range = 'a'..'e'; +/// assert_eq!(range.iter().collect::(), ['a', 'b', 'c', 'd']); /// ``` +/// +/// # Examples +/// +/// ```rust /// use rune::runtime::Range; /// /// let start = rune::to_value(1)?; @@ -22,11 +51,14 @@ use crate::runtime::{ /// let _ = Range::new(start, end); /// # Ok::<_, rune::Error>(()) /// ``` -#[derive(Clone)] +#[derive(Any, Clone)] +#[rune(builtin, constructor)] pub struct Range { /// The start value of the range. + #[rune(get, set)] pub start: Value, /// The to value of the range. + #[rune(get, set)] pub end: Value, } @@ -287,9 +319,3 @@ where } from_value!(Range, into_range); - -impl Named for Range { - const BASE_NAME: RawStr = RawStr::from_str("Range"); -} - -impl InstallWith for Range {} diff --git a/crates/rune/src/runtime/range_from.rs b/crates/rune/src/runtime/range_from.rs index a70883be0..9686b1ebb 100644 --- a/crates/rune/src/runtime/range_from.rs +++ b/crates/rune/src/runtime/range_from.rs @@ -3,27 +3,56 @@ use core::fmt; use core::ops; use crate as rune; -use crate::compile::Named; -use crate::module::InstallWith; use crate::runtime::{ - EnvProtocolCaller, FromValue, Iterator, ProtocolCaller, RawStr, ToValue, Value, VmErrorKind, - VmResult, + EnvProtocolCaller, FromValue, Iterator, ProtocolCaller, ToValue, Value, VmErrorKind, VmResult, }; +use crate::Any; -/// Struct representing an open range `start..`. +/// Type for a from range expression `start..`. /// /// # Examples /// +/// ```rune +/// let range = 0..; +/// +/// assert!(!range.contains(-10)); +/// assert!(range.contains(5)); +/// assert!(range.contains(10)); +/// assert!(range.contains(20)); +/// +/// assert!(range is std::ops::RangeFrom); /// ``` +/// +/// Ranges can contain any type: +/// +/// ```rune +/// let range = 'a'..; +/// assert_eq!(range.start, 'a'); +/// range.start = 'b'; +/// assert_eq!(range.start, 'b'); +/// ``` +/// +/// Certain ranges can be used as iterators: +/// +/// ```rune +/// let range = 'a'..; +/// assert_eq!(range.iter().take(5).collect::(), ['a', 'b', 'c', 'd', 'e']); +/// ``` +/// +/// # Rust examples +/// +/// ```rust /// use rune::runtime::RangeFrom; /// /// let start = rune::to_value(1)?; /// let _ = RangeFrom::new(start); /// # Ok::<_, rune::Error>(()) /// ``` -#[derive(Clone)] +#[derive(Any, Clone)] +#[rune(builtin, constructor)] pub struct RangeFrom { /// The start value of the range. + #[rune(get, set)] pub start: Value, } @@ -258,9 +287,3 @@ where } from_value!(RangeFrom, into_range_from); - -impl Named for RangeFrom { - const BASE_NAME: RawStr = RawStr::from_str("RangeFrom"); -} - -impl InstallWith for RangeFrom {} diff --git a/crates/rune/src/runtime/range_full.rs b/crates/rune/src/runtime/range_full.rs index 9f6c14267..1abad671e 100644 --- a/crates/rune/src/runtime/range_full.rs +++ b/crates/rune/src/runtime/range_full.rs @@ -3,21 +3,34 @@ use core::fmt; use core::ops; use crate as rune; -use crate::compile::Named; -use crate::module::InstallWith; -use crate::runtime::{FromValue, ProtocolCaller, RawStr, ToValue, Value, VmResult}; +use crate::runtime::{FromValue, ProtocolCaller, ToValue, Value, VmResult}; +use crate::Any; -/// Struct representing an open range `start..`. +/// Type for a full range expression `..`. /// /// # Examples /// +/// ```rune +/// let range = ..; +/// +/// assert!(range.contains(-10)); +/// assert!(range.contains(5)); +/// assert!(range.contains(10)); +/// assert!(range.contains(20)); +/// +/// assert!(range is std::ops::RangeFull); /// ``` +/// +/// # Rust Examples +/// +/// ```rust /// use rune::runtime::RangeFull; /// /// let _ = RangeFull::new(); /// # Ok::<_, rune::Error>(()) /// ``` -#[derive(Default, Clone)] +#[derive(Any, Default, Clone)] +#[rune(builtin, constructor)] pub struct RangeFull; impl RangeFull { @@ -94,9 +107,3 @@ impl FromValue for ops::RangeFull { } from_value!(RangeFull, into_range_full); - -impl Named for RangeFull { - const BASE_NAME: RawStr = RawStr::from_str("RangeFull"); -} - -impl InstallWith for RangeFull {} diff --git a/crates/rune/src/runtime/range_inclusive.rs b/crates/rune/src/runtime/range_inclusive.rs index 3fa418b59..ea4187f96 100644 --- a/crates/rune/src/runtime/range_inclusive.rs +++ b/crates/rune/src/runtime/range_inclusive.rs @@ -3,18 +3,48 @@ use core::fmt; use core::ops; use crate as rune; -use crate::compile::Named; -use crate::module::InstallWith; use crate::runtime::{ - EnvProtocolCaller, FromValue, Iterator, ProtocolCaller, RawStr, ToValue, Value, VmErrorKind, - VmResult, + EnvProtocolCaller, FromValue, Iterator, ProtocolCaller, ToValue, Value, VmErrorKind, VmResult, }; +use crate::Any; -/// Struct representing a range `start..=end`. +/// Type for an inclusive range expression `start..=end`. /// /// # Examples /// +/// ```rune +/// let range = 0..=10; +/// +/// assert!(!range.contains(-10)); +/// assert!(range.contains(5)); +/// assert!(range.contains(10)); +/// assert!(!range.contains(20)); +/// +/// assert!(range is std::ops::RangeInclusive); /// ``` +/// +/// Ranges can contain any type: +/// +/// ```rune +/// let range = 'a'..='f'; +/// assert_eq!(range.start, 'a'); +/// range.start = 'b'; +/// assert_eq!(range.start, 'b'); +/// assert_eq!(range.end, 'f'); +/// range.end = 'g'; +/// assert_eq!(range.end, 'g'); +/// ``` +/// +/// Certain ranges can be used as iterators: +/// +/// ```rune +/// let range = 'a'..='e'; +/// assert_eq!(range.iter().collect::(), ['a', 'b', 'c', 'd', 'e']); +/// ``` +/// +/// # Rust Examples +/// +/// ```rust /// use rune::runtime::RangeInclusive; /// /// let start = rune::to_value(1)?; @@ -22,11 +52,14 @@ use crate::runtime::{ /// let _ = RangeInclusive::new(start, end); /// # Ok::<_, rune::Error>(()) /// ``` -#[derive(Clone)] +#[derive(Any, Clone)] +#[rune(builtin, constructor)] pub struct RangeInclusive { /// The start value of the range. + #[rune(get, set)] pub start: Value, /// The end value of the range. + #[rune(get, set)] pub end: Value, } @@ -287,9 +320,3 @@ where } from_value!(RangeInclusive, into_range_inclusive); - -impl Named for RangeInclusive { - const BASE_NAME: RawStr = RawStr::from_str("RangeInclusive"); -} - -impl InstallWith for RangeInclusive {} diff --git a/crates/rune/src/runtime/range_to.rs b/crates/rune/src/runtime/range_to.rs index 495344af5..a36100411 100644 --- a/crates/rune/src/runtime/range_to.rs +++ b/crates/rune/src/runtime/range_to.rs @@ -3,26 +3,46 @@ use core::fmt; use core::ops; use crate as rune; -use crate::compile::Named; -use crate::module::InstallWith; -use crate::runtime::{ - EnvProtocolCaller, FromValue, ProtocolCaller, RawStr, ToValue, Value, VmResult, -}; +use crate::runtime::{EnvProtocolCaller, FromValue, ProtocolCaller, ToValue, Value, VmResult}; +use crate::Any; -/// Struct representing an open range `..end`. +/// Type for an inclusive range expression `..end`. /// /// # Examples /// +/// ```rune +/// let range = ..10; +/// assert!(range.contains(-10)); +/// assert!(range.contains(5)); +/// assert!(!range.contains(10)); +/// assert!(!range.contains(20)); +/// +/// assert!(range is std::ops::RangeTo); /// ``` +/// +/// Ranges can contain any type: +/// +/// ```rune +/// let range = ..'f'; +/// assert_eq!(range.end, 'f'); +/// range.end = 'g'; +/// assert_eq!(range.end, 'g'); +/// ``` +/// +/// # Examples +/// +/// ```rust /// use rune::runtime::RangeTo; /// /// let end = rune::to_value(1)?; /// let _ = RangeTo::new(end); /// # Ok::<_, rune::Error>(()) /// ``` -#[derive(Clone)] +#[derive(Any, Clone)] +#[rune(builtin, constructor)] pub struct RangeTo { /// The end value of the range. + #[rune(get, set)] pub end: Value, } @@ -188,9 +208,3 @@ where } from_value!(RangeTo, into_range_to); - -impl Named for RangeTo { - const BASE_NAME: RawStr = RawStr::from_str("RangeTo"); -} - -impl InstallWith for RangeTo {} diff --git a/crates/rune/src/runtime/range_to_inclusive.rs b/crates/rune/src/runtime/range_to_inclusive.rs index 25c2c2e1d..71acbb9d3 100644 --- a/crates/rune/src/runtime/range_to_inclusive.rs +++ b/crates/rune/src/runtime/range_to_inclusive.rs @@ -3,26 +3,46 @@ use core::fmt; use core::ops; use crate as rune; -use crate::compile::Named; -use crate::module::InstallWith; -use crate::runtime::{ - EnvProtocolCaller, FromValue, ProtocolCaller, RawStr, ToValue, Value, VmResult, -}; +use crate::runtime::{EnvProtocolCaller, FromValue, ProtocolCaller, ToValue, Value, VmResult}; +use crate::Any; -/// Struct representing an open range `..=end`. +/// Type for an inclusive range expression `..=end`. /// /// # Examples /// +/// ```rune +/// let range = ..=10; +/// assert!(range.contains(-10)); +/// assert!(range.contains(5)); +/// assert!(range.contains(10)); +/// assert!(!range.contains(20)); +/// +/// assert!(range is std::ops::RangeToInclusive); /// ``` +/// +/// Ranges can contain any type: +/// +/// ```rune +/// let range = ..='f'; +/// assert_eq!(range.end, 'f'); +/// range.end = 'g'; +/// assert_eq!(range.end, 'g'); +/// ``` +/// +/// # Examples +/// +/// ```rust /// use rune::runtime::RangeToInclusive; /// /// let end = rune::to_value(10)?; /// let _ = RangeToInclusive::new(end); /// # Ok::<_, rune::Error>(()) /// ``` -#[derive(Clone)] +#[derive(Any, Clone)] +#[rune(builtin, constructor)] pub struct RangeToInclusive { /// The end value of the range. + #[rune(get, set)] pub end: Value, } @@ -188,9 +208,3 @@ where } from_value!(RangeToInclusive, into_range_to_inclusive); - -impl Named for RangeToInclusive { - const BASE_NAME: RawStr = RawStr::from_str("RangeToInclusive"); -} - -impl InstallWith for RangeToInclusive {}