Skip to content

Commit 6a10e3a

Browse files
committed
Added oneshot protocol. Fixes #3125
1 parent 812db1e commit 6a10e3a

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/libcore/pipes.rs

+36
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export send_packet, recv_packet, send, recv, try_recv, peek;
7676
export select, select2, selecti, select2i, selectable;
7777
export spawn_service, spawn_service_recv;
7878
export stream, port, chan, shared_chan, port_set, channel;
79+
export oneshot, recv_one, try_recv_one;
7980

8081
#[doc(hidden)]
8182
const SPIN_COUNT: uint = 0;
@@ -1103,6 +1104,32 @@ impl<T: send, U: send, Left: selectable recv<T>, Right: selectable recv<U>>
11031104
}
11041105
}
11051106

1107+
proto! oneshot {
1108+
oneshot:send<T:send> {
1109+
send(T) -> !
1110+
}
1111+
}
1112+
1113+
/// Receive a message from a oneshot pipe.
1114+
fn recv_one<T: send>(+port: oneshot::server::oneshot<T>) -> T {
1115+
let oneshot::send(message) = recv(port);
1116+
message
1117+
}
1118+
1119+
/** Receive a message from a oneshot pipe, or fail if the connection
1120+
is closed.
1121+
1122+
*/
1123+
fn try_recv_one<T: send> (+port: oneshot::server::oneshot<T>) -> option<T> {
1124+
let message = try_recv(port);
1125+
1126+
if message == none { none }
1127+
else {
1128+
let oneshot::send(message) = option::unwrap(message);
1129+
some(message)
1130+
}
1131+
}
1132+
11061133
#[cfg(test)]
11071134
mod test {
11081135
#[test]
@@ -1119,4 +1146,13 @@ mod test {
11191146

11201147
c2.send(123);
11211148
}
1149+
1150+
#[test]
1151+
fn test_oneshot() {
1152+
let (c, p) = oneshot::init();
1153+
1154+
oneshot::client::send(c, ());
1155+
1156+
recv_one(p)
1157+
}
11221158
}

0 commit comments

Comments
 (0)