Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
9775b480
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
9775b480
编写于
1月 08, 2012
作者:
N
Niko Matsakis
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add spawn_connected
上级
91cd57e8
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
66 addition
and
13 deletion
+66
-13
src/libcore/task.rs
src/libcore/task.rs
+47
-13
src/test/stdtest/task.rs
src/test/stdtest/task.rs
+19
-0
未找到文件。
src/libcore/task.rs
浏览文件 @
9775b480
...
...
@@ -46,6 +46,9 @@
export
get_task
;
export
spawn
;
export
spawn_joinable
;
export
spawn_connected
;
export
connected_fn
;
export
connected_task
;
#[abi
=
"rust-intrinsic"
]
native
mod
rusti
{
...
...
@@ -190,27 +193,58 @@ fn spawn_joinable(+f: sendfn()) -> joinable_task {
}
/*
type connected_fn<ToCh, FrCh> = sendfn(comm::chan<FrCh>, comm::port<ToCh>);
Type: connected_fn
The prototype for a connected child task function. Such a function will be
supplied with a channel to send messages to the parent and a port to receive
messages from the parent. The type parameter `ToCh` is the type for messages
sent from the parent to the child and `FrCh` is the type for messages sent
from the child to the parent. */
type
connected_fn
<
ToCh
,
FrCh
>
=
sendfn
(
comm
::
port
<
ToCh
>
,
comm
::
chan
<
FrCh
>
);
/*
Type: connected_fn
The result type of <spawn_connected>
*/
type
connected_task
<
ToCh
,
FrCh
>
=
{
port
: comm::port<FrCh>,
chan
: comm::chan<ToCh>,
from_child
:
comm
::
port
<
FrCh
>
,
to_child
:
comm
::
chan
<
ToCh
>
,
task
:
task
};
/*
Function: spawn_connected
Spawns a child task along with a port/channel for exchanging messages
with the parent task. The type `ToCh` represents messages sent to the child
and `FrCh` messages received from the child.
Parameters:
f - the child function to execute
Returns:
The new child task along with the port to receive messages and the channel
to send messages.
*/
fn
spawn_connected
<
ToCh
:
send
,
FrCh
:
send
>
(
f
:
connected_fn
<
ToCh
,
FrCh
>
)
-> connected_
fn
{
let from_child_port = comm::port<FrCh>();
->
connected_
task
<
ToCh
,
FrCh
>
{
let
from_child_port
=
comm
::
port
::
<
FrCh
>
();
let
from_child_chan
=
comm
::
chan
(
from_child_port
);
let get_to_child_port = comm::port<comm::chan<ToCh>>();
let get_to_child_chan = comm::chan(to_child_port);
let
get_to_child_port
=
comm
::
port
::
<
comm
::
chan
<
ToCh
>>
();
let
get_to_child_chan
=
comm
::
chan
(
get_
to_child_port
);
let
child_task
=
spawn
(
sendfn
[
move
f
]()
{
let to_child_port = comm::port<ToCh>();
comm::send(get_to_child_chan,
to_child_port
);
f(
from_child_chan, to_child_port
);
let
to_child_port
=
comm
::
port
::
<
ToCh
>
();
comm
::
send
(
get_to_child_chan
,
comm
::
chan
(
to_child_port
)
);
f
(
to_child_port
,
from_child_chan
);
});
let to_child_chan = comm::recv(get_out);
ret {port: from_child_port, chan: to_child_chan, task: child_task};
let
to_child_chan
=
comm
::
recv
(
get_to_child_port
);
ret
{
from_child
:
from_child_port
,
to_child
:
to_child_chan
,
task
:
child_task
};
}
*/
/* Section: Operations */
...
...
src/test/stdtest/task.rs
浏览文件 @
9775b480
...
...
@@ -57,3 +57,22 @@ fn spawn_polymorphic() {
task
::
spawn
{||
foo
(
true
);};
task
::
spawn
{||
foo
(
42
);};
}
#[test]
fn
spawn_connected_stringifier
()
{
fn
stringifer
(
p
:
comm
::
port
<
uint
>
,
ch
:
comm
::
chan
<
str
>
)
{
let
u
=
1u
;
while
u
!=
0u
{
u
=
comm
::
recv
(
p
);
comm
::
send
(
ch
,
uint
::
to_str
(
u
,
10u
));
}
}
let
ch
=
task
::
spawn_connected
(
stringifer
);
comm
::
send
(
ch
.to_child
,
22u
);
assert
"22"
==
comm
::
recv
(
ch
.from_child
);
comm
::
send
(
ch
.to_child
,
44u
);
assert
"44"
==
comm
::
recv
(
ch
.from_child
);
comm
::
send
(
ch
.to_child
,
0u
);
assert
"0"
==
comm
::
recv
(
ch
.from_child
);
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录