|
12 | 12 | Message passing
|
13 | 13 | */
|
14 | 14 |
|
| 15 | +use cast::transmute; |
15 | 16 | use cast;
|
16 | 17 | use either::{Either, Left, Right};
|
17 | 18 | use kinds::Owned;
|
@@ -192,20 +193,20 @@ impl<T: Owned> Peekable<T> for Port<T> {
|
192 | 193 | fn peek(&self) -> bool {
|
193 | 194 | let mut endp = None;
|
194 | 195 | 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") |
198 | 199 | };
|
199 | 200 | self.endp <-> endp;
|
200 | 201 | peek
|
201 | 202 | }
|
202 | 203 | }
|
203 | 204 |
|
204 | 205 | impl<T: Owned> Selectable for Port<T> {
|
205 |
| - fn header(&self) -> *PacketHeader { |
| 206 | + fn header(&mut self) -> *mut PacketHeader { |
206 | 207 | unsafe {
|
207 | 208 | match self.endp {
|
208 |
| - Some(ref endp) => endp.header(), |
| 209 | + Some(ref mut endp) => endp.header(), |
209 | 210 | None => fail!(~"peeking empty stream")
|
210 | 211 | }
|
211 | 212 | }
|
@@ -327,23 +328,20 @@ impl<T: Owned> ::clone::Clone for SharedChan<T> {
|
327 | 328 | #[allow(non_camel_case_types)]
|
328 | 329 | pub mod oneshot {
|
329 | 330 | priv use core::kinds::Owned;
|
330 |
| - use ptr::to_unsafe_ptr; |
| 331 | + use ptr::to_mut_unsafe_ptr; |
331 | 332 |
|
332 | 333 | pub fn init<T: Owned>() -> (client::Oneshot<T>, server::Oneshot<T>) {
|
333 | 334 | pub use core::pipes::HasBuffer;
|
334 | 335 |
|
335 |
| - let buffer = |
336 |
| - ~::core::pipes::Buffer{ |
| 336 | + let mut buffer = ~::core::pipes::Buffer { |
337 | 337 | header: ::core::pipes::BufferHeader(),
|
338 |
| - data: __Buffer{ |
| 338 | + data: __Buffer { |
339 | 339 | Oneshot: ::core::pipes::mk_packet::<Oneshot<T>>()
|
340 | 340 | },
|
341 | 341 | };
|
342 | 342 | 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) |
347 | 345 | }
|
348 | 346 | }
|
349 | 347 | #[allow(non_camel_case_types)]
|
@@ -497,48 +495,66 @@ pub fn try_send_one<T: Owned>(chan: ChanOne<T>, data: T) -> bool {
|
497 | 495 |
|
498 | 496 |
|
499 | 497 | /// 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 { |
501 | 499 | wait_many(endpoints)
|
502 | 500 | }
|
503 | 501 |
|
504 | 502 | /// 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"), |
511 | 510 | }
|
512 | 511 | }
|
513 | 512 |
|
514 | 513 | /// Receive a message from one of two endpoints.
|
515 | 514 | pub trait Select2<T: Owned, U: Owned> {
|
516 | 515 | /// 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>>; |
518 | 517 | /// Receive a message or fail if a connection closes.
|
519 |
| - fn select(&self) -> Either<T, U>; |
| 518 | + fn select(&mut self) -> Either<T, U>; |
520 | 519 | }
|
521 | 520 |
|
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 | + } |
533 | 541 | }
|
534 | 542 | }
|
535 | 543 |
|
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 | + } |
542 | 558 | }
|
543 | 559 | }
|
544 | 560 | }
|
|
0 commit comments