Skip to content

std::comm: replace Handle.id with a method. #12225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),+
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to keep this actually because I'm pretty sure select!() will assert, and it also doesn't quite make much sense. Perhaps the * could also become a + though!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I.e. Select requires at least 1 element?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

) => ({
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