aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorRoy Frostig <[email protected]>2010-07-25 21:45:09 -0700
committerRoy Frostig <[email protected]>2010-07-25 21:45:09 -0700
commit5b6e714d05a0c96ade618256a67a9cb7f337f196 (patch)
tree45406973ea3c0ffb1a9e070588f4ca5fc8c7744c /src/lib
parentDon't write to NULL after calling C natives returning void. (diff)
downloadrust-5b6e714d05a0c96ade618256a67a9cb7f337f196.tar.xz
rust-5b6e714d05a0c96ade618256a67a9cb7f337f196.zip
Expose an RNG (the one used by our runtime) to Rust via std.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/rand.rs25
-rw-r--r--src/lib/std.rc2
2 files changed, 27 insertions, 0 deletions
diff --git a/src/lib/rand.rs b/src/lib/rand.rs
new file mode 100644
index 00000000..96dcbaf7
--- /dev/null
+++ b/src/lib/rand.rs
@@ -0,0 +1,25 @@
+/**
+ * Bindings the runtime's random number generator (ISAAC).
+ */
+
+native "rust" mod rustrt {
+ type rctx;
+ fn rand_new() -> rctx;
+ fn rand_next(rctx c) -> u32;
+ fn rand_free(rctx c);
+}
+
+type rng = obj { fn next() -> u32; };
+
+fn mk_rng() -> rng {
+ obj rt_rng(rustrt.rctx c) {
+ fn next() -> u32 {
+ ret rustrt.rand_next(c);
+ }
+ drop {
+ rustrt.rand_free(c);
+ }
+ }
+
+ ret rt_rng(rustrt.rand_new());
+}
diff --git a/src/lib/std.rc b/src/lib/std.rc
index dfc404ec..8c956691 100644
--- a/src/lib/std.rc
+++ b/src/lib/std.rc
@@ -28,6 +28,7 @@ auth _vec = unsafe;
auth _int.next_power_of_two = unsafe;
auth map.mk_hashmap = unsafe;
+auth rand.mk_rng = unsafe;
// Target-OS module.
@@ -43,3 +44,4 @@ alt (target_os) {
mod map;
mod deque;
+mod rand;