Skip to content

Commit 6535da8

Browse files
committed
Tighten pipe exports, and refactor traits.
1 parent 729c37f commit 6535da8

File tree

3 files changed

+50
-27
lines changed

3 files changed

+50
-27
lines changed

src/libcore/iter-trait/dlist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type IMPL_T<A> = dlist::dlist<A>;
77
* e.g. breadth-first search with in-place enqueues), but removing the current
88
* node is forbidden.
99
*/
10-
fn EACH<A>(self: IMPL_T<A>, f: fn(A) -> bool) {
10+
pure fn EACH<A>(self: IMPL_T<A>, f: fn(A) -> bool) {
1111
import dlist::extensions;
1212

1313
let mut link = self.peek_n();

src/libcore/iter-trait/option.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
type IMPL_T<A> = option<A>;
22

3-
fn EACH<A>(self: IMPL_T<A>, f: fn(A) -> bool) {
3+
pure fn EACH<A>(self: IMPL_T<A>, f: fn(A) -> bool) {
44
alt self {
55
none { }
66
some(a) { f(a); }

src/libcore/pipes.rs

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,22 @@ import either::{either, left, right};
55
import option::unwrap;
66
import arc::methods;
77

8-
/* Use this after the snapshot
8+
// Things used by code generated by the pipe compiler.
9+
export entangle;
10+
11+
// User-level things
12+
export send_packet, recv_packet, send, recv, try_recv, peek;
13+
export select, select2, selecti, select2i, selectable;
14+
export spawn_service, spawn_service_recv;
15+
export stream, port, chan, shared_chan, port_set, channel;
16+
917
macro_rules! move {
1018
{ $x:expr } => { unsafe { let y <- *ptr::addr_of($x); y } }
1119
}
12-
*/
1320

14-
fn macros() {
15-
#macro[
16-
[#move(x), { unsafe { let y <- *ptr::addr_of(x); y } }]
17-
];
18-
}
21+
// This is to help make sure we only move out of enums in safe
22+
// places. Once there is unary move, it can be removed.
23+
fn move<T>(-x: T) -> T { x }
1924

2025
enum state {
2126
empty,
@@ -465,6 +470,19 @@ proto! streamp {
465470
}
466471
}
467472

473+
// It'd be nice to call this send, but it'd conflict with the built in
474+
// send kind.
475+
trait channel<T: send> {
476+
fn send(+x: T);
477+
}
478+
479+
trait recv<T: send> {
480+
fn recv() -> T;
481+
fn try_recv() -> option<T>;
482+
// This should perhaps be a new trait
483+
pure fn peek() -> bool;
484+
}
485+
468486
type chan_<T:send> = { mut endp: option<streamp::client::open<T>> };
469487

470488
enum chan<T:send> {
@@ -483,7 +501,7 @@ fn stream<T:send>() -> (chan<T>, port<T>) {
483501
(chan_({ mut endp: some(c) }), port_({ mut endp: some(s) }))
484502
}
485503

486-
impl chan<T: send> for chan<T> {
504+
impl chan<T: send> of channel<T> for chan<T> {
487505
fn send(+x: T) {
488506
let mut endp = none;
489507
endp <-> self.endp;
@@ -492,7 +510,7 @@ impl chan<T: send> for chan<T> {
492510
}
493511
}
494512

495-
impl port<T: send> for port<T> {
513+
impl port<T: send> of recv<T> for port<T> {
496514
fn recv() -> T {
497515
let mut endp = none;
498516
endp <-> self.endp;
@@ -504,10 +522,10 @@ impl port<T: send> for port<T> {
504522
fn try_recv() -> option<T> {
505523
let mut endp = none;
506524
endp <-> self.endp;
507-
alt pipes::try_recv(unwrap(endp)) {
525+
alt move(pipes::try_recv(unwrap(endp))) {
508526
some(streamp::data(x, endp)) {
509-
self.endp = some(#move(endp));
510-
some(#move(x))
527+
self.endp = some(move!{endp});
528+
some(move!{x})
511529
}
512530
none { none }
513531
}
@@ -528,7 +546,7 @@ impl port<T: send> for port<T> {
528546
}
529547

530548
// Treat a whole bunch of ports as one.
531-
class port_set<T: send> {
549+
class port_set<T: send> : recv<T> {
532550
let mut ports: ~[pipes::port<T>];
533551

534552
new() { self.ports = ~[]; }
@@ -540,12 +558,12 @@ class port_set<T: send> {
540558
fn try_recv() -> option<T> {
541559
let mut result = none;
542560
while result == none && self.ports.len() > 0 {
543-
let i = pipes::wait_many(self.ports.map(|p| p.header()));
561+
let i = wait_many(self.ports.map(|p| p.header()));
544562
// dereferencing an unsafe pointer nonsense to appease the
545563
// borrowchecker.
546-
alt unsafe {(*ptr::addr_of(self.ports[i])).try_recv()} {
564+
alt move(unsafe {(*ptr::addr_of(self.ports[i])).try_recv()}) {
547565
some(m) {
548-
result = some(#move(m));
566+
result = some(move!{m});
549567
}
550568
none {
551569
// Remove this port.
@@ -562,10 +580,19 @@ class port_set<T: send> {
562580
fn recv() -> T {
563581
option::unwrap(self.try_recv())
564582
}
583+
584+
pure fn peek() -> bool {
585+
// It'd be nice to use self.port.each, but that version isn't
586+
// pure.
587+
for vec::each(self.ports) |p| {
588+
if p.peek() { ret true }
589+
}
590+
false
591+
}
565592
}
566593

567-
impl<T: send> of selectable for pipes::port<T> {
568-
pure fn header() -> *pipes::packet_header unchecked {
594+
impl<T: send> of selectable for port<T> {
595+
pure fn header() -> *packet_header unchecked {
569596
alt self.endp {
570597
some(endp) {
571598
endp.header()
@@ -576,13 +603,9 @@ impl<T: send> of selectable for pipes::port<T> {
576603
}
577604

578605

579-
type shared_chan<T: send> = arc::exclusive<pipes::chan<T>>;
580-
581-
trait send_on_shared_chan<T> {
582-
fn send(+x: T);
583-
}
606+
type shared_chan<T: send> = arc::exclusive<chan<T>>;
584607

585-
impl chan<T: send> of send_on_shared_chan<T> for shared_chan<T> {
608+
impl chan<T: send> of channel<T> for shared_chan<T> {
586609
fn send(+x: T) {
587610
let mut xx = some(x);
588611
do self.with |_c, chan| {
@@ -593,6 +616,6 @@ impl chan<T: send> of send_on_shared_chan<T> for shared_chan<T> {
593616
}
594617
}
595618

596-
fn shared_chan<T:send>(+c: pipes::chan<T>) -> shared_chan<T> {
619+
fn shared_chan<T:send>(+c: chan<T>) -> shared_chan<T> {
597620
arc::exclusive(c)
598621
}

0 commit comments

Comments
 (0)