Remove spawn_listener, spawn_conversation
These are not needed in a pipe-based Rustiverse
This commit is contained in:
parent
dff2853e4d
commit
80ef7243ea
|
@ -461,7 +461,7 @@ and child both need to exchange messages with each other. The
|
|||
function `std::comm::DuplexStream()` supports this pattern. We'll
|
||||
look briefly at how to use it.
|
||||
|
||||
To see how `spawn_conversation()` works, we will create a child task
|
||||
To see how `DuplexStream()` works, we will create a child task
|
||||
that repeatedly receives a `uint` message, converts it to a string, and sends
|
||||
the string in response. The child terminates when it receives `0`.
|
||||
Here is the function that implements the child task:
|
||||
|
|
|
@ -433,43 +433,6 @@ impl TaskBuilder {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a new task while providing a channel from the parent to the child
|
||||
*
|
||||
* Sets up a communication channel from the current task to the new
|
||||
* child task, passes the port to child's body, and returns a channel
|
||||
* linked to the port to the parent.
|
||||
*
|
||||
* This encapsulates some boilerplate handshaking logic that would
|
||||
* otherwise be required to establish communication from the parent
|
||||
* to the child.
|
||||
*/
|
||||
fn spawn_listener<A: Owned>(f: fn~(comm::Port<A>)) -> comm::Chan<A> {
|
||||
let setup_po = comm::Port();
|
||||
let setup_ch = comm::Chan(&setup_po);
|
||||
do self.spawn |move f| {
|
||||
let po = comm::Port();
|
||||
let ch = comm::Chan(&po);
|
||||
comm::send(setup_ch, ch);
|
||||
f(move po);
|
||||
}
|
||||
comm::recv(setup_po)
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a new task, setting up communication in both directions
|
||||
*/
|
||||
fn spawn_conversation<A: Owned, B: Owned>
|
||||
(f: fn~(comm::Port<A>, comm::Chan<B>))
|
||||
-> (comm::Port<B>, comm::Chan<A>) {
|
||||
let from_child = comm::Port();
|
||||
let to_parent = comm::Chan(&from_child);
|
||||
let to_child = do self.spawn_listener |move f, from_parent| {
|
||||
f(from_parent, to_parent)
|
||||
};
|
||||
(from_child, to_child)
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a function in another task and return either the return value
|
||||
* of the function or result::err.
|
||||
|
@ -567,28 +530,6 @@ pub fn spawn_with<A:Owned>(arg: A, f: fn~(v: A)) {
|
|||
task().spawn_with(move arg, move f)
|
||||
}
|
||||
|
||||
pub fn spawn_listener<A:Owned>(f: fn~(comm::Port<A>)) -> comm::Chan<A> {
|
||||
/*!
|
||||
* Runs a new task while providing a channel from the parent to the child
|
||||
*
|
||||
* This function is equivalent to `task().spawn_listener(f)`.
|
||||
*/
|
||||
|
||||
task().spawn_listener(move f)
|
||||
}
|
||||
|
||||
pub fn spawn_conversation<A: Owned, B: Owned>
|
||||
(f: fn~(comm::Port<A>, comm::Chan<B>))
|
||||
-> (comm::Port<B>, comm::Chan<A>) {
|
||||
/*!
|
||||
* Runs a new task, setting up communication in both directions
|
||||
*
|
||||
* This function is equivalent to `task().spawn_conversation(f)`.
|
||||
*/
|
||||
|
||||
task().spawn_conversation(move f)
|
||||
}
|
||||
|
||||
pub fn spawn_sched(mode: SchedMode, f: fn~()) {
|
||||
/*!
|
||||
* Creates a new scheduler and executes a task on it
|
||||
|
@ -926,34 +867,6 @@ fn test_back_to_the_future_result() {
|
|||
let _ = task().future_result(util::ignore).future_result(util::ignore);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_spawn_listiner_bidi() {
|
||||
let po = comm::Port();
|
||||
let ch = comm::Chan(&po);
|
||||
let ch = do spawn_listener |po| {
|
||||
// Now the child has a port called 'po' to read from and
|
||||
// an environment-captured channel called 'ch'.
|
||||
let res: ~str = comm::recv(po);
|
||||
assert res == ~"ping";
|
||||
comm::send(ch, ~"pong");
|
||||
};
|
||||
// Likewise, the parent has both a 'po' and 'ch'
|
||||
comm::send(ch, ~"ping");
|
||||
let res: ~str = comm::recv(po);
|
||||
assert res == ~"pong";
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_spawn_conversation() {
|
||||
let (recv_str, send_int) = do spawn_conversation |recv_int, send_str| {
|
||||
let input = comm::recv(recv_int);
|
||||
let output = int::str(input);
|
||||
comm::send(send_str, move output);
|
||||
};
|
||||
comm::send(send_int, 1);
|
||||
assert comm::recv(recv_str) == ~"1";
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_try_success() {
|
||||
match do try {
|
||||
|
@ -1115,15 +1028,6 @@ fn test_avoid_copying_the_body_spawn() {
|
|||
avoid_copying_the_body(spawn);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_avoid_copying_the_body_spawn_listener() {
|
||||
do avoid_copying_the_body |f| {
|
||||
spawn_listener(fn~(move f, _po: comm::Port<int>) {
|
||||
f();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_avoid_copying_the_body_task_spawn() {
|
||||
do avoid_copying_the_body |f| {
|
||||
|
@ -1133,15 +1037,6 @@ fn test_avoid_copying_the_body_task_spawn() {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_avoid_copying_the_body_spawn_listener_1() {
|
||||
do avoid_copying_the_body |f| {
|
||||
task().spawn_listener(fn~(move f, _po: comm::Port<int>) {
|
||||
f();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_avoid_copying_the_body_try() {
|
||||
do avoid_copying_the_body |f| {
|
||||
|
|
|
@ -64,7 +64,7 @@ pub fn from_file<T>(file: ~str, owner: SrvOwner<T>) -> T {
|
|||
fn run<T>(owner: SrvOwner<T>, source: ~str, +parse: Parser) -> T {
|
||||
|
||||
let srv_ = Srv({
|
||||
ch: do task::spawn_listener |move parse, po| {
|
||||
ch: do util::spawn_listener |move parse, po| {
|
||||
act(po, source, parse);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -39,7 +39,7 @@ fn run(
|
|||
return doc;
|
||||
}
|
||||
|
||||
let (result_port, page_chan) = do task::spawn_conversation
|
||||
let (result_port, page_chan) = do util::spawn_conversation
|
||||
|page_port, result_chan| {
|
||||
comm::send(result_chan, make_doc_from_pages(page_port));
|
||||
};
|
||||
|
|
|
@ -17,3 +17,26 @@ impl<T: Copy> NominalOp<T>: Clone {
|
|||
fn clone(&self) -> NominalOp<T> { copy *self }
|
||||
}
|
||||
|
||||
pub fn spawn_listener<A: Owned>(
|
||||
+f: fn~(comm::Port<A>)) -> comm::Chan<A> {
|
||||
let setup_po = comm::Port();
|
||||
let setup_ch = comm::Chan(&setup_po);
|
||||
do task::spawn |move f| {
|
||||
let po = comm::Port();
|
||||
let ch = comm::Chan(&po);
|
||||
comm::send(setup_ch, ch);
|
||||
f(move po);
|
||||
}
|
||||
comm::recv(setup_po)
|
||||
}
|
||||
|
||||
pub fn spawn_conversation<A: Owned, B: Owned>
|
||||
(+f: fn~(comm::Port<A>, comm::Chan<B>))
|
||||
-> (comm::Port<B>, comm::Chan<A>) {
|
||||
let from_child = comm::Port();
|
||||
let to_parent = comm::Chan(&from_child);
|
||||
let to_child = do spawn_listener |move f, from_parent| {
|
||||
f(from_parent, to_parent)
|
||||
};
|
||||
(from_child, to_child)
|
||||
}
|
||||
|
|
|
@ -131,6 +131,19 @@ fn creature(
|
|||
}
|
||||
|
||||
fn rendezvous(nn: uint, set: ~[color]) {
|
||||
|
||||
pub fn spawn_listener<A: Send>(+f: fn~(comm::Port<A>)) -> comm::Chan<A> {
|
||||
let setup_po = comm::Port();
|
||||
let setup_ch = comm::Chan(&setup_po);
|
||||
do task::spawn |move f| {
|
||||
let po = comm::Port();
|
||||
let ch = comm::Chan(&po);
|
||||
comm::send(setup_ch, ch);
|
||||
f(move po);
|
||||
}
|
||||
comm::recv(setup_po)
|
||||
}
|
||||
|
||||
// these ports will allow us to hear from the creatures
|
||||
let from_creatures: comm::Port<creature_info> = comm::Port();
|
||||
let from_creatures_log: comm::Port<~str> = comm::Port();
|
||||
|
@ -146,7 +159,7 @@ fn rendezvous(nn: uint, set: ~[color]) {
|
|||
// give us a channel to talk to each
|
||||
let ii = ii;
|
||||
let col = *col;
|
||||
do task::spawn_listener |from_rendezvous, move ii, move col| {
|
||||
do spawn_listener |from_rendezvous, move ii, move col| {
|
||||
creature(ii, col, from_rendezvous, to_rendezvous,
|
||||
to_rendezvous_log);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue