rust/src/test/run-pass/pipe-presentation-examples.rs

174 lines
4.1 KiB
Rust
Raw Normal View History

// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Examples from Eric's internship final presentation.
//
// Code is easier to write in emacs, and it's good to be sure all the
// code samples compile (or not) as they should.
2012-08-08 14:00:00 -05:00
2012-09-05 14:32:05 -05:00
use double_buffer::client::*;
use double_buffer::give_buffer;
macro_rules! select_if (
{
$index:expr,
$count:expr,
$port:path => [
$($message:path$(($($x: ident),+))dont_type_this*
-> $next:ident $e:expr),+
],
$( $ports:path => [
$($messages:path$(($($xs: ident),+))dont_type_this*
-> $nexts:ident $es:expr),+
], )*
} => {
if $index == $count {
2012-09-19 00:45:24 -05:00
match move pipes::try_recv(move $port) {
$(Some($message($($(move $x,)+)* move next)) => {
2012-09-19 00:45:24 -05:00
let $next = move next;
move $e
})+
_ => fail
}
} else {
2012-08-22 19:24:52 -05:00
select_if!(
$index,
$count + 1,
$( $ports => [
$($messages$(($($xs),+))dont_type_this*
-> $nexts $es),+
], )*
2012-08-22 19:24:52 -05:00
)
}
};
{
$index:expr,
$count:expr,
} => {
fail
}
)
macro_rules! select (
{
$( $port:path => {
$($message:path$(($($x: ident),+))dont_type_this*
-> $next:ident $e:expr),+
} )+
} => ({
2012-10-09 23:28:04 -05:00
let index = pipes::selecti([$(($port).header()),+]);
2012-08-22 19:24:52 -05:00
select_if!(index, 0, $( $port => [
$($message$(($($x),+))dont_type_this* -> $next $e),+
2012-08-22 19:24:52 -05:00
], )+)
})
)
// Types and protocols
struct Buffer {
foo: (),
}
impl Buffer : Drop {
fn finalize(&self) {}
}
proto! double_buffer (
acquire:send {
request -> wait_buffer
}
wait_buffer:recv {
give_buffer(Buffer) -> release
}
release:send {
release(Buffer) -> acquire
}
)
// Code examples
fn render(_buffer: &Buffer) {
// A dummy function.
}
fn draw_frame(+channel: double_buffer::client::acquire) {
2012-09-19 00:45:24 -05:00
let channel = request(move channel);
2012-08-22 19:24:52 -05:00
select! (
channel => {
give_buffer(buffer) -> channel {
render(&buffer);
2012-09-19 00:45:24 -05:00
release(move channel, move buffer)
}
}
2012-08-22 19:24:52 -05:00
);
}
fn draw_two_frames(+channel: double_buffer::client::acquire) {
2012-09-19 00:45:24 -05:00
let channel = request(move channel);
2012-08-22 19:24:52 -05:00
let channel = select! (
channel => {
give_buffer(buffer) -> channel {
render(&buffer);
2012-09-19 00:45:24 -05:00
release(move channel, move buffer)
}
}
2012-08-22 19:24:52 -05:00
);
2012-09-19 00:45:24 -05:00
let channel = request(move channel);
2012-08-22 19:24:52 -05:00
select! (
channel => {
give_buffer(buffer) -> channel {
render(&buffer);
2012-09-19 00:45:24 -05:00
release(move channel, move buffer)
}
}
2012-08-22 19:24:52 -05:00
);
}
#[cfg(bad1)]
fn draw_two_frames_bad1(+channel: double_buffer::client::acquire) {
let channel = request(channel);
2012-08-22 19:24:52 -05:00
select! (
channel => {
give_buffer(buffer) -> channel {
render(&buffer);
}
}
2012-08-22 19:24:52 -05:00
);
let channel = request(channel);
2012-08-22 19:24:52 -05:00
select! (
channel => {
give_buffer(buffer) -> channel {
render(&buffer);
release(channel, move buffer)
}
}
2012-08-22 19:24:52 -05:00
);
}
#[cfg(bad2)]
fn draw_two_frames_bad2(+channel: double_buffer::client::acquire) {
let channel = request(channel);
2012-08-22 19:24:52 -05:00
select! (
channel => {
give_buffer(buffer) -> channel {
render(&buffer);
release(channel, move buffer);
render(&buffer);
release(channel, move buffer);
}
}
2012-08-22 19:24:52 -05:00
);
}
2012-08-08 19:24:12 -05:00
fn main() { }