Skip to content

Commit 12319e1

Browse files
committed
---
yaml --- r: 60116 b: refs/heads/master c: 6a44482 h: refs/heads/master v: v3
1 parent 73884d7 commit 12319e1

File tree

5 files changed

+247
-203
lines changed

5 files changed

+247
-203
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 803a4f45fa5b581155e638143afb97195cfa9f2e
2+
refs/heads/master: 6a44482b175a5486039fd5f2fd32f1051ce80e50
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 2d28d645422c1617be58c8ca7ad9a457264ca850
55
refs/heads/try: c50a9d5b664478e533ba1d1d353213d70c8ad589

trunk/src/libcore/comm.rs

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
Message passing
1313
*/
1414

15+
use cast::transmute;
1516
use cast;
1617
use either::{Either, Left, Right};
1718
use kinds::Owned;
@@ -192,20 +193,20 @@ impl<T: Owned> Peekable<T> for Port<T> {
192193
fn peek(&self) -> bool {
193194
let mut endp = None;
194195
endp <-> self.endp;
195-
let peek = match &endp {
196-
&Some(ref endp) => peek(endp),
197-
&None => fail!(~"peeking empty stream")
196+
let peek = match endp {
197+
Some(ref mut endp) => peek(endp),
198+
None => fail!(~"peeking empty stream")
198199
};
199200
self.endp <-> endp;
200201
peek
201202
}
202203
}
203204
204205
impl<T: Owned> Selectable for Port<T> {
205-
fn header(&self) -> *PacketHeader {
206+
fn header(&mut self) -> *mut PacketHeader {
206207
unsafe {
207208
match self.endp {
208-
Some(ref endp) => endp.header(),
209+
Some(ref mut endp) => endp.header(),
209210
None => fail!(~"peeking empty stream")
210211
}
211212
}
@@ -327,23 +328,20 @@ impl<T: Owned> ::clone::Clone for SharedChan<T> {
327328
#[allow(non_camel_case_types)]
328329
pub mod oneshot {
329330
priv use core::kinds::Owned;
330-
use ptr::to_unsafe_ptr;
331+
use ptr::to_mut_unsafe_ptr;
331332
332333
pub fn init<T: Owned>() -> (client::Oneshot<T>, server::Oneshot<T>) {
333334
pub use core::pipes::HasBuffer;
334335
335-
let buffer =
336-
~::core::pipes::Buffer{
336+
let mut buffer = ~::core::pipes::Buffer {
337337
header: ::core::pipes::BufferHeader(),
338-
data: __Buffer{
338+
data: __Buffer {
339339
Oneshot: ::core::pipes::mk_packet::<Oneshot<T>>()
340340
},
341341
};
342342
do ::core::pipes::entangle_buffer(buffer) |buffer, data| {
343-
{
344-
data.Oneshot.set_buffer(buffer);
345-
to_unsafe_ptr(&data.Oneshot)
346-
}
343+
data.Oneshot.set_buffer(buffer);
344+
to_mut_unsafe_ptr(&mut data.Oneshot)
347345
}
348346
}
349347
#[allow(non_camel_case_types)]
@@ -497,48 +495,66 @@ pub fn try_send_one<T: Owned>(chan: ChanOne<T>, data: T) -> bool {
497495
498496
499497
/// Returns the index of an endpoint that is ready to receive.
500-
pub fn selecti<T: Selectable>(endpoints: &[T]) -> uint {
498+
pub fn selecti<T: Selectable>(endpoints: &mut [T]) -> uint {
501499
wait_many(endpoints)
502500
}
503501
504502
/// Returns 0 or 1 depending on which endpoint is ready to receive
505-
pub fn select2i<A: Selectable, B: Selectable>(a: &A, b: &B) ->
506-
Either<(), ()> {
507-
match wait_many([a.header(), b.header()]) {
508-
0 => Left(()),
509-
1 => Right(()),
510-
_ => fail!(~"wait returned unexpected index")
503+
pub fn select2i<A:Selectable, B:Selectable>(a: &mut A, b: &mut B)
504+
-> Either<(), ()> {
505+
let mut endpoints = [ a.header(), b.header() ];
506+
match wait_many(endpoints) {
507+
0 => Left(()),
508+
1 => Right(()),
509+
_ => fail!(~"wait returned unexpected index"),
511510
}
512511
}
513512
514513
/// Receive a message from one of two endpoints.
515514
pub trait Select2<T: Owned, U: Owned> {
516515
/// Receive a message or return `None` if a connection closes.
517-
fn try_select(&self) -> Either<Option<T>, Option<U>>;
516+
fn try_select(&mut self) -> Either<Option<T>, Option<U>>;
518517
/// Receive a message or fail if a connection closes.
519-
fn select(&self) -> Either<T, U>;
518+
fn select(&mut self) -> Either<T, U>;
520519
}
521520
522-
impl<T: Owned, U: Owned,
523-
Left: Selectable + GenericPort<T>,
524-
Right: Selectable + GenericPort<U>>
525-
Select2<T, U> for (Left, Right) {
526-
527-
fn select(&self) -> Either<T, U> {
528-
match *self {
529-
(ref lp, ref rp) => match select2i(lp, rp) {
530-
Left(()) => Left (lp.recv()),
531-
Right(()) => Right(rp.recv())
532-
}
521+
impl<T:Owned,
522+
U:Owned,
523+
Left:Selectable + GenericPort<T>,
524+
Right:Selectable + GenericPort<U>>
525+
Select2<T, U>
526+
for (Left, Right) {
527+
fn select(&mut self) -> Either<T, U> {
528+
// XXX: Bad borrow check workaround.
529+
unsafe {
530+
let this: &(Left, Right) = transmute(self);
531+
match *this {
532+
(ref lp, ref rp) => {
533+
let lp: &mut Left = transmute(lp);
534+
let rp: &mut Right = transmute(rp);
535+
match select2i(lp, rp) {
536+
Left(()) => Left(lp.recv()),
537+
Right(()) => Right(rp.recv()),
538+
}
539+
}
540+
}
533541
}
534542
}
535543
536-
fn try_select(&self) -> Either<Option<T>, Option<U>> {
537-
match *self {
538-
(ref lp, ref rp) => match select2i(lp, rp) {
539-
Left(()) => Left (lp.try_recv()),
540-
Right(()) => Right(rp.try_recv())
541-
}
544+
fn try_select(&mut self) -> Either<Option<T>, Option<U>> {
545+
// XXX: Bad borrow check workaround.
546+
unsafe {
547+
let this: &(Left, Right) = transmute(self);
548+
match *this {
549+
(ref lp, ref rp) => {
550+
let lp: &mut Left = transmute(lp);
551+
let rp: &mut Right = transmute(rp);
552+
match select2i(lp, rp) {
553+
Left(()) => Left (lp.try_recv()),
554+
Right(()) => Right(rp.try_recv()),
555+
}
556+
}
557+
}
542558
}
543559
}
544560
}

0 commit comments

Comments
 (0)