Skip to content

Commit 22e955a

Browse files
committed
Move streams into core.
1 parent 594d9a0 commit 22e955a

File tree

2 files changed

+71
-43
lines changed

2 files changed

+71
-43
lines changed

src/libcore/pipes.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import unsafe::{forget, reinterpret_cast, transmute};
44
import either::{either, left, right};
5+
import option::unwrap;
56

67
enum state {
78
empty,
@@ -428,3 +429,69 @@ fn spawn_service_recv<T: send>(
428429

429430
client
430431
}
432+
433+
// Streams - Make pipes a little easier in general.
434+
435+
proto! streamp {
436+
open:send<T: send> {
437+
data(T) -> open<T>
438+
}
439+
}
440+
441+
type chan<T:send> = { mut endp: option<streamp::client::open<T>> };
442+
type port<T:send> = { mut endp: option<streamp::server::open<T>> };
443+
444+
fn stream<T:send>() -> (chan<T>, port<T>) {
445+
let (c, s) = streamp::init();
446+
447+
#macro[
448+
[#move[x],
449+
unsafe { let y <- *ptr::addr_of(x); y }]
450+
];
451+
452+
({ mut endp: some(c) }, { mut endp: some(s) })
453+
}
454+
455+
impl chan<T: send> for chan<T> {
456+
fn send(+x: T) {
457+
let mut endp = none;
458+
endp <-> self.endp;
459+
self.endp = some(
460+
streamp::client::data(unwrap(endp), x))
461+
}
462+
}
463+
464+
impl port<T: send> for port<T> {
465+
fn recv() -> T {
466+
let mut endp = none;
467+
endp <-> self.endp;
468+
let streamp::data(x, endp) = pipes::recv(unwrap(endp));
469+
self.endp = some(endp);
470+
x
471+
}
472+
473+
fn try_recv() -> option<T> {
474+
let mut endp = none;
475+
endp <-> self.endp;
476+
alt pipes::try_recv(unwrap(endp)) {
477+
some(streamp::data(x, endp)) {
478+
self.endp = some(#move(endp));
479+
some(#move(x))
480+
}
481+
none { none }
482+
}
483+
}
484+
485+
pure fn peek() -> bool unchecked {
486+
let mut endp = none;
487+
endp <-> self.endp;
488+
let peek = alt endp {
489+
some(endp) {
490+
pipes::peek(endp)
491+
}
492+
none { fail "peeking empty stream" }
493+
};
494+
self.endp <-> endp;
495+
peek
496+
}
497+
}

src/test/bench/shootout-k-nucleotide-pipes.rs

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,7 @@ import std::map;
99
import std::map::hashmap;
1010
import std::sort;
1111

12-
import stream::{stream, chan, port};
13-
14-
// After a snapshot, this should move into core, or std.
15-
mod stream {
16-
import option::unwrap;
17-
18-
proto! streamp {
19-
open:send<T: send> {
20-
data(T) -> open<T>
21-
}
22-
}
23-
24-
type chan<T:send> = { mut endp: option<streamp::client::open<T>> };
25-
type port<T:send> = { mut endp: option<streamp::server::open<T>> };
26-
27-
fn stream<T:send>() -> (chan<T>, port<T>) {
28-
let (c, s) = streamp::init();
29-
({ mut endp: some(c) }, { mut endp: some(s) })
30-
}
31-
32-
impl chan<T: send> for chan<T> {
33-
fn send(+x: T) {
34-
let mut endp = none;
35-
endp <-> self.endp;
36-
self.endp = some(
37-
streamp::client::data(unwrap(endp), x))
38-
}
39-
}
40-
41-
impl port<T: send> for port<T> {
42-
fn recv() -> T {
43-
let mut endp = none;
44-
endp <-> self.endp;
45-
let streamp::data(x, endp) = unwrap(
46-
pipes::try_recv(unwrap(endp)));
47-
self.endp = some(endp);
48-
x
49-
}
50-
}
51-
}
12+
import pipes::{stream, port, chan};
5213

5314
// given a map, print a sorted version of it
5415
fn sort_and_fmt(mm: hashmap<~[u8], uint>, total: uint) -> str {
@@ -127,8 +88,8 @@ fn windows_with_carry(bb: ~[const u8], nn: uint,
12788
ret vec::slice(bb, len - (nn - 1u), len);
12889
}
12990

130-
fn make_sequence_processor(sz: uint, from_parent: stream::port<~[u8]>,
131-
to_parent: stream::chan<str>) {
91+
fn make_sequence_processor(sz: uint, from_parent: pipes::port<~[u8]>,
92+
to_parent: pipes::chan<str>) {
13293

13394
let freqs: hashmap<~[u8], uint> = map::bytes_hash();
13495
let mut carry: ~[u8] = ~[];
@@ -190,7 +151,7 @@ fn main(args: ~[str]) {
190151

191152
vec::push(from_child, from_child_);
192153

193-
let (to_child, from_parent) = stream::stream();
154+
let (to_child, from_parent) = pipes::stream();
194155

195156
do task::spawn_with(from_parent) |from_parent| {
196157
make_sequence_processor(sz, from_parent, to_parent_);

0 commit comments

Comments
 (0)