From e1637f105a56bc136a5a598aa3b28408bd8a4d69 Mon Sep 17 00:00:00 2001 From: Jack Moffitt Date: Wed, 8 Jan 2014 21:05:03 -0700 Subject: [PATCH] Upgrade to latest Rust. --- Makefile.in | 2 +- glut.rc | 5 +- glut.rs | 249 ++++++++++++++++++++-------------------------------- machack.rs | 2 +- test.rs | 10 +-- 5 files changed, 102 insertions(+), 166 deletions(-) diff --git a/Makefile.in b/Makefile.in index d29758d..551e3a3 100644 --- a/Makefile.in +++ b/Makefile.in @@ -19,7 +19,7 @@ all: librustglut.dummy $(CC) $< -o $@ -c $(CFLAGS) librustglut.dummy: glut.rc $(RUST_SRC) libandroid-glue.a - $(RUSTC) $(RUSTFLAGS) $< -o $@ + $(RUSTC) $(RUSTFLAGS) $< --out-dir . --lib touch $@ rustglut-test: glut.rc $(RUST_SRC) libandroid-glue.a diff --git a/glut.rc b/glut.rc index 507ad91..c174b0f 100644 --- a/glut.rc +++ b/glut.rc @@ -7,13 +7,10 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#[link(name = "glut", - vers = "0.1")]; +#[crate_id = "github.com/mozilla-servo/rust-glut#glut:0.1"]; #[crate_type = "lib"]; -#[feature(globs)]; #[feature(macro_rules)]; -#[feature(managed_boxes)]; extern mod std; diff --git a/glut.rs b/glut.rs index 7c590ef..7b548d8 100644 --- a/glut.rs +++ b/glut.rs @@ -9,11 +9,10 @@ /* automatically generated by rust-bindgen */ -use std::libc::*; +use std::cast; +use std::libc::{c_int, c_uint, c_uchar, c_char}; use std::local_data; use std::ptr::{null, to_unsafe_ptr}; -use std::cast; -use std::cast::transmute; /* FIXME: global variable glutStrokeRoman */ @@ -64,89 +63,78 @@ pub static HAVE_PRECISE_MOUSE_WHEEL: bool = false; #[cfg(target_os="macos")] pub static HAVE_PRECISE_MOUSE_WHEEL: bool = true; +pub trait DisplayCallback { fn call(&self); } +pub trait KeyboardCallback { fn call(&self, key: c_uchar, x: c_int, y: c_int); } +pub trait MouseCallback { fn call(&self, button: c_int, state: c_int, x: c_int, y: c_int); } +pub trait MotionCallback { fn call(&self, x: c_int, y: c_int); } +pub trait PassiveMotionCallback { fn call(&self, x: c_int, y: c_int); } +pub trait TimerCallback { fn call(&self); } +pub trait ReshapeCallback { fn call(&self, x: c_int, y: c_int); } +pub trait IdleCallback { fn call(&self); } +pub trait MouseWheelCallback { fn call(&self, wheel: c_int, direction: c_int, x: c_int, y: c_int); } + macro_rules! local_data_key( ($name:ident, $func:ty) => ( static $name: local_data::Key<$func> = &local_data::Key; ); ) -pub type DisplayFun = ~fn(); -pub type KeyboardFun = ~fn(key: c_uchar, x: c_int, y: c_int); -pub type MouseFun = ~fn(button: c_int, state: c_int, x: c_int, y: c_int); -pub type MotionFun = ~fn(x: c_int, y: c_int); -pub type PassiveMotionFun = ~fn(x: c_int, y: c_int); -pub type TimerFun = ~[~fn()]; -pub type ReshapeFun = ~fn(x: c_int, y: c_int); -pub type IdleFun = ~fn(); -#[cfg(target_os="linux")] -#[cfg(target_os="android")] -pub type MouseWheelFun = ~fn(wheel: c_int, direction: c_int, x: c_int, y: c_int); - - -local_data_key!(display_tls_key, DisplayFun) -local_data_key!(keyboard_tls_key, KeyboardFun) -local_data_key!(mouse_tls_key, MouseFun) -local_data_key!(motion_tls_key, MotionFun) -local_data_key!(passive_motion_tls_key, PassiveMotionFun) -local_data_key!(timer_tls_key, TimerFun) -local_data_key!(reshape_tls_key, ReshapeFun) -local_data_key!(idle_tls_key, IdleFun) -#[cfg(target_os="linux")] -#[cfg(target_os="android")] -local_data_key!(mouse_wheel_tls_key, MouseWheelFun) +local_data_key!(display_tls_key, ~DisplayCallback) +local_data_key!(keyboard_tls_key, ~KeyboardCallback) +local_data_key!(mouse_tls_key, ~MouseCallback) +local_data_key!(motion_tls_key, ~MotionCallback) +local_data_key!(passive_motion_tls_key, ~PassiveMotionCallback) +local_data_key!(timer_tls_key, ~TimerCallback) +local_data_key!(reshape_tls_key, ~ReshapeCallback) +local_data_key!(idle_tls_key, ~IdleCallback) +local_data_key!(mouse_wheel_tls_key, ~MouseWheelCallback) pub enum State { WindowWidth, WindowHeight } -#[fixed_stack_segment] #[inline(never)] pub fn init() { unsafe { let argc = 0 as c_int; let glut = ~"glut"; - do glut.to_c_str().with_ref |command| { + glut.to_c_str().with_ref(|command| { let argv: (*u8, *u8) = (command as *u8, null()); - let argv_p = transmute(to_unsafe_ptr(&argv)); + let argv_p = cast::transmute(to_unsafe_ptr(&argv)); glutInit(to_unsafe_ptr(&argc), argv_p); - } + }); } } -#[fixed_stack_segment] #[inline(never)] pub fn create_window(name: ~str) -> Window { unsafe { - do name.to_c_str().with_ref |bytes| { + name.to_c_str().with_ref(|bytes| { Window(glutCreateWindow(bytes as *c_char)) - } + }) } } -#[fixed_stack_segment] #[inline(never)] pub fn destroy_window(window: Window) { unsafe { glutDestroyWindow(*window); } } -#[fixed_stack_segment] #[inline(never)] pub fn set_window(window: Window) { unsafe { glutSetWindow(*window); } } -#[fixed_stack_segment] #[inline(never)] pub fn set_window_title(_window: Window, title: &str) { unsafe { let title = title.to_owned(); - do title.to_c_str().with_ref |bytes| { + title.to_c_str().with_ref(|bytes| { glutSetWindowTitle(bytes as *c_char); - } + }); } } -#[fixed_stack_segment] #[inline(never)] pub fn reshape_window(window: Window, width: c_int, height: c_int) { unsafe { let current_window = glutGetWindow(); @@ -156,148 +144,120 @@ pub fn reshape_window(window: Window, width: c_int, height: c_int) { } } -#[fixed_stack_segment] #[inline(never)] pub extern "C" fn display_callback() { - do local_data::get(display_tls_key) |data| { - do data.as_ref().map |&ref cb| { - (**cb)(); - }; - } + local_data::get(display_tls_key, |callback| { + callback.as_ref().map(|&ref cb| { + cb.call(); + }); + }); } -#[fixed_stack_segment] #[inline(never)] -pub fn display_func(callback: DisplayFun) { +pub fn display_func(callback: ~DisplayCallback) { local_data::set(display_tls_key, callback); unsafe { glutDisplayFunc(display_callback); } } -#[fixed_stack_segment] #[inline(never)] pub extern "C" fn keyboard_callback(key: c_uchar, x: c_int, y: c_int) { - do local_data::get(keyboard_tls_key) |data| { - do data.as_ref().map |&ref cb| { - (**cb)(key, x, y); - }; - } + local_data::get(keyboard_tls_key, |callback| { + callback.as_ref().map(|&ref cb| { + cb.call(key, x, y); + }); + }); } -#[fixed_stack_segment] #[inline(never)] -pub fn keyboard_func(callback: KeyboardFun) { +pub fn keyboard_func(callback: ~KeyboardCallback) { local_data::set(keyboard_tls_key, callback); unsafe { glutKeyboardFunc(keyboard_callback); } } -#[fixed_stack_segment] #[inline(never)] pub extern "C" fn mouse_callback(button: c_int, state: c_int, x: c_int, y: c_int) { - do local_data::get(mouse_tls_key) |data| { - do data.as_ref().map |&ref cb| { - (**cb)(button, state, x, y); - }; - } + local_data::get(mouse_tls_key, |callback| { + callback.as_ref().map(|&ref cb| { + cb.call(button, state, x, y); + }); + }); } -#[fixed_stack_segment] #[inline(never)] -pub fn mouse_func(callback: MouseFun) { +pub fn mouse_func(callback: ~MouseCallback) { local_data::set(mouse_tls_key, callback); unsafe { glutMouseFunc(mouse_callback); } } -#[fixed_stack_segment] #[inline(never)] pub extern "C" fn motion_callback(x: c_int, y: c_int) { - do local_data::get(motion_tls_key) |data| { - do data.as_ref().map |&ref cb| { - (**cb)(x, y); - }; - } + local_data::get(motion_tls_key, |callback| { + callback.as_ref().map(|&ref cb| { + cb.call(x, y); + }); + }); } -#[fixed_stack_segment] #[inline(never)] -pub fn motion_func(callback: MotionFun) { +pub fn motion_func(callback: ~MotionCallback) { local_data::set(motion_tls_key, callback); unsafe { glutMotionFunc(motion_callback); } } -#[fixed_stack_segment] #[inline(never)] pub extern "C" fn passive_motion_callback(x: c_int, y: c_int) { - do local_data::get(passive_motion_tls_key) |data| { - do data.as_ref().map |&ref cb| { - (**cb)(x, y); - }; - } + local_data::get(passive_motion_tls_key, |callback| { + callback.as_ref().map(|&ref cb| { + cb.call(x, y); + }); + }); } -#[fixed_stack_segment] #[inline(never)] -pub fn passive_motion_func(callback: PassiveMotionFun) { +pub fn passive_motion_func(callback: ~PassiveMotionCallback) { local_data::set(passive_motion_tls_key, callback); unsafe { glutPassiveMotionFunc(passive_motion_callback); } } -#[fixed_stack_segment] #[inline(never)] -pub extern "C" fn timer_callback(index: int) { - do local_data::get(timer_tls_key) |data| { - do data.as_ref().map |&ref cb| { - ((**cb)[index as uint])(); - }; - } +pub extern "C" fn timer_callback(_index: int) { + local_data::get(timer_tls_key, |callback| { + callback.as_ref().map(|&ref cb| { + cb.call(); + }); + }); } -#[fixed_stack_segment] #[inline(never)] -//FIXME(aydin.kim): we need to get rid of @mut. This is just a workaround for pushing owned fn() to callback array. -pub fn timer_func(msecs: u32, callback: @mut ~fn()) { - do local_data::get(timer_tls_key) |data| { - let callbacks; - if data.is_none() { - callbacks = @mut ~[]; - local_data::set(timer_tls_key, unsafe { cast::transmute(callbacks) }); - } - else { - callbacks = unsafe { cast::transmute(data.unwrap())}; - } - callbacks.push(callback); - let index = (callbacks.len() - 1) as c_int; - unsafe { - glutTimerFunc(msecs, timer_callback, index); - } +pub fn timer_func(msecs: u32, callback: ~TimerCallback) { + local_data::set(timer_tls_key, callback); + unsafe { + glutTimerFunc(msecs, timer_callback, 0); } } -#[fixed_stack_segment] #[inline(never)] pub extern "C" fn reshape_callback(width: c_int, height: c_int) { - do local_data::get(reshape_tls_key) |data| { - do data.as_ref().map |&ref cb| { - (**cb)(width, height); - }; - } + local_data::get(reshape_tls_key, |callback| { + callback.as_ref().map(|&ref cb| { + cb.call(width, height); + }); + }); } -#[fixed_stack_segment] #[inline(never)] -pub fn reshape_func(_window: Window, callback: ReshapeFun) { +pub fn reshape_func(_window: Window, callback: ~ReshapeCallback) { local_data::set(reshape_tls_key, callback); unsafe { glutReshapeFunc(reshape_callback); } } -#[fixed_stack_segment] #[inline(never)] pub extern "C" fn idle_callback() { - do local_data::get(idle_tls_key) |data| { - do data.as_ref().map |&ref cb| { - (**cb)(); - }; - } + local_data::get(idle_tls_key, |callback| { + callback.as_ref().map(|&ref cb| { + cb.call(); + }); + }); } -#[fixed_stack_segment] #[inline(never)] -pub fn idle_func(callback: IdleFun) { +pub fn idle_func(callback: ~IdleCallback) { local_data::set(idle_tls_key, callback); unsafe { glutIdleFunc(idle_callback); @@ -309,19 +269,17 @@ pub fn idle_func(callback: IdleFun) { // This is not part of the standard, but it's supported by freeglut and our Mac hack. #[cfg(target_os="linux")] #[cfg(target_os="android")] -#[fixed_stack_segment] #[inline(never)] pub extern "C" fn mouse_wheel_callback(wheel: c_int, direction: c_int, x: c_int, y: c_int) { - do local_data::get(mouse_wheel_tls_key) |data| { - do data.as_ref().map |&ref cb| { - (**cb)(wheel, direction, x, y); - }; - } + local_data::get(mouse_wheel_tls_key, |callback| { + callback.as_ref().map(|&ref cb| { + cb.call(wheel, direction, x, y); + }); + }); } #[cfg(target_os="linux")] #[cfg(target_os="android")] -#[fixed_stack_segment] #[inline(never)] -pub fn mouse_wheel_func(callback: MouseWheelFun) { +pub fn mouse_wheel_func(callback: ~MouseWheelCallback) { local_data::set(mouse_wheel_tls_key, callback); unsafe { glutMouseWheelFunc(mouse_wheel_callback); @@ -329,13 +287,11 @@ pub fn mouse_wheel_func(callback: MouseWheelFun) { } #[cfg(target_os="macos")] -#[fixed_stack_segment] #[inline(never)] -pub fn mouse_wheel_func(callback: @fn(wheel: c_int, direction: c_int, x: c_int, y: c_int)) { +pub fn mouse_wheel_func(callback: ~MouseWheelCallback) { local_data::set(mouse_wheel_tls_key, callback); } #[cfg(target_os="macos")] -#[fixed_stack_segment] #[inline(never)] pub fn check_loop() { unsafe { glutCheckLoop(); @@ -344,35 +300,30 @@ pub fn check_loop() { #[cfg(target_os="linux")] #[cfg(target_os="android")] -#[fixed_stack_segment] #[inline(never)] pub fn check_loop() { unsafe { glutMainLoopEvent(); } } -#[fixed_stack_segment] #[inline(never)] pub fn init_display_mode(mode: c_uint) { unsafe { glutInitDisplayMode(mode); } } -#[fixed_stack_segment] #[inline(never)] pub fn swap_buffers() { unsafe { glutSwapBuffers(); } } -#[fixed_stack_segment] #[inline(never)] pub fn post_redisplay() { unsafe { glutPostRedisplay(); } } -#[fixed_stack_segment] #[inline(never)] pub fn get(state: State) -> c_int { unsafe { let glut_state; @@ -384,52 +335,40 @@ pub fn get(state: State) -> c_int { } } -#[fixed_stack_segment] #[inline(never)] pub fn get_modifiers() -> c_int { unsafe { glutGetModifiers() } } -#[fixed_stack_segment] #[inline(never)] pub fn init_window_size(width:uint, height:uint) { unsafe { glutInitWindowSize(width as c_int, height as c_int); } } -#[cfg(target_os="macos")] -#[nolink] -#[link_args="-framework GLUT"] -extern { -} - -#[cfg(target_os="linux")] -#[link_args="-lglut"] -extern { -} - #[cfg(target_os="android")] -#[link_args="-landroid-glue"] -extern { -} +#[link(name="android-glue")] +extern {} #[cfg(target_os="macos")] -#[nolink] +#[link(name="GLUT", kind="framework")] extern { // Mac GLUT extension. fn glutCheckLoop(); } +#[cfg(target_os="linux")] +#[link(name="glut")] +extern {} + #[cfg(target_os="linux")] #[cfg(target_os="android")] -#[nolink] extern { // freeglut extension. fn glutMainLoopEvent(); } -#[nolink] extern { pub fn glutInit(argcp: *c_int, argv: **c_char); diff --git a/machack.rs b/machack.rs index 796bd42..ea2f3ed 100644 --- a/machack.rs +++ b/machack.rs @@ -11,7 +11,7 @@ use std::ptr::null; use machack::cocoa::base::{SEL, class_addMethod, id, msg_send_double, msg_send_id, objc_getClass}; use machack::cocoa::base::{sel_registerName}; -#[link_args="-framework Carbon"] +#[link(name="Carbon", kind="framework")] extern { // Carbon API. #[fast_ffi] diff --git a/test.rs b/test.rs index 77bf8ff..eb397c9 100644 --- a/test.rs +++ b/test.rs @@ -166,12 +166,12 @@ fn test_triangle_and_square() { } */ -pub fn spawn_listener(f: ~fn(Port)) -> Chan { - let (setup_po, setup_ch) = comm::stream(); - do task::spawn { - let (po, ch) = comm::stream(); +pub fn spawn_listener(f: proc(Port)) -> Chan { + let (setup_po, setup_ch) = Chan::new(); + spawn(proc() { + let (po, ch) = Chan::new(); setup_ch.send(ch); f(po); - } + }); setup_po.recv() }