Skip to content

Commit

Permalink
std::comm: replace Handle.id with a method.
Browse files Browse the repository at this point in the history
The `id` shouldn't be changed by external code, and exposing it publicly
allows to be accidentally changed.

Also, remove the first element special case in the `select!` macro.
  • Loading branch information
huonw authored and alexcrichton committed Feb 13, 2014
1 parent 866d6cc commit 411a01f
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/libstd/comm/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,17 @@ use uint;

macro_rules! select {
(
$name1:pat = $port1:ident.$meth1:ident() => $code1:expr,
$($name:pat = $port:ident.$meth:ident() => $code:expr),*
$($name:pat = $port:ident.$meth:ident() => $code:expr),+
) => ({
use std::comm::Select;
let sel = Select::new();
let mut $port1 = sel.handle(&$port1);
$( let mut $port = sel.handle(&$port); )*
$( let mut $port = sel.handle(&$port); )+
unsafe {
$port1.add();
$( $port.add(); )*
$( $port.add(); )+
}
let ret = sel.wait();
if ret == $port1.id { let $name1 = $port1.$meth1(); $code1 }
$( else if ret == $port.id { let $name = $port.$meth(); $code } )*
else { unreachable!() }
$( if ret == $port.id() { let $name = $port.$meth(); $code } else )+
{ unreachable!() }
})
}

Expand All @@ -94,7 +90,7 @@ pub struct Select {
pub struct Handle<'port, T> {
/// The ID of this handle, used to compare against the return value of
/// `Select::wait()`
id: uint,
priv id: uint,
priv selector: &'port Select,
priv next: *mut Handle<'static, ()>,
priv prev: *mut Handle<'static, ()>,
Expand Down Expand Up @@ -150,7 +146,7 @@ impl Select {

/// Waits for an event on this port set. The returned valus is *not* and
/// index, but rather an id. This id can be queried against any active
/// `Handle` structures (each one has a public `id` field). The handle with
/// `Handle` structures (each one has an `id` method). The handle with
/// the matching `id` will have some sort of event available on it. The
/// event could either be that data is available or the corresponding
/// channel has been closed.
Expand Down Expand Up @@ -242,6 +238,10 @@ impl Select {
}

impl<'port, T: Send> Handle<'port, T> {
/// Retrieve the id of this handle.
#[inline]
pub fn id(&self) -> uint { self.id }

/// Receive a value on the underlying port. Has the same semantics as
/// `Port.recv`
pub fn recv(&mut self) -> T { self.port.recv() }
Expand Down Expand Up @@ -355,7 +355,7 @@ mod test {
)
drop(c2);
select! (
bar = p2.recv_opt() => { assert_eq!(bar, None); },
bar = p2.recv_opt() => { assert_eq!(bar, None); }
)
})

Expand Down

0 comments on commit 411a01f

Please sign in to comment.