Skip to content

Commit a0cd462

Browse files
author
Eric Holk
committed
---
yaml --- r: 4463 b: refs/heads/master c: 86babab h: refs/heads/master i: 4461: e5e6333 4459: b172142 4455: 3be4011 4447: edc5b32 v: v3
1 parent 119fc58 commit a0cd462

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 0aeddb3673459969fdbb32018f9c59f603607cf5
2+
refs/heads/master: 86babab2fe92223511042e9cb4b233841cffa054

trunk/src/lib/comm.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ export _chan;
66
export _port;
77

88
export mk_port;
9+
export chan_from_unsafe_ptr;
910

1011
native "rust" mod rustrt {
1112
type void;
1213
type rust_chan;
1314
type rust_port;
1415

1516
fn new_chan(po : *rust_port) -> *rust_chan;
16-
fn del_chan(ch : *rust_chan);
17+
fn take_chan(ch : *rust_chan);
1718
fn drop_chan(ch : *rust_chan);
1819
fn chan_send(ch: *rust_chan, v : *void);
1920

@@ -42,6 +43,16 @@ obj _chan[T](raw_chan : @chan_ptr) {
4243
rustrt::chan_send(**raw_chan,
4344
unsafe::reinterpret_cast(ptr::addr_of(v)));
4445
}
46+
47+
// Use this to get something we can send over a channel.
48+
fn unsafe_ptr() -> *u8 {
49+
rustrt::take_chan(**raw_chan);
50+
ret unsafe::reinterpret_cast(**raw_chan);
51+
}
52+
}
53+
54+
fn chan_from_unsafe_ptr[T](ch : *u8) -> _chan[T] {
55+
_chan(@chan_ptr(unsafe::reinterpret_cast(ch)))
4556
}
4657

4758
obj _port[T](raw_port : @port_ptr) {
Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1+
use std;
2+
3+
import std::comm;
4+
15
fn main() { test05(); }
26

3-
fn test05_start(ch: chan[int]) { ch <| 10; ch <| 20; ch <| 30; }
7+
fn test05_start(pch: *u8) {
8+
let ch = comm::chan_from_unsafe_ptr(pch);
9+
10+
ch.send(10);
11+
ch.send(20);
12+
ch.send(30);
13+
}
414

515
fn test05() {
6-
let po: port[int] = port();
7-
let ch: chan[int] = chan(po);
8-
spawn test05_start(chan(po));
9-
let value: int;
10-
po |> value;
11-
po |> value;
12-
po |> value;
16+
let po = comm::mk_port[int]();
17+
let ch = po.mk_chan();
18+
spawn test05_start(ch.unsafe_ptr());
19+
let value = po.recv();
20+
value = po.recv();
21+
value = po.recv();
1322
assert (value == 30);
1423
}

0 commit comments

Comments
 (0)