Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
Tranquility Virtual Machine
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Owen Hayes
Tranquility Virtual Machine
Commits
81a13dd1
Commit
81a13dd1
authored
2 years ago
by
MarvelousAnything
Browse files
Options
Downloads
Patches
Plain Diff
Possible solution for state trait object saftey issue.
parent
f0e4d42c
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/frame.rs
+11
-0
11 additions, 0 deletions
src/frame.rs
src/state.rs
+131
-4
131 additions, 4 deletions
src/state.rs
src/tvm.rs
+3
-1
3 additions, 1 deletion
src/tvm.rs
with
145 additions
and
5 deletions
src/frame.rs
+
11
−
0
View file @
81a13dd1
use
crate
::
callable
::
Callable
;
use
crate
::
callable
::
Callable
;
use
crate
::
instruction
::
Instruction
;
use
crate
::
instruction
::
Instruction
;
use
crate
::
tvm
::
Tvm
;
#[derive(Debug,
Clone,
PartialEq,
Eq)]
#[derive(Debug,
Clone,
PartialEq,
Eq)]
pub
struct
Frame
{
pub
struct
Frame
{
...
@@ -16,3 +17,13 @@ pub enum FrameData {
...
@@ -16,3 +17,13 @@ pub enum FrameData {
Instruction
(
Instruction
,
Vec
<
i32
>
),
Instruction
(
Instruction
,
Vec
<
i32
>
),
Primitive
(
i32
),
Primitive
(
i32
),
}
}
pub
trait
FrameEvaluator
{
fn
eval_frame
(
&
mut
self
,
frame
:
Frame
);
}
impl
FrameEvaluator
for
Tvm
{
fn
eval_frame
(
&
mut
self
,
frame
:
Frame
)
{
println!
(
"Evaluating frame: {:?}"
,
frame
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/state.rs
+
131
−
4
View file @
81a13dd1
...
@@ -3,10 +3,8 @@ use crate::callable::Callable;
...
@@ -3,10 +3,8 @@ use crate::callable::Callable;
use
crate
::
frame
::
Frame
;
use
crate
::
frame
::
Frame
;
use
crate
::
tvm
::
Tvm
;
use
crate
::
tvm
::
Tvm
;
pub
trait
State
:
Debug
+
Clone
{
pub
trait
State
:
Debug
{
fn
pause
(
&
mut
self
);
fn
tick
(
&
mut
self
,
tvm
:
&
mut
Tvm
)
->
StateResult
;
fn
resume
(
&
mut
self
);
fn
tick
(
&
mut
self
)
->
StateResult
;
}
}
#[derive(Debug,
Clone,
PartialEq,
Eq)]
#[derive(Debug,
Clone,
PartialEq,
Eq)]
...
@@ -26,13 +24,55 @@ pub enum TvmState {
...
@@ -26,13 +24,55 @@ pub enum TvmState {
Halted
,
Halted
,
}
}
impl
TvmState
{
pub
fn
to_state
(
&
self
)
->
Box
<
dyn
State
>
{
match
self
{
TvmState
::
Waiting
=>
Box
::
new
(
states
::
Waiting
),
TvmState
::
Paused
=>
Box
::
new
(
states
::
Paused
),
TvmState
::
Call
(
callable
)
=>
Box
::
new
(
states
::
Call
{
callable
:
callable
.clone
()
}),
TvmState
::
Eval
(
frame
,
pc
)
=>
Box
::
new
(
states
::
Eval
{
frame
:
frame
.clone
(),
pc
:
*
pc
}),
TvmState
::
FrameEval
(
frame
)
=>
Box
::
new
(
states
::
FrameEval
{
frame
:
frame
.clone
()
}),
TvmState
::
Halted
=>
Box
::
new
(
states
::
Halted
),
}
}
pub
fn
is_waiting
(
&
self
)
->
bool
{
matches!
(
self
,
TvmState
::
Waiting
)
}
pub
fn
is_paused
(
&
self
)
->
bool
{
matches!
(
self
,
TvmState
::
Paused
)
}
pub
fn
is_call
(
&
self
)
->
bool
{
matches!
(
self
,
TvmState
::
Call
(
_
))
}
pub
fn
is_eval
(
&
self
)
->
bool
{
matches!
(
self
,
TvmState
::
Eval
(
_
,
_
))
}
pub
fn
is_frame_eval
(
&
self
)
->
bool
{
matches!
(
self
,
TvmState
::
FrameEval
(
_
))
}
pub
fn
is_halted
(
&
self
)
->
bool
{
matches!
(
self
,
TvmState
::
Halted
)
}
}
pub
trait
Stateful
:
Debug
{
pub
trait
Stateful
:
Debug
{
fn
get_state
(
&
self
)
->
TvmState
;
fn
get_state
(
&
self
)
->
TvmState
;
fn
set_state
(
&
mut
self
,
state
:
TvmState
);
fn
set_state
(
&
mut
self
,
state
:
TvmState
);
fn
get_ticks
(
&
self
)
->
usize
;
fn
get_ticks
(
&
self
)
->
usize
;
fn
increment_ticks
(
&
mut
self
);
fn
increment_ticks
(
&
mut
self
);
fn
previous_state
(
&
self
)
->
Option
<
TvmState
>
;
fn
previous_state
(
&
self
)
->
Option
<
TvmState
>
;
fn
pause
(
&
mut
self
);
fn
resume
(
&
mut
self
);
fn
tick
(
&
mut
self
);
fn
get_last_result
(
&
self
)
->
Option
<
StateResult
>
;
fn
is_paused
(
&
self
)
->
bool
;
fn
is_paused
(
&
self
)
->
bool
;
fn
handle_result
(
&
mut
self
,
result
:
StateResult
);
}
}
impl
Stateful
for
Tvm
{
impl
Stateful
for
Tvm
{
...
@@ -57,8 +97,95 @@ impl Stateful for Tvm {
...
@@ -57,8 +97,95 @@ impl Stateful for Tvm {
self
.previous_state
.clone
()
self
.previous_state
.clone
()
}
}
fn
pause
(
&
mut
self
)
{
if
!
self
.is_paused
()
{
self
.set_state
(
TvmState
::
Paused
);
}
}
fn
resume
(
&
mut
self
)
{
if
self
.is_paused
()
{
self
.set_state
(
self
.previous_state
()
.unwrap
());
}
}
fn
tick
(
&
mut
self
)
{
}
fn
get_last_result
(
&
self
)
->
Option
<
StateResult
>
{
todo!
()
}
fn
is_paused
(
&
self
)
->
bool
{
fn
is_paused
(
&
self
)
->
bool
{
self
.state
==
TvmState
::
Paused
self
.state
==
TvmState
::
Paused
}
}
fn
handle_result
(
&
mut
self
,
result
:
StateResult
)
{
println!
(
"Handling result: {:?}"
,
result
);
}
}
pub
mod
states
{
use
super
::
*
;
#[derive(Debug,
Clone)]
pub
struct
Waiting
;
#[derive(Debug,
Clone)]
pub
struct
Paused
;
#[derive(Debug,
Clone)]
pub
struct
Call
{
pub
callable
:
Callable
,
}
#[derive(Debug,
Clone)]
pub
struct
Eval
{
pub
frame
:
Frame
,
pub
pc
:
usize
,
}
#[derive(Debug,
Clone)]
pub
struct
FrameEval
{
pub
frame
:
Frame
,
}
#[derive(Debug,
Clone)]
pub
struct
Halted
;
impl
State
for
Waiting
{
// Tick should do nothing.
fn
tick
(
&
mut
self
,
tvm
:
&
mut
Tvm
)
->
StateResult
{
StateResult
::
Continue
}
}
impl
State
for
Paused
{
// Tick should do nothing.
fn
tick
(
&
mut
self
,
tvm
:
&
mut
Tvm
)
->
StateResult
{
StateResult
::
Continue
}
}
}
impl
State
for
Call
{
fn
tick
(
&
mut
self
,
tvm
:
&
mut
Tvm
)
->
StateResult
{
unimplemented!
()
}
}
impl
State
for
Eval
{
fn
tick
(
&
mut
self
,
tvm
:
&
mut
Tvm
)
->
StateResult
{
unimplemented!
()
}
}
impl
State
for
FrameEval
{
fn
tick
(
&
mut
self
,
tvm
:
&
mut
Tvm
)
->
StateResult
{
unimplemented!
()
}
}
impl
State
for
Halted
{
fn
tick
(
&
mut
self
,
tvm
:
&
mut
Tvm
)
->
StateResult
{
unimplemented!
()
}
}
}
This diff is collapsed.
Click to expand it.
src/tvm.rs
+
3
−
1
View file @
81a13dd1
use
crate
::
frame
::
Frame
;
use
crate
::
frame
::
Frame
;
use
crate
::
state
::{
TvmState
};
use
crate
::
state
::{
StateResult
,
TvmState
};
#[derive(Debug,
Clone)]
#[derive(Debug,
Clone)]
pub
struct
Tvm
{
pub
struct
Tvm
{
...
@@ -10,6 +10,7 @@ pub struct Tvm {
...
@@ -10,6 +10,7 @@ pub struct Tvm {
pub
state
:
TvmState
,
pub
state
:
TvmState
,
pub
ticks
:
usize
,
pub
ticks
:
usize
,
pub
previous_state
:
Option
<
TvmState
>
,
pub
previous_state
:
Option
<
TvmState
>
,
pub
last_result
:
Option
<
StateResult
>
,
}
}
impl
Default
for
Tvm
{
impl
Default
for
Tvm
{
...
@@ -22,6 +23,7 @@ impl Default for Tvm {
...
@@ -22,6 +23,7 @@ impl Default for Tvm {
state
:
TvmState
::
Waiting
,
state
:
TvmState
::
Waiting
,
ticks
:
0
,
ticks
:
0
,
previous_state
:
None
,
previous_state
:
None
,
last_result
:
None
,
}
}
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment