Skip to content

Commit b157f0b

Browse files
committed
core: Add comm::peek for looking into the message queue
1 parent a53a08e commit b157f0b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/libcore/comm.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import task;
2626

2727
export send;
2828
export recv;
29+
export peek;
2930
export chan::{};
3031
export port::{};
3132

@@ -153,6 +154,11 @@ fn recv_<T: send>(p: *rust_port) -> T {
153154
ret res;
154155
}
155156

157+
#[doc = "Returns true if there are messages available"]
158+
fn peek<T: send>(p: port<T>) -> bool {
159+
rustrt::rust_port_size(***p) != 0u as ctypes::size_t
160+
}
161+
156162
#[doc(
157163
brief = "Constructs a channel. The channel is bound to the \
158164
port used to construct it."
@@ -203,3 +209,13 @@ fn chan_chan() {
203209
recv(p);
204210
}
205211

212+
#[test]
213+
fn test_peek() {
214+
let po = port();
215+
let ch = chan(po);
216+
assert !peek(po);
217+
send(ch, ());
218+
assert peek(po);
219+
recv(po);
220+
assert !peek(po);
221+
}

0 commit comments

Comments
 (0)