8000 rune: Store GeneratorState in AnyObj instead of Mutable (relates #844) · rune-rs/rune@cf4516c · GitHub
[go: up one dir, main page]

Skip to content

Commit cf4516c

Browse files
committed
rune: Store GeneratorState in AnyObj instead of Mutable (relates #844)
1 parent a7f0f89 commit cf4516c

File tree

9 files changed

+147
-217
lines changed

9 files changed

+147
-217
lines changed

crates/rune/src/module/module.rs

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use crate::item::IntoComponent;
1717
use crate::macros::{MacroContext, TokenStream};
1818
use crate::module::DocFunction;
1919
use crate::runtime::{
20-
ConstConstruct, GeneratorState, InstAddress, MaybeTypeOf, Memory, Output, Protocol,
21-
ToConstValue, TypeCheck, TypeHash, TypeInfo, TypeOf, Value, VmResult,
20+
ConstConstruct, InstAddress, MaybeTypeOf, Memory, Output, Protocol, ToConstValue, TypeCheck,
21+
TypeHash, TypeInfo, TypeOf, Value, VmResult,
2222
};
2323
use crate::{Hash, Item, ItemBuf};
2424

@@ -353,53 +353,6 @@ impl Module {
353353
Ok(())
354354
}
355355

356-
/// Construct the type information for the `GeneratorState` type.
357-
///
358-
/// Registering this allows the given type to be used in Rune scripts when
359-
/// referring to the `GeneratorState` type.
360-
///
361-
/// # Examples
362-
///
363-
/// This shows how to register the `GeneratorState` as
364-
/// `nonstd::ops::GeneratorState`.
365-
///
366-
/// ```
367-
/// use rune::Module;
368-
///
369-
/// let mut module = Module::with_crate_item("nonstd", ["ops"])?;
370-
/// module.generator_state(["GeneratorState"])?;
371-
///
372-
/// Ok::<_, rune::support::Error>(())
373-
pub fn generator_state<N>(
374-
&mut self,
375-
name: N,
376-
) -> Result<InternalEnumMut<'_, GeneratorState>, ContextError>
377-
where
378-
N: IntoComponent,
379-
{
380-
let mut enum_ = InternalEnum::new(
381-
"GeneratorState",
382-
crate::runtime::static_type::GENERATOR_STATE,
383-
);
384-
385-
// Note: these numeric variants are magic, and must simply match up with
386-
// what's being used in the virtual machine implementation for these
387-
// types.
388-
enum_.variant(
389-
"Complete",
390-
TypeCheck::GeneratorState(0),
391-
GeneratorState::Complete,
392-
)?;
393-
394-
enum_.variant(
395-
"Yielded",
396-
TypeCheck::GeneratorState(1),
397-
GeneratorState::Yielded,
398-
)?;
399-
400-
self.install_internal_enum(name, enum_)
401-
}
402-
403356
/// Construct type information for the `Option` type.
404357
///
405358
/// Registering this allows the given type to be used in Rune scripts when

crates/rune/src/modules/ops/generator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub fn module() -> Result<Module, ContextError> {
4545
}
4646

4747
{
48-
m.generator_state(["GeneratorState"])?.docs(docstring! {
48+
m.ty::<GeneratorState>()?.docs(docstring! {
4949
/// Enum indicating the state of a generator.
5050
})?;
5151

crates/rune/src/runtime/bytes.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ use serde::ser;
1111
use crate as rune;
1212
use crate::alloc::prelude::*;
1313
use crate::alloc::{self, Box, Vec};
14-
use crate::runtime::{RawAnyGuard, Ref, UnsafeToRef, Value, VmResult};
1514
use crate::Any;
1615

16+
use super::{IntoOutput, RawAnyGuard, Ref, UnsafeToRef, Value, VmResult};
17+
1718
/// A vector of bytes.
1819
#[derive(Default, Any, PartialEq, Eq, PartialOrd, Ord, Hash)]
1920
#[rune(static_type = BYTES)]
@@ -440,3 +441,21 @@ impl<'de> de::Deserialize<'de> for Bytes {
440441
deserializer.deserialize_bytes(Visitor)
441442
}
442443
}
444+
445+
impl TryFrom<&[u8]> for Value {
446+
type Error = alloc::Error;
447+
448+
#[inline]
449+
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
450+
Value::new(Bytes::try_from(value)?)
451+
}
452+
}
453+
454+
impl IntoOutput for &[u8] {
455+
type Output = Bytes;
456+
457+
#[inline]
458+
fn into_output(self) -> VmResult<Self::Output> {
459+
VmResult::Ok(vm_try!(Bytes::try_from(self)))
460+
}
461+
}

crates/rune/src/runtime/generator_state.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ use crate::Any;
5050
/// # Ok::<_, rune::support::Error>(())
5151
/// ```
5252
#[derive(Any, Debug, TryClone)]
53-
#[rune(builtin, static_type = GENERATOR_STATE)]
53+
#[rune(static_type = GENERATOR_STATE)]
5454
pub enum GeneratorState {
5555
/// The generator yielded.
56-
Yielded(Value),
56+
#[rune(constructor)]
57+
Yielded(#[rune(get, set)] Value),
5758
/// The generator completed.
58-
Complete(Value),
59+
#[rune(constructor)]
60+
Complete(#[rune(get, set)] Value),
5961
}
6062

6163
impl GeneratorState {
@@ -97,10 +99,3 @@ impl GeneratorState {
9799
}
98100
}
99101
}
100-
101-
from_value2!(
102-
GeneratorState,
103-
into_generator_state_ref,
104-
into_generator_state_mut,
105-
into_generator_state
106-
);

crates/rune/src/runtime/inst.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ pub enum TypeCheck {
7171
/// A result type, and the specified variant index.
7272
#[musli(packed)]
7373
Result(usize),
74-
/// A generator state type, and the specified variant index.
75-
#[musli(packed)]
76-
GeneratorState(usize),
7774
}
7875

7976
impl fmt::Display for TypeCheck {
@@ -87,8 +84,6 @@ impl fmt::Display for TypeCheck {
8784
Self::Option(..) => write!(fmt, "Option::None"),
8885
Self::Result(0) => write!(fmt, "Result::Ok"),
8986
Self::Result(..) => write!(fmt, "Result::Err"),
90-
Self::GeneratorState(0) => write!(fmt, "GeneratorState::Yielded"),
91-
Self::GeneratorState(..) => write!(fmt, "GeneratorState::Complete"),
9287
}
9388
}
9489
}
@@ -1878,3 +1873,12 @@ where
18781873
fmt::Debug::fmt(&self.0, f)
18791874
}
18801875
}
1876+
1877+
impl IntoOutput for &str {
1878+
type Output = String;
1879+
1880+
#[inline]
1881+
fn into_output(self) -> VmResult<Self::Output> {
1882+
VmResult::Ok(vm_try!(String::try_from(self)))
1883+
}
1884+
}

crates/rune/src/runtime/type_info.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ impl fmt::Display for TypeInfo {
109109
}
110110
}
111111

112+
impl From<AnyTypeInfo> for TypeInfo {
113+
#[inline]
114+
fn from(value: AnyTypeInfo) -> Self {
115+
Self::any_type_info(value)
116+
}
117+
}
118+
112119
/// Type information for the [`Any`][crate::Any] type.
113120
#[derive(Debug, TryClone, Clone, Copy, PartialEq, Eq)]
114121
#[try_clone(copy)]

crates/rune/src/runtime/value.rs

Lines changed: 10 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ use crate::{Any, Hash};
3030

3131
use super::static_type;
3232
use super::{
33-
AccessError, AnyObj, AnyObjDrop, BorrowMut, BorrowRef, Bytes, CallResultOnly, ConstValue,
34-
ConstValueKind, ControlFlow, DynGuardedArgs, EnvProtocolCaller, Format, Formatter, FromValue,
35-
Function, Future, Generator, GeneratorState, IntoOutput, Iterator, MaybeTypeOf, Mut, Object,
36-
OwnedTuple, Protocol, ProtocolCaller, RawAnyObjGuard, Ref, RuntimeError, Shared, Snapshot,
37-
Stream, Type, TypeInfo, Variant, Vec, Vm, VmErrorKind, VmIntegerRepr, VmResult,
33+
AccessError, AnyObj, AnyObjDrop, BorrowMut, BorrowRef, CallResultOnly, ConstValue,
34+
ConstValueKind, DynGuardedArgs, EnvProtocolCaller, Formatter, FromValue, Function, Future,
35+
Generator, IntoOutput, Iterator, MaybeTypeOf, Mut, Object, OwnedTuple, Protocol,
36+
ProtocolCaller, RawAnyObjGuard, Ref, RuntimeError, Shared, Snapshot, Stream, Type, TypeInfo,
37+
Variant, Vec, Vm, VmErrorKind, VmIntegerRepr, VmResult,
3838
};
3939
#[cfg(feature = "alloc")]
4040
use super::{Hasher, Tuple};
@@ -428,9 +428,6 @@ impl Value {
428428
Mutable::Object(value) => Mutable::Object(vm_try!(value.try_clone())),
429429
Mutable::Stream(value) => Mutable::Stream(vm_try!(value.try_clone())),
430430
Mutable::Generator(value) => Mutable::Generator(vm_try!(value.try_clone())),
431-
Mutable::GeneratorState(value) => {
432-
Mutable::GeneratorState(vm_try!(value.try_clone()))
433-
}
434431
Mutable::Option(value) => Mutable::Option(vm_try!(value.try_clone())),
435432
Mutable::Result(value) => Mutable::Result(vm_try!(value.try_clone())),
436433
Mutable::EmptyStruct(value) => Mutable::EmptyStruct(vm_try!(value.try_clone())),
@@ -512,9 +509,6 @@ impl Value {
512509
Mutable::Generator(value) => {
513510
vm_try!(vm_write!(f, "{value:?}"));
514511
}
515-
Mutable::GeneratorState(value) => {
516-
vm_try!(vm_write!(f, "{value:?}"));
517-
}
518512
Mutable::Option(value) => {
519513
vm_try!(vm_write!(f, "{value:?}"));
520514
}
@@ -837,16 +831,6 @@ impl Value {
837831
into_function,
838832
}
839833

840-
into! {
841-
/// Coerce into a [`GeneratorState`].
842-
GeneratorState(GeneratorState),
843-
into_generator_state_ref,
844-
into_generator_state_mut,
845-
borrow_generator_state_ref,
846-
borrow_generator_state_mut,
847-
into_generator_state,
848-
}
849-
850834
into! {
851835
/// Coerce into a [`Generator`].
852836
Generator(Generator<Vm>),
@@ -1880,33 +1864,6 @@ impl TryFrom<&str> for Value {
18801864
}
18811865
}
18821866

1883-
impl IntoOutput for &str {
1884-
type Output = String;
1885-
1886-
#[inline]
1887-
fn into_output(self) -> VmResult<Self::Output> {
1888-
VmResult::Ok(vm_try!(String::try_from(self)))
1889-
}
1890-
}
1891-
1892-
impl TryFrom<&[u8]> for Value {
1893-
type Error = alloc::Error;
1894-
1895-
#[inline]
1896-
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
1897-
Value::new(Bytes::try_from(value)?)
1898-
}
1899-
}
1900-
1901-
impl IntoOutput for &[u8] {
1902-
type Output = Bytes;
1903-
1904-
#[inline]
1905-
fn into_output(self) -> VmResult<Self::Output> {
1906-
VmResult::Ok(vm_try!(Bytes::try_from(self)))
1907-
}
1908-
}
1909-
19101867
impl IntoOutput for Mutable {
19111868
type Output = Mutable;
19121869

@@ -1928,7 +1885,6 @@ inline_from! {
19281885

19291886
from! {
19301887
Function => Function,
1931-
GeneratorState => GeneratorState,
19321888
Vec => Vec,
19331889
EmptyStruct => EmptyStruct,
19341890
TupleStruct => TupleStruct,
@@ -1942,10 +1898,11 @@ from! {
19421898
}
19431899

19441900
any_from! {
1945-
String,
1946-
Bytes,
1947-
Format,
1948-
ControlFlow,
1901+
crate::alloc::String,
1902+
super::Bytes,
1903+
super::Format,
1904+
super::ControlFlow,
1905+
super::GeneratorState,
19491906
}
19501907

19511908
from_container! {
@@ -2077,8 +2034,6 @@ pub(crate) enum Mutable {
20772034
Stream(Stream<Vm>),
20782035
/// A stored generator.
20792036
Generator(Generator<Vm>),
2080-
/// Generator state.
2081-
GeneratorState(GeneratorState),
20822037
/// An empty value indicating nothing.
20832038
Option(Option<Value>),
20842039
/// A stored result in a slot.
@@ -2104,7 +2059,6 @@ impl Mutable {
21042059
Mutable::Future(..) => TypeInfo::static_type(static_type::FUTURE),
21052060
Mutable::Stream(..) => TypeInfo::static_type(static_type::STREAM),
21062061
Mutable::Generator(..) => TypeInfo::static_type(static_type::GENERATOR),
2107-
Mutable::GeneratorState(..) => TypeInfo::static_type(static_type::GENERATOR_STATE),
21082062
Mutable::Option(..) => TypeInfo::static_type(static_type::OPTION),
21092063
Mutable::Result(..) => TypeInfo::static_type(static_type::RESULT),
21102064
Mutable::Function(..) => TypeInfo::static_type(static_type::FUNCTION),
@@ -2127,7 +2081,6 @@ impl Mutable {
21272081
Mutable::Future(..) => static_type::FUTURE.hash,
21282082
Mutable::Stream(..) => static_type::STREAM.hash,
21292083
Mutable::Generator(..) => static_type::GENERATOR.hash,
2130-
Mutable::GeneratorState(..) => static_type::GENERATOR_STATE.hash,
21312084
Mutable::Result(..) => static_type::RESULT.hash,
21322085
Mutable::Option(..) => static_type::OPTION.hash,
21332086
Mutable::Function(..) => static_type::FUNCTION.hash,

crates/rune/src/runtime/value/serde.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,6 @@ impl ser::Serialize for Value {
7878
Mutable::Future(..) => Err(ser::Error::custom("cannot serialize futures")),
7979
Mutable::Stream(..) => Err(ser::Error::custom("cannot serialize streams")),
8080
Mutable::Generator(..) => Err(ser::Error::custom("cannot serialize generators")),
81-
Mutable::GeneratorState(..) => {
82-
Err(ser::Error::custom("cannot serialize generator states"))
83-
}
8481
Mutable::Function(..) => {
8582
Err(ser::Error::custom("cannot serialize function pointers"))
8683
}

0 commit comments

Comments
 (0)
0