FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
RustPython/examples/generator.rs at main · RustPython/RustPython · GitHub
RustPython
RustPython
Repository navigation
Code
Issues
185
(185)
Pull requests
113
(113)
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
RustPython
/
examples
/
generator.rs
Copy path
More file actions
More file actions
Latest commit
History
History
History
50 lines (45 loc) · 1.33 KB
Breadcrumbs
RustPython
/
examples
/
generator.rs
Copy path
File metadata and controls
50 lines (45 loc) · 1.33 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use
rustpython_vm
as
vm
;
use
std
::
process
::
ExitCode
;
use
vm
::
{
Interpreter
,
PyResult
,
builtins
::
PyIntRef
,
protocol
::
{
PyIter
,
PyIterReturn
}
,
}
;
fn
py_main
(
interp
:
&
Interpreter
)
-> vm
::
PyResult
<
(
)
>
{
let
generator = interp
.
enter
(
|vm|
{
let
scope = vm
.
new_scope_with_builtins
(
)
;
let
generator = vm
.
run_block_expr
(
scope
,
r#"
def gen():
for i in range(10):
yield i
gen()
"#
,
)
?
;
Ok
(
generator
)
}
)
?
;
loop
{
let
r = interp
.
enter
(
|vm|
{
let
v =
match
PyIter
::
new
(
generator
.
clone
(
)
)
.
next
(
vm
)
?
{
PyIterReturn
::
Return
(
obj
)
=>
{
PyIterReturn
::
Return
(
obj
.
try_into_value
::
<
PyIntRef
>
(
vm
)
?
)
}
PyIterReturn
::
StopIteration
(
x
)
=>
PyIterReturn
::
StopIteration
(
x
)
,
}
;
PyResult
::
Ok
(
v
)
}
)
?
;
match
r
{
PyIterReturn
::
Return
(
value
)
=>
println
!
(
"{value}"
)
,
PyIterReturn
::
StopIteration
(
_
)
=>
break
,
}
}
Ok
(
(
)
)
}
fn
main
(
)
->
ExitCode
{
let
builder = vm
::
Interpreter
::
builder
(
Default
::
default
(
)
)
;
let
defs = rustpython_stdlib
::
stdlib_module_defs
(
&
builder
.
ctx
)
;
let
interp = builder
.
add_native_modules
(
&
defs
)
.
build
(
)
;
let
result =
py_main
(
&
interp
)
;
vm
::
host_env
::
os
::
exit_code
(
interp
.
run
(
|_vm| result
)
)
}
Back
|
FazBrowse Home
|
New Git URL